All my OpenGL work was previewsly done on Carbon, Full Screen with GLUT. But now, I want to create everything inside a Cocoa NSOpenGLView. I create the project, subclass OpenGLView, and write my code. here is my code:
However, I don't want to work into this coordinate space. That way, in order to draw something, I will have to use numbers less than 1.0. So, I try to Change the view to have other coordinates. Here is my new code.
I get what I wanted... but for only half a second! The image of a triangle is displayed for 0.1 secs, and then disappears!
EDIT: Here is something a bit strange (at least to me). I changed the code so that the drawing function would happen only ONCE in the program (of course that way, any redraw to the image view will render it useless). However, the image is displayed correctly:
So, something happens when the image is drawn more than once...
Can anyone help me?
Code:
static void drawAnObject ()
{
glColor3f(1.0f, 0.85f, 0.35f);
glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 0.6, 0.0);
glVertex3f( -0.1, -0.2, 0.0);
glVertex3f( 0.1, -0.2 ,0.0);
}
glEnd();
}
@implementation MyOpenGLView
-(void)awakeFromNib
{
}
- (void)drawRect:(NSRect)rect
{
NSLog(@"%f", [self bounds].size.width);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
drawAnObject();
glFlush();
}
@end
However, I don't want to work into this coordinate space. That way, in order to draw something, I will have to use numbers less than 1.0. So, I try to Change the view to have other coordinates. Here is my new code.
Code:
static void drawAnObject ()
{
glColor3f(1.0f, 0.85f, 0.35f);
glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 60, 0.0);
glVertex3f( -10, -20, 0.0);
glVertex3f( 10, -20 ,0.0);
}
glEnd();
}
@implementation MyOpenGLView
- (void)drawRect:(NSRect)rect
{
//glViewport(-100, 100, -100, 100);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(-100, 100, -100, 100, -10, 10);
glClearColor(0, 0, 0, 0);
drawAnObject();
glFlush();
}
@end
I get what I wanted... but for only half a second! The image of a triangle is displayed for 0.1 secs, and then disappears!
EDIT: Here is something a bit strange (at least to me). I changed the code so that the drawing function would happen only ONCE in the program (of course that way, any redraw to the image view will render it useless). However, the image is displayed correctly:
Code:
int test = 1;
static void drawAnObject ()
{
NSLog(@"drawn object!");
glColor3f(1.0f, 0.85f, 0.35f);
glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 60, 0.0);
glVertex3f( -10, -20, 0.0);
glVertex3f( 10, -20 ,0.0);
}
glEnd();
}
@implementation MyOpenGLView
-(void)awakeFromNib
{
}
- (void)drawRect:(NSRect)rect
{
NSLog(@"redraw");
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(-100, 100, -100, 100, -1, 1);
drawAnObject();
if (test == 1) {
[[self openGLContext]flushBuffer];
test = 0;
}
}
@end
So, something happens when the image is drawn more than once...
Can anyone help me?