[OpenGL]课后案例08:选择模式拾取程序
2012-11-13 16:45
465 查看
//A.8 选择模式拾取程序 /* demonstrates picking used selection mode */ #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); } 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"); else printf ("blue rectangle\n"); ptr++; } printf ("\n"); } } #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 5×5 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(); } } 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; }
相关文章推荐
- [OpenGL]课后案例07:使用双缓存的程序
- [OpenGL]课后案例09:立方体旋转程序
- [OpenGL]课后案例10:立方体旋转程序
- [OpenGL]课后案例11:用虚拟跟踪球实现立方体旋转程序
- <<opengl编程基础>> 3.13 拾取和选择模式
- [OpenGL]课后案例12:观察者漫游程序
- [OpenGL]课后案例13:球体的细分逼近程序
- [OpenGL]课后案例16:带纹理立方体的旋转程序
- [OpenGL]课后案例02:生成Sierpinski镂垫的递归程序
- [OpenGL]课后案例17:GLSL程序
- [OpenGL]课后案例03:三维Sierpinski镂垫的递归程序
- [OpenGL]课后案例04:Marching-Square程序
- [OpenGL]课后案例19:Bezier曲线程序
- [OpenGL]课后案例05:生成正方形的程序
- [OpenGL]课后案例06:画图程序
- OpenGL-选择与拾取
- OpenGL选择-拾取机制(下)
- OpenGL选择与拾取GL_SELECT 附源码
- 理解OpenGL拾取模式(OpenGL Picking)
- OpenGL 的选择模式