gpt4 book ai didi

c - GluProject 未在 C 中显示准确的坐标

转载 作者:行者123 更新时间:2023-11-30 14:22:25 25 4
gpt4 key购买 nike

我正在尝试从 opengl 坐标(投影在 3D 空间中)找到屏幕坐标,为此我使用了 glproject 调用,我在代码中使用了旋转和平移

在执行一些转换后的某个时刻,我调用 glproject api 来获取特定投影点 P(x,y,z) 的屏幕坐标

glproject(x,y,z,模型矩阵,投影矩阵,视口(viewport),*x_s,*y_s,*z_s);

我能够在 x_s 中正确获取 x 屏幕坐标,但 y 坐标不同

y 中唯一的变化(x 中没有)是当我最初调用 glperspective 来设置 fovy(视野角,以度为单位,在 y 方向)时。 gluPerspective(60.0f, 宽度/高度,0.0001f,1000.0f);

让我重新表述一下这个问题:我在屏幕上创建了一个 3D 点,现在我通过 GLproject 获取该点的 2d(x,y) 坐标,它们与鼠标坐标不同。获得正确的可能解决方案是什么坐标。

这是代码片段

#include<GL/glut.h>     /* Header File For The GLUT Library */
GLint Window; /* The number of our GLUT window */
float tmp_x,tmp_y,tmp_z;
GLfloat w = 1200; /* Window size. Global for use in rotation routine */
GLfloat h = 1200;
GLint prevx, prevy; /* Remember previous x and y positions */
GLfloat xt=1.0,yt=1.0,zt=1.0; /* translate */
int width = 1600;
int height = 1200;

//This function will set windowing transformation
void transform(GLfloat Width , GLfloat Height )
{
glViewport(0,0, (GLfloat)Width, (GLfloat)Height);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, Width/Height,0.0001f,1000.0f);
glTranslatef(0.0, 0.0, -15.0f); /* Centre and away the viewer */
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


GLvoid draw_room()
{
int i;
glPushMatrix();
glShadeModel(GL_SMOOTH);
glLineWidth(1.0);
glPointSize(4.0); /* Add point size, to make it clear */
glBegin(GL_POINTS); /* start drawing the cube.*/
glColor3f(0.0f,0.0f,1.0f); /* Set The Color To Orange*/
glColor3f(1.0f,0.0f,0.0f); /* Set The Color To Orange*/
glVertex3f(3.1f,2.1f,2.1f);
glEnd(); /* Done Drawing The Cube*/
glEnable(GL_DEPTH_TEST);
}
//OpenGl Display callback Function It call init room
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
printf("%f %f %f\n",xt,yt,zt);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(xt,yt, zt);
draw_room();
glPopMatrix();
glutSwapBuffers(); /* Swap buffers */
glFlush();
}

GLvoid Mouse( int b , int s, int xx, int yy)
{

double a1,a2,a3;
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
//gluProject(xt+3.1f,yt+2.1f,zt+2.1f, modelview, projection, viewport, &a1, &a2, &a3);
gluProject(3.1f,2.1f,2.1f, modelview, projection, viewport, &a1, &a2, &a3);

printf("Mouse: %d %d\n",xx,yy); // Both Print are giving different coordinaes.
printf("Unproject %f %f %f\n",a1,a2,a3);
switch (b) {
case GLUT_LEFT_BUTTON: /* only stash away for left mouse */
prevx = xx - w/2;
prevy = h/2 - yy;
break;
case GLUT_MIDDLE_BUTTON:
break;
case GLUT_RIGHT_BUTTON:
break;
}
}

可能的解决方案是什么?

最佳答案

OpenGL使用屏幕左下角作为坐标系原点。窗口系统通常使用左上角作为坐标系原点。您需要处理这种差异。示例:

printf("Mouse: %d %d\n",xx,yy);                
printf("Unproject %f %f %f\n",a1,viewport[1]-a2,a3);

关于c - GluProject 未在 C 中显示准确的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776537/

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