I'm trying to create a simple colored cube using vbo(well it actually a rectangle with z = 0).
I am using glew & freeglut.
Here 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[] =
{
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
1, -1, -1,
-1, -1, -1,
-1, 1, -1,
1, 1, -1
};
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, sizeof(GLfloat) * 3 * VertexCount, &PositionData[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &colorId);
glBindBuffer(GL_ARRAY_BUFFER, vertexId);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLubyte) * 3 * VertexCount, &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);
}
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;
}
But it shows only black screen. What is the problem?