OpenGL VBO程序给出了空白屏幕

时间:2012-06-15 23:18:17

标签: opengl opengl-3

我一直在尝试将一些代码转换为现代OpenGL。我已经到了我没有得到任何OpenGL错误的地步,但是当我尝试绘制一个对象时没有任何显示。这是我的代码(减去上下文创建和错误检查):

//Compile shaders and create/link program
//I very highly doubt the problem's here (all my tests say it worked fine),
//so I'm leaving this out for now,  but I'll dig it out of my classes if
//there's no obvious problem with the VBO code.

//Create VAO, VBO

unsigned vaoId, vboId;
int positionAttributeLocation;
float vertices[] = {...vertex data here...};
unsigned indices[] = {...index data here...};

positionAttributeLocation = glGetAttribLocation(programId, "position");

glGenVertexArrays(1, &vaoId);
glGenBuffers(1, &vboId);

glBindVertexArray(vaoId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, null);
glEnableVertexAttribArray(positionAttributeLocation);

//Create index buffer
unsigned indexId;
glGenBuffers(1, &indexId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glUseProgram(programId);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(unsigned int), GL_UNSIGNED_INT, null);

不完全是SSCCE,但我认为这可能是导致问题的所有代码,并且它几乎是独立的。

3 个答案:

答案 0 :(得分:0)

glUseProgram()来电之前尝试glGetAttribLocation()/glEnableVertexAttribArray()

答案 1 :(得分:0)

您的问题很可能在于您的cg程序和模型视图空间。

在gldrawarrays之前将cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);添加到您的程序中,并在您的cg文件中添加OUT.HPos = mul(ModelViewProj, IN.position);

另外在你的initcg部分添加modelViewMatrix作为cgparameter。

我从cgtoolkit中的基本opengl示例中解决了这个问题,我的渲染函数与你的渲染函数非常相似,现在可以解决同样的问题。

答案 2 :(得分:0)

我明白了。在我的一些重构中,我忘了正确设置宽度和高度变量,创建一个0乘0的视口。糟糕...

相关问题