So, I’m trying to study vbo and just do some examples, but opengl is killing me. I’ve been four hours trying to do this code work, but I dont have a clue why this dont work.
I was trying to draw 4 squares with different colors.
But if I try to draw using GL_POINTS i only see 4 points in the screen, and is supposed to be 9 points. If I try using GL_QUADS it simply don’t draw nothing.
The interesting is that when I change this line:
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
to this:
glDrawElements(GL_POINTS, 64, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
Opengl draw the 9 dots, but only that, GL_QUADS dont work.
Please, someone give me a light on this problem, it’s killing me!!
The complete code is here:
#define GL_GLEXT_PROTOTYPES
#include<GL/glut.h>
#include<iostream>
using namespace std;
#define BUFFER_OFFSET(offset) ((GLfloat*) NULL + offset)
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
void init_2(){
GLuint buffers[NUM_BUFFERS];
GLfloat vertices[][3]= {
{0.0, 0.0, 0.0},
{0.0, 40.0, 0.0},
{0.0, 80.0, 0.0},
{40.0, 0.0, 0.0},
{40.0, 40.0, 0.0},
{40.0, 80.0, 0.0},
{80.0, 0.0, 0.0},
{80.0, 40.0, 0.0},
{80.0, 80.0, 0.0}
};
GLuint indices[][4] = {
{0, 1, 4, 3},
{1, 2, 5, 4},
{3, 4, 7, 6},
{4, 5, 8, 7}
};
glGenBuffers(NUM_BUFFERS, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexPointer(3, 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);
}
void display(void){
glPushMatrix();
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glPopMatrix();
glutSwapBuffers();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
gluOrtho2D((GLdouble) -1.0, (GLdouble) 90.0, (GLdouble) -1.0, (GLdouble) 90.0);
init2();
}
int main(int argv, char** argc) {
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("myCode.cpp");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Here’s the problem:
You declare
GLuintindices, but then tellglDrawElementsthat you’re giving it bytes.GLuintis a 32-bit type, so there are 4 bytes for each. As your index data is very small, each 32-bit value has only data in the bottom byte, leaving the rest as zero.So in the first 16 bytes, which are the first 4
GLuints, there are only 4 discrete values, so you see 4 points. If you try 64 (which is 16 * 4), you will see all 9 points. However these points are mixed in with a lot of zeros, so when you try to render asGL_QUADS, nothing will be drawn.