I’m having a strange issue where I draw a texture and then draw a line afterwards in red. The texture draws fine, and so does the line, but if I redraw again, the texture draws with a shade of red.
(I’m working in Cocoa and am new to OpenGL, although I wrote a small cross-platform Tetris game back in 06 but have since forgotten everything. Currently trying to learn a little more by porting over an old 2D game.)
Here’s the code:
- (void)drawRect:(NSRect)rect
{
[[self openGLContext] makeCurrentContext];
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if (!bgImg)
bgImg = [[GLImage alloc] initWithImageNamed:@"background.png"];
[bgImg drawAtPoint:NSZeroPoint];
glBegin(GL_LINES);
glColor3d(1.0, 0.0, 0.0);
glLineWidth(5.0);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(mouseLoc.x, mouseLoc.y, 0.0f);
glEnd();
[[self openGLContext] flushBuffer];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
[self setNeedsDisplay:YES];
}
GLImage is just a separate class that handles loading and drawing an image.