Hi all,
i’m a beginner in OpenGL and trying to understand VBOs.
The Problem is, that i can’t find the mistake in the following program. The program uses VBOs and has two variants. The first variant uses glDrawElements and the seconds glDrawArrays.
Both variants work fine on my desktop pc but on my laptop only glDrawArrays works. glDrawElements just produces rubbish.
To compile the DrawElements variant use:
g++ -Wall -o test test.cpp -lglut
To compile the DrawArrays variant use:
g++ -DUSE_DRAW_ARRAYS -Wall -o test test.cpp -lglut
The OS on both computers is Ubuntu 9.10 64Bit.
The desktop pc has a geforce 8800gts and the laptop an intel gm45 gpu.
I would like to hear if someone has any idea what could cause this problem.
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <iostream>
using namespace std;
GLfloat position[] = { 1.0, 1.0, 1.1 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
#define X .525731112119133606
#define Z .850650808352039932
struct Vertex
{
GLfloat x,y,z;
};
Vertex vdata[12] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
GLushort indices[20][3] = {
{0, 4, 1}, {0, 9, 4}, {9, 5, 4}, {4, 5, 8}, {4, 8, 1},
{8, 10, 1}, {8, 3, 10}, {5, 3, 8}, {5, 2, 3}, {2, 7, 3},
{7, 10, 3}, {7, 6, 10}, {7, 11, 6}, {11, 0, 6}, {0, 1, 6},
{6, 1, 10}, {9, 0, 11}, {9, 11, 2}, {9, 2, 5}, {7, 2, 11}
};
GLushort vertexCount = 12;
GLushort indexCount = 60;
GLuint vbo[2];
void
initGL (void)
{
glClearColor (0, 0, 0, 1);
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glLightfv (GL_LIGHT0, GL_POSITION, position);
glLightfv (GL_LIGHT0, GL_SPECULAR, white);
glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glShadeModel (GL_SMOOTH);
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_NORMAL_ARRAY);
#ifdef USE_DRAW_ARRAYS
Vertex *vertices = new Vertex[indexCount];
vertexCount = 0;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 3; j++)
vertices[vertexCount++] = vdata[indices[i][j]];
cout << "using glDrawArrays" << endl;
#else
Vertex *vertices = vdata;
cout << "using glDrawElements" << endl;
#endif
glGenBuffers (2, vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo[0]);
glBufferData (GL_ARRAY_BUFFER, vertexCount*sizeof(Vertex),
vertices, GL_STATIC_DRAW);
glVertexPointer (3, GL_FLOAT, 0, 0);
glNormalPointer (GL_FLOAT, 0, 0);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, indexCount*sizeof(GLushort), indices,
GL_STATIC_DRAW);
}
void
resize (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void
draw (void)
{
static GLfloat angle = 0.0;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -2.0);
if (angle >= 360.0)
angle -= 360.0;
else
angle += 0.01;
glRotatef (angle, 1.0, 1.0, 1.0);
#ifdef USE_DRAW_ARRAYS
glDrawArrays (GL_TRIANGLES, 0, vertexCount);
#else
glDrawElements (GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, 0);
#endif
glutSwapBuffers ();
}
int
main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (682, 384);
glutCreateWindow ("VBO Example");
initGL ();
glutDisplayFunc (draw);
glutIdleFunc (draw);
glutReshapeFunc (resize);
glutMainLoop ();
glDeleteBuffers (2, vbo);
return 0;
}