| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 273 人关注过本帖
标题:急!!!求助有关openGL拾取的问题
取消只看楼主 加入收藏
ckjj688
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
急!!!求助有关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;
}        
搜索更多相关主题的帖子: void include 正方形 
2012-11-23 17:43
快速回复:急!!!求助有关openGL拾取的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015958 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved