gpt4 book ai didi

c++ - gluUnProject 试图将鼠标坐标转换为 opengl 坐标的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 03:13:53 25 4
gpt4 key购买 nike

我尝试将鼠标点击坐标转换为 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...
}

关于c++ - gluUnProject 试图将鼠标坐标转换为 opengl 坐标的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17570611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com