带有VBO和着色器的C ++ openGL静态窗口

时间:2016-05-26 23:31:57

标签: c++ opengl glsl vbo

所以我一直在构建一个非常简单的代码片段来渲染.OBJ文件并让用户环顾四周使用VBO和最简单的GLSL着色器是不可想象的,但是在运行时窗口只是一个静态图像(看起来喜欢从0,0,0看到的对象。如果没有着色器程序,它仍然可以正常呈现,只是没有颜色。知道我在做错什么吗?

VBO&着色器:

//Create buffer for vertex data (x,y,z)
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vector3f), &vertices[0], GL_STATIC_DRAW);
//Create buffer for color data (r,g,b)
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, temp.size() * sizeof(vector3f), &temp[0], GL_STATIC_DRAW);

//Create and compile shaders
GLuint vertShader = 0, fragShader = 0, program = 0;
createShader("vertex.glsl", GL_VERTEX_SHADER, vertShader);
createShader("fragment.glsl", GL_FRAGMENT_SHADER, fragShader);
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glBindAttribLocation(program, vertexAttribIndex, "vertex_position");
glBindAttribLocation(program, colorAttribIndex, "vertex_colour");
glLinkProgram(program);
printShaderInfoLog(vertShader);
printProgramInfoLog(program);

//Set attribute pointers for GLSL
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(vertexAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(vertexAttribIndex);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(colorAttribIndex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(colorAttribIndex);

渲染循环:

while (!glfwWindowShouldClose(window)) {
    glClearColor(0.0F, 0.0F, 0.0F, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glUseProgram(program);
    glViewport(0, 0, width, height);
    gluPerspective(70.0, width / height, 1.0, 30.0);
    glMatrixMode(GL_MODELVIEW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLES, 0, vertBufferSize);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glfwSwapBuffers(window);
    glfwPollEvents();
}

顶点着色器:

#version 420

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_colour;

out vec3 colour;
out vec4 gl_Position;

void main () {
  colour = vertex_colour;
  gl_Position = vec4 (vertex_position, 1.0);
}

片段着色器:

#version 420

in vec3 colour;
out vec4 colorOut;

void main () {
  colorOut = vec4(colour, 1.0);
}

对不起,如果我错过任何明显的事情,我已经打了好几个小时。

0 个答案:

没有答案