关于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;
...全文
2008 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sannuo22 2016-08-17
  • 打赏
  • 举报
回复
困扰了一个星期,今天突然终于搞定,原来 gluOrtho2D (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);要一致,我在reshape那里改了~,送给后来入OpenGL坑的筒子们~
内容概要:本文系统研究了构网型变流器的正负序阻抗解耦特性及其在弱电网环境下的稳定性表现,重点依托Matlab/Simulink仿真平台,构建了详细的阻抗数学模型,设计了解耦控制策略,并采用小信号扫频法进行频域辨识与稳定性验证。研究深入探讨了构网型变流器与传统跟网型逆变器在正负序阻抗特性上的本质差异,结合虚拟同步发电机(VSG)等先进控制技术,分析其在抑制宽频带振荡、削弱锁相环动态耦合等方面的优越性。文中不仅提供了完整的仿真模型与MATLAB代码实现,还整合了光伏、风电、储能、微电网等多类新能源系统的阻抗建模与稳定性分析资源,形成了一套面向新型电力系统稳定性的综合性技术资料体系,具有较强的科研复现与工程参考价值。; 适合人群:面向具备电力电子、电力系统自动化、新能源并网等专业背景的研究生、高校教师及工程技术人员,特别适用于从事阻抗建模、小干扰稳定性分析、宽频振荡机理研究以及撰写高水平学术论文的科研工作者。; 使用场景及目标:①掌握构网型变流器正负序阻抗建模与扫频辨识的仿真方法;②深入理解VSG等构网型控制在弱电网中提升稳定性的内在机理;③复现顶刊论文中的阻抗分析流程与稳定性判据应用;④利用提供的成熟模型与代码加速科研进程,支撑课题研究与学术成果产出。; 阅读建议:建议结合文中提供的Simulink模型与MATLAB代码,按照“理论建模—仿真搭建—扫频激励—频响提取—Nyquist判据分析”的完整流程进行实践操作,重点关注扫频信号的注入方式、频率范围设置及阻抗曲线的物理意义解读,并参考博士论文复现案例深化对复杂动态耦合问题的理解。

607

社区成员

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

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