PollyPocket4eva
02-24-2010, 08:13 AM
Hi,
I am trying to do something like the process described here:
http://www.lighthouse3d.com/opengl/picking/index.php?color1
In brief, I want to use double buffering in my opengl view so that the user sees every triangle shaded white (the front buffer) and in the back buffer each triangle is shaded with its own unique color. When the mouse is pressed, we read from the back buffer. But otherwise we always display the front, which is the white triangles.
I think I have the right method for reading from the back buffer, I'm uncertain of how to generate that buffer in the first place. Meaning I'm not sure how to go about drawing the scene twice, once for each buffer.
Here is what I have. I know it does not work. But not sure where and when to call drawCodedScene.
Any help would be greatly appreciated.
Thanks!
- (void) drawRect:(NSRect)rect {
[self resizeGL];
[self updateModelView];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self drawScene];
[[self openGLContext] flushBuffer];
}
-(void) drawScene {
for(int i = 0; i < 5; i++) {
glColor3f(255.0,255.0,255.0); // NOT color coded!
glBegin(GL_TRIANGLES);
glVertex3f(vertA[i].x,vertA[i].y,vertA[i].z);
glVertex3f(vertB[i].x,vertB[i].y,vertB[i].z);
glVertex3f(vertC[i].x,vertC[i].y,vertC[i].z);
glEnd();
}
[self setNeedsDisplay: YES];
}
-(void) drawCodedScene {
for(int i = 0; i < 5; i++) {
glColor3f(i/255.0,i/255.0,i/255.0); // Color coded!
glBegin(GL_TRIANGLES);
glVertex3f(vertA[i].x,vertA[i].y,vertA[i].z);
glVertex3f(vertB[i].x,vertB[i].y,vertB[i].z);
glVertex3f(vertC[i].x,vertC[i].y,vertC[i].z);
glEnd();
}
[self setNeedsDisplay: YES];
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil];
location.y = camera.viewHeight - location.y;
[self pixelValueForX:location.x Y:location.y];
}
- (void)pixelValueForX:(float)myX Y:(float)myY {
[self drawCodedScene];
GLint viewport[4];
GLubyte pixelVal[3];
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(myX, (float)viewport[3]-myY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixelVal);
NSLog(@"%i %i %i",pixelVal[0],pixelVal[1],pixelVal[2]);
}
I am trying to do something like the process described here:
http://www.lighthouse3d.com/opengl/picking/index.php?color1
In brief, I want to use double buffering in my opengl view so that the user sees every triangle shaded white (the front buffer) and in the back buffer each triangle is shaded with its own unique color. When the mouse is pressed, we read from the back buffer. But otherwise we always display the front, which is the white triangles.
I think I have the right method for reading from the back buffer, I'm uncertain of how to generate that buffer in the first place. Meaning I'm not sure how to go about drawing the scene twice, once for each buffer.
Here is what I have. I know it does not work. But not sure where and when to call drawCodedScene.
Any help would be greatly appreciated.
Thanks!
- (void) drawRect:(NSRect)rect {
[self resizeGL];
[self updateModelView];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self drawScene];
[[self openGLContext] flushBuffer];
}
-(void) drawScene {
for(int i = 0; i < 5; i++) {
glColor3f(255.0,255.0,255.0); // NOT color coded!
glBegin(GL_TRIANGLES);
glVertex3f(vertA[i].x,vertA[i].y,vertA[i].z);
glVertex3f(vertB[i].x,vertB[i].y,vertB[i].z);
glVertex3f(vertC[i].x,vertC[i].y,vertC[i].z);
glEnd();
}
[self setNeedsDisplay: YES];
}
-(void) drawCodedScene {
for(int i = 0; i < 5; i++) {
glColor3f(i/255.0,i/255.0,i/255.0); // Color coded!
glBegin(GL_TRIANGLES);
glVertex3f(vertA[i].x,vertA[i].y,vertA[i].z);
glVertex3f(vertB[i].x,vertB[i].y,vertB[i].z);
glVertex3f(vertC[i].x,vertC[i].y,vertC[i].z);
glEnd();
}
[self setNeedsDisplay: YES];
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil];
location.y = camera.viewHeight - location.y;
[self pixelValueForX:location.x Y:location.y];
}
- (void)pixelValueForX:(float)myX Y:(float)myY {
[self drawCodedScene];
GLint viewport[4];
GLubyte pixelVal[3];
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(myX, (float)viewport[3]-myY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixelVal);
NSLog(@"%i %i %i",pixelVal[0],pixelVal[1],pixelVal[2]);
}