gluUnProject试图将鼠标坐标转换为opengl坐标的奇怪行为
Strange behaviour of gluUnProject trying to convert mouse coords to opengl coords
我试图将click中的鼠标坐标转换为opengl坐标。事实上,它似乎有效,但当我取消对我刚刚为测试而写的cout行的注释时,它会将我的vars设置为非数字-nan
。这是怎么发生的?我该怎么修?
//global:
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
//mouse coords to gl coords
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_UP){ //on release left mouse button
std::cout << x << " * "<< y << std::endl;
GetOGLPos(x, y);
std::cout
<< mouseOgl[0] << " # "
<< mouseOgl[1] << " # "
<< mouseOgl[2] << " # "
<< std::endl;
glutPostRedisplay();
}
break;
}
}
此处的完整代码:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
const GLint nNumPoints = 5;
GLfloat ctrlpoints[5][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}, {6.0, 2.0, 0.0}};
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
void GetOGLPos(int x, int y);
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, // Type of data generated
0.0f, // Lower u range
1.0f, // Upper u range
3, // Distance between points in the data
nNumPoints, // number of control points
&ctrlpoints[0][0]); // array of control points
// Enable the evaluator
glEnable(GL_MAP1_VERTEX_3);
glEnable(GL_DEPTH);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
//Kontrollpunkte:
glPointSize(5.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < nNumPoints; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i < nNumPoints; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//keep aspect ratio:
if (w <= h)
glOrtho(-10.0, 10.0, -10.0*(GLfloat)h/(GLfloat)w,
10.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-10.0*(GLfloat)w/(GLfloat)h,
10.0*(GLfloat)w/(GLfloat)h, -10.0, 10.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
//mouse coords to gl coords
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_UP){ //on release left mouse button
std::cout << x << " * "<< y << std::endl;
GetOGLPos(x, y);
std::cout
<< mouseOgl[0] << " # "
<< mouseOgl[1] << " # "
<< mouseOgl[2] << " # "
<< std::endl;
glutPostRedisplay();
}
break;
}
}
// detailed information:
// http://nehe.gamedev.net/article/using_gluunproject/16013/
void GetOGLPos(int x, int y)
{
//init vars:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
//get gl specs
glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get Modelmatrix
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get projection matrix
glGetIntegerv( GL_VIEWPORT, viewport ); //get viewport values
//calculate the gl mouseposition
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
/*
following line needed to run the program propper?!?!
*/
std::cout << "positions:" << posX << " | " << posY << " | " << posZ << std::endl;
mouseOgl[0] = posX;
mouseOgl[1] = posY;
mouseOgl[2] = posZ;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouse);
glutMainLoop();
return 0;
}
鼠标回调可以在任何时候调用,因此当您处于回调中时,您不确定渲染代码的正确状态。。。
我认为您应该在这个回调中标记鼠标被按下(将其保存到某个变量wasMousePressed = true;
),然后在OnRender函数中检查鼠标点击情况。这样它就会和opengl同步。然后检查您的cout
代码是否正常工作。
onMouse() {
if (...)
mousePressed = true;
else
mousePressed = false;
}
onRender() {
clearBuffer();
setupCameraAndProjection();
if (mousePressed)
checkOGLState();
render...
}
相关文章:
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- 如何使用OpenGL知道所有顶点的当前坐标
- 在 openGL 中,模型坐标应该在我的 CPU 上计算还是在 GPU 上使用 OpenGL 调用计算?
- 检查刻度中的坐标不起作用(OpenGL)
- 将 OpenGL 坐标系统转换为自定义并返回
- OpenGL屏幕坐标到世界坐标
- 如何将鼠标坐标正确转换为 opengl 坐标?
- 在 OpenGL 3.3 中使用整数坐标绘制点?
- 如何在OpenGL中绘制一组坐标?
- OpenGL 纹理坐标不起作用
- OpenGL从窗口点到3D坐标
- 给定两组 xyz 坐标和一个 sag (a) 的值,我可以在 OpenGL 中的这些点之间绘制一个悬链线
- Opengl 土壤纹理坐标无法识别
- C++OpenGL坐标阵列来绘制线条/边界和填充矩形
- 如何在 opengl 中放置坐标> 100.0f?
- 在OpenGL 2.1中,即使在2D空间(具有2D纹理),也可以安全使用3D TEX坐标
- OpenGL获取离当前鼠标位置最近的世界三维点的三维坐标
- 在球形坐标(OpenGL、C++、GLUT)之间跨步
- 传递纹理坐标(OpenGL)时,不会显示任何内容