I am trying to rework the accepted answer of this question to work in a Cocoa application.
I have made a class called OpenGLView that is a subclass of NSOpenGLView and in my Xib file I have an OpenGL view with a custom class of OpenGLView.
And I have made my OpenGLView class implementation the following:
#import "OpenGLView.h"
#include <OpenGL/OpenGL.h>
#include <OpenCL/OpenCL.h>
@implementation OpenGLView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glEnableClientState(GL_VERTEX_ARRAY);
float data[][2] = {{50,50},{100,50},{75,100}};
glGenBuffers(1,&ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);
glDrawArrays(GL_TRIANGLES,0,3);
glFlush();
}
@end
Basically, (with the exception of glewInit()
) I have copied the init method and copied and pasted the display()
functions contents in my -(void)drawRect
method. When I run the program I get no errors or warning, no runtime errors, but I get only a black screen with no triangle. But, there is a white screen, as the glClear(GL_COLOR_BUFFER_BIT)
should do.