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?

Recommended Answers

All 2 Replies

You made a typo on line 61:

glBindBuffer(GL_ARRAY_BUFFER, vertexId);

It should be:

glBindBuffer(GL_ARRAY_BUFFER, colorId);

Also, I think you should be using GL_TRIANGLE_STRIP instead of GL_TRIANGLES. See explanation or this page. You simply cannot have 8 vertices representing triangles (2 triangles = 6 points, 3 triangles = 9 points). You might also try the deprecated GL_QUADS instead (which, in this case, would draw two rectangles, one in front (z=1), one in back (z=-1)).

To draw the complete cube, using your 8 points (8 corners of a cube), you need to use the glDrawElements to provide a array of indices to order and reuse the points to make the complete mesh. For example, this could work:

static const GLsizeiptr PointsCount = 17;
static const GLubyte PointsData[] =
{
    0, 2, 1,
    7,
    4,
    6,
    5,
    3,
    0,
    2,
    3,
    7,
    6,
    5,
    4,
    0,
    1
};


//... everything else the same, until:

  glDrawElements(GL_TRIANGLE_STRIP, PointsCount, GL_UNSIGNED_BYTE, PointsData);

If I have done it correctly (ordering the vertices of the triangle-strip), then it should give you a cube.

Finally, in your draw function, you might want to add a translation, otherwise, you will have trouble seeing the cube because the camera is smack dead in the center of it. You should move back (using glTranslatef) a little bit before drawing the scene:

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0.0, 0.0, -5.0);

    //...

Oh! I see.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.