VBO not working

VBO not working

本文关键字:working not VBO      更新时间:2023-10-16

当我尝试使用vbo时,我只得到一个黑屏。我将GLFW与GLEW一起使用。它对纹理也有同样的作用,但我没有用纹理来判断它是否有效,但出于某种原因,它不起作用。我让它工作,但我对代码做了一些更改,所以我想我可能已经做了一些事情。PS:如果代码中有错误,请告诉我,因为我删除了不影响渲染的代码,我可能已经删除了事故上的重要代码

这是main.cpp,去掉了一些不影响OpenGL的东西:

//Include all OpenGL stuff, such as gl.h, glew.h, and glfw3.h
#include "gl.h"
//Include a header which adds some functions for loading shaders easier, there is nothing wrong with this code though
#include "shader.h"
float data[3][3] = {
{0.0, 1.0, 0.0},
{-1.0, -1.0, 0.0},
{1.0, -1.0, 0.0}
};
int main()
{
   if (!glfwInit())
    return 1;
    GLFWwindow* window;
    window = glfwCreateWindow(800, 600, "VBO Test", NULL, NULL);
    if (!window)
    {
       glfwTerminate();
       return 1;
    }
    glfwMakeContextCurrent(window);
    if (GLEW_OK != glewInit())
    {
        return 1;
        glfwTerminate();
    }
    //There are normal error handling stuff I do to ensure everything is loaded properly, so the shaders not loading isn't a concern as it'll clearly tell me :)
    GLuint vertexShader = loadShader("shader.vert", GL_VERTEX_SHADER);
    GLuint fragmentShader = loadShader("shader.frag", GL_VERTEX_SHADER);
    GLuint program = createProgram(vertexShader, fragmentShader);
    //Also, the shader files should make everything I draw yellow, and they are not defective
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    glColorPointer(3, GL_FLOAT, 0, 0);
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glUsePorgram(program);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

着色器.vert

void main(void)
{
   gl_Position = gl_Vertex;
}      

着色器.frag

void main()
{
    //Set fragment
    gl_FragColor = vec4( 1.0, 1.0, 0.0, 1.0 );
} 

首先想到的是没有为顶点和颜色指针启用客户端状态。

由于颜色和位置数据重叠,颜色看起来会有点奇怪。我只从glVertexPointer和glEnableClientState(GL_VERTEX_ARRAY)开始,使用单个glColor3f调用。然后添加一个颜色数组。

此外,您将同一数据缓冲到同一VBO两次。VBO只需要在调用glBufferData和gl*Pointer函数之前进行绑定。

就我个人而言,我不喜欢2D数组,尽管它可能在静态声明时工作。OpenGL将线性地从内存中取出数据。使用structs数组可能非常不错。

请参阅以下小示例:http://goanna.cs.rmit.edu.au/~pknowles/SimpleVBO.c