Hey all, I am trying desperately to create a cube using VBO. I have the correct vertices that must be used yet when I render it, it only displays one part of the face of the cube (which in this case is a half triangle, since a square is made up of two triangles).

Anyways here is my class. Don't mind the class name though, I've been just practicing with this class and changing the vertices and drawing methods and what not.

#ifdef _WIN32
#include <windows.h>
#else
#include <GL/glx.h>
#endif

#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glext.h"
#include "example.h"

PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBINDBUFFERPROC glBindBuffer = NULL;
PFNGLBUFFERDATAPROC glBufferData = NULL;
GLfloat vertex[];

Example::Example()
{
}

bool Example::init()
{
#ifdef _WIN32
    glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers");
    glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
    glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
#else
    glGenBuffers = (PFNGLGENBUFFERSARBPROC)glXGetProcAddress((const GLubyte*)"glGenBuffers");
    glBindBuffer = (PFNGLBINDBUFFERPROC)glXGetProcAddress((const GLubyte*)"glBindBuffer");
    glBufferData = (PFNGLBUFFERDATAPROC)glXGetProcAddress((const GLubyte*)"glBufferData");
#endif

    if (!glGenBuffers || !glBindBuffer || !glBufferData)
    {
        std::cerr << "VBOs are not supported by your graphics card" << std::endl;
        return false;
    }

    glEnable(GL_DEPTH_TEST);
    glClearColor(0.5f, 0.5f, 0.5f, 0.5f);

	GLfloat vertex[] = {
						1,1,1,    -1,1,1,    -1,-1,1,    1,-1,1,        // v0-v1-v2-v3
						1,1,1,     1,-1,1,    1,-1,-1,   1,1,-1,        // v0-v3-v4-v5
						1,1,1,     1,1,-1,   -1,1,-1,   -1,1,1,         // v0-v5-v6-v1
					   -1,1,1,    -1,1,-1,   -1,-1,-1,  -1,-1,1,		// v1-v6-v7-v2
                       -1,-1,-1,   1,-1,-1,   1,-1,1,   -1,-1,1,        // v7-v4-v3-v2
                        1,-1,-1,  -1,-1,-1,  -1,1,-1,    1,1,-1};	    // v4-v7-v6-v5



	glGenBuffers(1, &m_vertexBuffer); //Generate a buffer for the vertices
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); //Bind the vertex buffer
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, &vertex[0], GL_STATIC_DRAW); //Send the data to OpenGL

    //Return success
    return true;
}

void Example::prepare(float dt)
{
	const float SPEED = 15.0f;
    m_rotationAngle += SPEED * dt;
    if (m_rotationAngle > 360.0f)
    {
        m_rotationAngle -= 360.0f;
    }
}

void Example::render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    
	glTranslatef(0.0f,0.0f,-6.0f);
	
    glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, 0);
	glDrawArrays(GL_QUADS, 0, 24);
    glDisableClientState(GL_VERTEX_ARRAY);


}

void Example::shutdown()
{
}

void Example::onResize(int width, int height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(52.0f, float(width) / float(height), 1.0f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

What am I missing? Is it in the buffer area? There must certainly not be any errors in the rendering part since that part is pretty straight forward.

Oh and if it helps, I used this "code structure" whilst creating a pyramid, which I successfully have done. I just edited some arguments and the vertices array itself to comply for VBO usage.

Thank you to those who'll be helping.

Your problem is that you declare the array of vertices as having 9 coordinates in total, which would only include the first 3 points, i.e. the first triangle.

//This is where that is set:
 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, &vertex[0], GL_STATIC_DRAW);

//This is what it should be:
 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, &vertex[0], GL_STATIC_DRAW);

I see, how stupid of me. I thought that was where the problem existed, I just did not know what to replace the data argument for that function. Thank you very much!

I know that this seems out of context for my thread but, how do I change the perspective of the camera in such a way I can see the figure in its 3d goodness?

Sorry for this noob question, I really am fairly new to openGL and I am just experimenting with code I find in the internet to achieve what I want.

Thanks again!

If you are just starting out, it is a bit of a jump to be writing vertex buffers. Don't you think? Try going through the NEHE tutorials in order (they are quick and easy to follow, and before you know it you'll be rendering flags flopping in the wind on top of a hill with a bunch of trees around and a beach!).

Anyhow, if you want to make it look more 3D, the perspective you have there is fine (normal camera perspective transformation, the usual). What really make your cube look 2D is because you are looking at it directly in the face, so it will just look like a square not a cube. Try adding glRotatef() after glTranslatef(), play with the parameters for a while and you should see more of a 3D cube.

Thank you for the tip! I am indeed studying with no direction whatsoever. Haha.

Oh, and will play around with the glRotatef() function to get to what I want.

I'll just mark this as solved! :D

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.