gpt4 book ai didi

c++ - 使用 openGL 未按预期显示立方体

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:00 26 4
gpt4 key购买 nike

我的立方体不是立方体,而是如下所示:

enter image description here

我似乎无法弄清楚为什么它没有呈现为立方体我想知道这里是否有人有任何指示。

我的代码如下:

#define GLEW_STATIC

#include <GL/glew.h>
#include <GLFW/glfw3.h> // glfw manages the window
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdio.h>
#include <chrono>
#include <math.h>

#define GLSL(version , shader) "#version " #version " core \n" #shader

int main()
{
glfwInit();

// setup glfw
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

// create window
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
glfwMakeContextCurrent(window);

// window complete OpenGL code below ---------------------------------------

// initialise glew
glewExperimental = GL_TRUE;
glewInit();

glEnable(GL_DEPTH_TEST);

GLuint elements[] = {
0, 1, 2
};

// create the shape of triangle

GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,

-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f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 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,

-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
};

const GLchar* vertexSource = GLSL(150,
in vec3 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
void main()
{
Color = color;
Texcoord = texcoord;
gl_Position = proj * view * model * vec4(position, 1.0);
}
);

const GLchar* fragmentSource = GLSL(150,
in vec3 Color;
out vec4 outColor;
void main()
{
outColor = vec4(Color, 1.0);
}
);

GLuint vao;
glGenVertexArrays(1 ,&vao);
glBindVertexArray(vao);

GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

// create our shaders
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);


//GLuint uniColor = glGetUniformLocation(shaderProgram, "triangleColor");
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE,5 * sizeof(float), 0);

GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
auto t_start = std::chrono::high_resolution_clock::now();

GLint uniTrans = glGetUniformLocation(shaderProgram, "model");
GLint uniView = glGetUniformLocation(shaderProgram, "view");

glm::mat4 view = glm::lookAt(
glm::vec3(1.2f, 1.2f, 1.2f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f)
);

glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 1.0f, 10.0f);
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));


while(!glfwWindowShouldClose(window))
{

auto t_now = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration_cast<std::chrono::duration<float> >(t_now - t_start).count();

glUniform3f(colAttrib, (sin(time * 4.0f) + 1.0f) / 2.0f, 0.0f, 0.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(
model,
time * glm::radians(180.0f),
glm::vec3(0.0f, 0.0f, 1.0f)
);

glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));

glDrawArrays(GL_TRIANGLES, 0, 36);

glfwPollEvents();
glfwSwapBuffers(window);

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}

glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);

glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
glDeleteVertexArrays(1, &vao);
glDeleteVertexArrays(1, &vao);

glfwTerminate();

return 0;
}

最佳答案

您的 glVertexAttribPointer() 调用暗示数据布局与 vertices 中的内容不匹配。

指定与您的数据相匹配的布局(四分量顶点 (XYZW) + 四分量颜色 (RGBA)):

glVertexAttribPointer
(
posAttrib,
4, // four-component vertices (XYZW)
GL_FLOAT,
GL_FALSE,
8 * sizeof(float), // a new vertex every 8 floats
0 // first vertex starts zero floats in
);

glVertexAttribPointer
(
colAttrib,
4, // four-component colors (RGBA)
GL_FLOAT,
GL_FALSE,
8 * sizeof(float), // a new color every 8 floats
(void*)(4 * sizeof(float)) // first color starts 4 floats in
);

关于c++ - 使用 openGL 未按预期显示立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50960610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com