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
[size=4]
//
// MyOpenGLView.h
// testOpenGl
//
// Created by astronautusKobayus on 3/7/12.
//[/size]
#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;
@endMyOpenGLView.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






