Jump to content
  • Advertisement
Sign in to follow this  
astronautusKobayus

OpenGL VBO with element_array does not render a 3dCube, instead a black screen appears

This topic is 2464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started a small test program for Mac with Xcode 4.2 that aims to render a 3d Cube using OpenGL VBO. The fact is that all I get from running the application is a black screen.

What I am basically doing is creating a new Cocoa project with the default AppDelegate.m and AppDelegate.h inheriting from NSObject, I don't modify these files. Then in the MainMenu.xib I add an OPenGLView object from the Object libraries and specify in the attributes tab a class that I create to handle all the OpenGL stuff.

This class is MyOpenGLView (MyOpenGLView.h and MyOpenGLView.m). I attach those two files to a better understanding of what is happening.

What am I doing wrong?

MyOpenGLView.h


//
// MyOpenGLView.h
// testOpenGl
//
// Created by astronautusKobayus on 3/7/12.
//
#import <Cocoa/Cocoa.h>
GLfloat gCubeVertexData[] =
{
0.3f, 0.3f, 0.3f,
-0.3f, 0.3f, 0.3f ,
-0.3f, -0.3f, 0.3f ,
0.3f, -0.3f, 0.3f ,
0.3f, -0.3f, -0.3f ,
-0.3f, -0.3f, -0.3f ,
-0.3f, 0.3f, -0.3f ,
0.3f, 0.3f, -0.3f
};
// color array
GLfloat colors[] = {
1,1,1, 1,1,0, 1,0,0, 1,0,1, // v0-v1-v2-v3
1,1,1, 1,0,1, 0,0,1, 0,1,1, // v0-v3-v4-v5
1,1,1, 0,1,1, 0,1,0, 1,1,0, // v0-v5-v6-v1
1,1,0, 0,1,0, 0,0,0, 1,0,0, // v1-v6-v7-v2
0,0,0, 0,0,1, 1,0,1, 1,0,0, // v7-v4-v3-v2
0,0,1, 0,0,0, 0,1,0, 0,1,1}; // v4-v7-v6-v5
// Define the vertex indices for the cube.
GLuint g_Indices[24] = {
0, 1, 2, 3, // Front face
7, 4, 5, 6, // Back face
6, 5, 2, 1, // Left face
7, 0, 3, 4, // Right face
7, 6, 1, 0, // Top face
3, 2, 5, 4 // Bottom face
};
@interface MyOpenGLView : NSOpenGLView
{
@public
GLuint vertexBufferID; // vertex Buffer that holds sphere vertices array
GLuint g_uiIndicesVBO; // indices buffer ID
}
- (void) drawAnObject;
- (void) drawRect: (NSRect) bounds;
- (void) setCubeVertices;
@end


MyOpenGLView.m

//
// MyOpenGLView.m
// testOpenGl
//
// Created by astronautusKobayus on 3/7/12.
//
#import "MyOpenGLView.h"
#include <OpenGL/gl.h>
@implementation MyOpenGLView
-(void) setCubeVertices
{
glGenBuffers(1, &vertexBufferID);
glGenBuffers( 1, &g_uiIndicesVBO );
// Copy the vertex data to the VBO
glBindBuffer(GL_ARRAY_BUFFER_ARB, vertexBufferID);
glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(gCubeVertexData), &gCubeVertexData);
glBufferSubData(GL_ARRAY_BUFFER_ARB, sizeof(gCubeVertexData), sizeof(gCubeVertexData)+sizeof(colors), &colors);
// Copy the index data to the VBO
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, g_uiIndicesVBO );
glBufferData( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(g_Indices), g_Indices, GL_STATIC_DRAW_ARB );

}

-(void) drawAnObject
{
glColor3f(1.0f, 0.85f, 0.35f);
// We need to enable the client stats for the vertex attributes we want
// to render even if we are not using client-side vertex arrays.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Bind the vertices's VBO
glBindBufferARB( GL_ARRAY_BUFFER_ARB, vertexBufferID );
// before draw, specify vertex and index arrays with their offsets
glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(gCubeVertexData))); //after all vertices there are all colors
// Bind the indices's VBO
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_uiIndicesVBO );
glDrawElements( GL_QUADS, 24, GL_INT, 0 );

// it is good idea to release VBOs with ID 0 after use.
// Once bound with 0, all pointers in gl*Pointer() behave as real
// pointer, so, normal vertex array operations are re-activated
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arrays
}

-(void) drawRect: (NSRect) bounds
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
[self setCubeVertices ];
[self drawAnObject];
glFlush();
}
@end

Share this post


Link to post
Share on other sites
Advertisement
Check the documentation for glDrawElements and look at the valid index types for the index array.

Besides that, don't load your VBO every time you draw it. Load it once, and then refer to it by its handle instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!