PDA

View Full Version : Vertex Buffer/Index Buffer help



prash_prash
08-03-2011, 05:57 AM
Hello all,

I am struggling with the usage of Vertex Buffer and index buffer since quite some time now, but haven't been able to figure out what I might be missing, and why it doesn't works for me. I only see a blank window when I execute!

I would really really appreciate if anyone could guide me in the right direction.

My complete code is as follows:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <stdio.h>

GLuint colorTexture;
int cWidth = 0;
int cHeight = 0;
int cimageSize = 0;
GLuint NumIndices = 4;

GLuint VBO;
GLuint IBO;

struct Vector3f{
float x;
float y;
float z;

Vector3f()
{
}

Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};


static void createVertexBuffer(){
Vector3f Vertices[4];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f( 0.0f, -1.0f, 1.0f);
Vertices[2] = Vector3f( 1.0f, -1.0f, 0.0f);
Vertices[3] = Vector3f( 0.0f, 1.0f, 0.0f);
glGenBuffers(1, &amp;VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

static void createIndexBuffer(){
unsigned int Indices[] = {0, 1, 3,
1, 3, 2,
2, 3, 0,
0, 2, 1};

glGenBuffers(1, &amp;IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}


void display(void){
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt (0.25, 0.25, 0.25, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);

glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);

glutSwapBuffers();
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
//glutInitWindowSize (1024, 768);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);


init ();
glutDisplayFunc(display);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
createVertexBuffer();
createIndexBuffer();

glutMainLoop();
return 0;
}

mbentrup
08-04-2011, 02:51 AM
I think you can't use glVertexAttribPointer(0) for the fixed-function pipeline. Use glVertexPointer() instead.

BionicBytes
08-04-2011, 07:34 AM
For definate, you can only use the generic glVertexAttribPointer command when using a shader.