关于OpenGL 鼠标拾取的问题,请大神帮忙

sannuo22 2016-08-17 05:13:31
就是用红宝书第七版第十三章选择那一章,改写一下,先在屏幕上用鼠标随机画了点,然后点右键,用线连接,再点下选取模式,用鼠标去选择那些线,但是怎么也选不中,以下是完整代码,其中Point3D 就是一个包括x.y.z三个坐标点的类。

求指点。困扰了好多天了,实在看不出问题在哪里~快哭了~~~~~~~~~~~

#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;
...全文
1890 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sannuo22 2016-08-17
  • 打赏
  • 举报
回复
困扰了一个星期,今天突然终于搞定,原来 gluOrtho2D (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);要一致,我在reshape那里改了~,送给后来入OpenGL坑的筒子们~

602

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧