急!!!求助有关openGL拾取的问题
需要实现的功能:右键点击任一个正方形,此正方形消失。中键点击任一个正方形,此正方形变色。点红色红色变化,点哪个哪个发生变化。就是要根据拾取的结果来变化。 现在的程序是:单击右键,绿色正方形消失。单击中键,绿色正方形变色。
希望有大神帮忙解决问题,修改程序。
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void drawObjects(GLenum mode)
{
if(mode == GL_SELECT) glLoadName(1);
glColor3f(1.0, 0.0, 0.0);
glRectf(-0.5, -0.5, 1.0, 1.0);
if(mode == GL_SELECT) glLoadName(2);
glColor3f(0.0, 0.0, 1.0);
glRectf(-1.0, -1.0, 0.5, 0.5);
if(mode == GL_SELECT) glLoadName(2);
glColor3f(0.0, 1.0, 0.0);
glRectf(-1.5, -1.5, 0.0, 0.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawObjects(GL_RENDER);
glFlush();
}
/* processHits prints out the contents of the
* selection array.
*/
void processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
ptr+=3;
for (j = 0; j < names; j++) { /* for each name */
if(*ptr==1) printf ("red rectangle\n");
if(*ptr==2) printf ("blue rectangle\n");
if(*ptr==3) printf ("green rectangle\n");
ptr++;
}
printf ("\n");
}
}
void processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
ptr+=3;
for (j = 0; j < names; j++) { /* for each name */
if(*ptr==1)
#define SIZE 512
void mouse(int button, int state, int x, int y)
{
GLuint selectBuf[SIZE];
GLint hits;
GLint viewport[4];
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (SIZE, selectBuf); //设置选择缓冲区
glRenderMode(GL_SELECT); //激活选择模式
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix (); //将当前的投影矩阵复制一个并压入堆栈
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluOrtho2D (-2.0, 2.0, -2.0, 2.0);
drawObjects(GL_SELECT); //用选择模式绘制图形
//恢复投影变换
glMatrixMode (GL_PROJECTION);
glPopMatrix (); //将投影矩阵堆栈中的栈顶元素删除
glFlush ();
//获得选择集并输出
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf); //输出选择结果
glutPostRedisplay();
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRectf(-0.5, -0.5, 1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glRectf(-1.0, -1.0, 0.5, 0.5);
glFlush();
1200638679670
}
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRectf(-0.5, -0.5, 1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glRectf(-1.0, -1.0, 0.5, 0.5);
glColor3f(1.0, 0.0, 1.0);
glRectf(-1.5, -1.5, 0.0, 0.0);
glFlush();
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/* Main Loop */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutMouseFunc (mouse);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}