VBO:数组未绘制 [英] VBO: Array not drawn
问题描述
我关注本指南,我想尝试将四边形绘制到屏幕。我也看到源代码,它是相同的,它应该工作,但在我的情况下没有什么显示在屏幕上。我使用OpenGL 2.0与顶点着色器,只是将颜色设置为红色的四边形应该在屏幕上可见。
I'm following this guide and I'm trying to draw a quad to the screen. I also saw the source code, it's the same and it should work, but in my case nothing is displayed on the screen. I'm using OpenGL 2.0 with a vertex shader that just sets the color to be red in a way that the quad should be visible on the screen.
在callig glutMainLoop之前,我生成顶点缓冲对象:
Before callig glutMainLoop I generate the vertex buffer object:
#include <GL/glut.h>
#include <GL/glew.h>
vector<GLfloat> quad;
GLuint buffer;
void init()
{
// This routine gets called before glutMainLoop(), I omitted all the code
// that has to do with shaders, since it's correct.
glewInit();
quad= vector<GLfloat>{-1,-1,0, 1,-1,0, 1,1,0, -1,1,0};
glGenBuffers(1,&buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*12,quad.data(),GL_STATIC_DRAW);
}
这是我的呈现程序:
void display()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,buffer);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
// I also tried passing quad.data() as last argument, but nothing to do.
glDrawArrays(GL_QUADS,0,12);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
问题是没有什么东西画到屏幕上,我只是看到一个黑色窗口。
The problem is that nothing is drawn to the screen, I just see a black window. The quad should be red because I set the red color in the vertex shader.
推荐答案
我失去了glEnableClientState,像这样:
I was missing glEnableClientState like this:
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS,0,12);
glDisableClientState(GL_VERTEX_ARRAY);
这篇关于VBO:数组未绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!