Opengl鼠标拾取问题。 [英] Opengl mouse pickup question.
本文介绍了Opengl鼠标拾取问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是完整的代码,Point3D是一个只有三个值的类:x,y,z代表协调。
首先要画一些点用鼠标在屏幕上。然后用线连接它们。然后我只想拿起这些线路
但它不起作用。
有人能帮帮我吗?我已经挣扎了好几天了。我筋疲力尽!!!
Following is the complete code, the Point3D is a class only with three value:x,y,z represent the coordination.
First thing is draw some points on the screen with the mouse. Then connect them with line. Then I just want to pick up these lines
But it does not work.
Can somebody help me Please? I have struggled for several days. I am exhausted!!!
#include "StdAfx.h"
#include <Windows.h>
#include <gl/GL.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<time.h>
#include<iostream>
#include<math.h>
#include<vector>
#include "Point3D.h"
using namespace std;
#define BUFSIZE 512
int screenWidth=340;
int screenHeight=280;
vector<Point3D> P;
static int pickUpModel=0; //1: mouse pickup work 0:close the function of mouse pick up
void drawSpere(float x, float y,float z);
void drawLine2D(GLenum);
void pickUp(int x, int y);
void processHits (GLint hits, GLuint buffer[]);
void myInit(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f (0.0, 1.0, 1.0);
}
void myReshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
// gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
if(w<=h)
glOrtho(0,screenWidth*2, 0, screenHeight*2*(GLfloat)h/w,-screenWidth,screenWidth);
else
glOrtho(0,screenWidth*2*(GLfloat)w/h,0,screenHeight*2,-screenWidth,screenWidth);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void myMouse(int button,int state,int x,int y)
{
Point3D X;
if((state==GLUT_DOWN)&&(button==GLUT_LEFT_BUTTON)&&(pickUpModel==0))
{
X.x=x;
X.y=screenHeight*2-y;
X.z=0;
P.push_back(X);
pointNum++;
for(int i=0;i<pointNum;i++)
drawSpere(P[i].x, P[i].y, P[i].z);
cout<<x<<" "<<y<<endl;
}
if((state==GLUT_DOWN)&&(button==GLUT_LEFT_BUTTON)&&(pickUpModel==1))
{
pickUp(x,y);
}
// glutSwapBuffers();
glFlush();
}
void myMenu(int value){
if(value==1)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
P.clear();
pointNum=0;
idx=0;
glFlush();
}
if(value==2)
{
drawLine2D(GL_RENDER);
}
if(value==3)
{
}
if(value==4)
pickUpModel=1;
if(value==5)
pickUpModel=0;
}
void drawSpere(float x, float y,float z)
{
glPushMatrix();
glTranslatef (x, y, 0.0);
glRotatef (5, 0.0, 1.0, 0.0);
glutWireSphere(5, 100, 80); /* draw smaller planet */
glPopMatrix();
}
void drawLine2D(GLenum mode)
{
glLineWidth(5);
for(int i=0;i<pointNum;i++){
if(mode==GL_SELECT)
glPushName(i+1);
glBegin(GL_LINE_LOOP);
glVertex2f(P[i%pointNum].x, P[i%pointNum].y);
glVertex2f(P[(i+1)%pointNum].x, P[(i+1)%pointNum].y);
glEnd();
}
glFlush();
}
void pickUp(int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) 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 (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);
drawLine2D(GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode(GL_RENDER);
processHits (hits, selectBuf);
}
void processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
printf (" number of names for hit = %d\n", names); ptr++;
printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" the name is ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr); ptr++;
}
printf ("\n");
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
// glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitDisplayMode (GLUT_SINGLE| GLUT_RGB);
glutInitWindowSize (screenWidth*2, screenHeight*2);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
myInit();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMouseFunc(myMouse);
int id=glutCreateMenu(myMenu);
glutAddMenuEntry("Clear Screen",1);
glutAddMenuEntry("InitialDraw",2);
glutAddMenuEntry("iter",3);
glutAddMenuEntry("pickUpOpen",4);
glutAddMenuEntry("pickUpClose",5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
我的尝试:
我只是复制OpenGL红皮书(第7章)第13章代码并修改以选择行,但它不起作用!
What I have tried:
I just copy the OpenGL redbook(7th) chapter 13 code and modify to choose line,but it just doesn't work!
推荐答案
I知道是什么问题!
gluOrtho2D(0.0,screenWidth * 2.0,0.0,screenHeight * 2.0);应该在整个代码中保持相同,而我在重塑函数中更改它。当我统一代码时,它可以工作!
I know what is the matter!
The gluOrtho2D (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);should keep the same in the whole code, while I change it in the reshape funcion. When I uniform the code, it works!
这篇关于Opengl鼠标拾取问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文