PDA

View Full Version : draw cube using VBO



ChuckE
12-12-2013, 09:28 AM
I'm trying to draw a cube using VBO.
I'm using freeglut & glew.
Here is my code


#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>

const int g_width = 800;
const int g_height = 800;

#pragma comment(lib, "glew32.lib")

static const GLsizeiptr PositionSize = 8 * 3 * sizeof(GLfloat);
static const GLfloat PositionData[] =
{
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, -1.0f,
1.0f, 1.0f, -1.0f,
0.0f, 1.0f, -1.0f
};

static const GLsizeiptr ColorSize = 8 * 3 * sizeof(GLubyte);
static const GLubyte ColorData[] =
{
255, 0, 0,
255, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
255, 0, 0,
255, 255, 0,
0, 255, 0
};

static const GLsizei VertexCount = 8;

GLuint vertexId;
GLuint colorId;

void initBuffers()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glShadeModel(GL_SMOOTH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, 640.0f / 480.0f, 0.5f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glGenBuffers(1, &vertexId);
glBindBuffer(GL_ARRAY_BUFFER, vertexId);
glBufferData(GL_ARRAY_BUFFER, PositionSize, &PositionData[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &colorId);
glBindBuffer(GL_ARRAY_BUFFER, vertexId);
glBufferData(GL_ARRAY_BUFFER, ColorSize , &ColorData[0], GL_STATIC_DRAW);

}

void freeBuffers()
{
glDeleteBuffers(1, &vertexId);
glDeleteBuffers(1, &colorId);
}

void drawBuffers()
{

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, colorId);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, vertexId);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, VertexCount);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);


glEnd();
}

void draw()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glEnable(GL_COLOR_MATERIAL);
drawBuffers();
glDisable(GL_COLOR_MATERIAL);

glFlush();
glutSwapBuffers();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(g_width, g_height);
glutCreateWindow("VBO ASSIMP STARTER");

glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initilaize GLEW\n");
return -1;
}

initBuffers();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(draw);
glutMainLoop();
freeBuffers();

return 0;
}


The problem is that window is black and it doesn't show any cube.
What is the problem?

CarstenT
12-13-2013, 12:52 PM
hi ChuckE
Don't take advise from a newbie like me too serious! The opengl seems to be parted in old and new ways (pre, post 3.0 - fixedFunction pipeline & shaders + buffers). You should investigate the boundary between the two. I can use modern buffers and glut but not paint anything without involving shaders. You seem to have omitted the shaders. If you are all new it might pose a bit of an obstacle ;o/ But it's no small feat to be where you are now. Doing the shadeing-thing is a confined part and can be worked on separately.

chbaker0
12-13-2013, 07:38 PM
Extending on what CarstenT said, this (http://www.arcsynthesis.org/gltut/) is a good tutorial for modern OpenGL (v. 3.3+): it teaches you plenty of theory behind graphics (vectors, matrices, transformations, etc), a necessity since the matrix stacks and such were removed, at a very reasonable pace. It has a quite intuitive discussion of shaders and such.

tonyo_au
12-13-2013, 11:40 PM
This is good also http://ogldev.atspace.co.uk/index.html