最近,我正在学习OpenGL编程。但是,当我想使用VAO实现样本时。它不像红皮书中所说的那样有效。
下面是我的代码。我创建了两个对象(一个四边形和一个三角形)。然后为每个对象使用一个vAO。但是在display()函数中,我无法通过调用glBindVertexArray()和glDrawElements()来绘制特定对象。
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <GL/glut.h>
#include <stdio.h>
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))
static GLfloat spin = 0.0;
GLfloat vertices[][2] =
{
{-25.0, -25.0},
{-25.0, 25.0},
{25.0, -25.0},
{25.0, 25.0}
};
GLfloat vertices2[][2] =
{
{-25.0, -25.0},
{-25.0, 25.0},
{25.0, -25.0}
};
GLubyte indices[4] = {1,0, 2, 3};
GLubyte indices2[3] = {1, 0, 2};
enum { qua, tri};
GLuint VAO[2];
GLenum PrimType[2];
GLsizei NumElements[2];
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
//just draw the quad for testing
glBindVertexArray(VAO[qua]);
glDrawElements(PrimType[qua], NumElements[qua], GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
display();
}
void myinit(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glShadeModel(GL_FLAT);
GLuint buffers[NUM_BUFFERS];
GLuint buffers2[NUM_BUFFERS];
//quad
glGenVertexArrays(1, &VAO[qua]);
{
glBindVertexArray(VAO[qua]);
glEnableVertexAttribArray(0);
glGenBuffers(NUM_BUFFERS, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));;
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
PrimType[qua] = GL_QUADS;
NumElements[qua] = sizeof(indices) / sizeof(indices[0]);
glBindVertexArray(0);
}
//triangle
glGenVertexArrays(1, &VAO[tri]);
{
glBindVertexArray(VAO[tri]);
glEnableVertexAttribArray(0);
glGenBuffers(NUM_BUFFERS, buffers2);
glBindBuffer(GL_ARRAY_BUFFER, buffers2[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers2[INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
PrimType[tri] = GL_TRIANGLES;
NumElements[tri] = sizeof(indices2) / sizeof(indices2[0]);
}
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
else
glOrtho (-50.0*(GLfloat)w/(GLfloat)h,
50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(500, 500);
glutInitWindowSize(300, 300);
glutCreateWindow("double buffer");
myinit();
glutIdleFunc(spinDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
}