Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: draw cube using VBO

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Dec 2013
    Posts
    1

    draw cube using VBO

    I'm trying to draw a cube using VBO.
    I'm using freeglut & glew.
    Here is my code
    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?

  2. #2
    Intern Contributor
    Join Date
    Jul 2013
    Posts
    62
    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.
    Last edited by CarstenT; 12-13-2013 at 01:02 PM.

  3. #3
    Intern Newbie
    Join Date
    Dec 2013
    Posts
    45
    Extending on what CarstenT said, this 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.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,105

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •