将坐标从3D透视投影映射到OpenGL在C ++中的2D正交投影 [英] Mapping coordinates from 3D perspective projection to 2D orthographic projection in OpenGL in C++

查看:553
本文介绍了将坐标从3D透视投影映射到OpenGL在C ++中的2D正交投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编写了一个简单的openGL程序。该程序在3D透视投影中绘制球体,并尝试在2D正交投影中绘制将球体中心连接到当前光标位置的线。

I have written a simple openGL program in C++. This program draws a sphere in 3D perspective projection and tries to draw a line joining the center of the sphere to the current cursor position in 2D orthographic projection. Now for drawing the line I can't figure out the coordinate of center of the sphere.

这是我的代码:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void passive(int,int);
void reshape(int,int);
void init(void);
void display(void);
void camera(void);

int cursorX,cursorY,width,height;

int main (int argc,char **argv) {
    glutInit (&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
    glutInitWindowSize(1364,689);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Sample");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutPassiveMotionFunc(passive);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}    

void display() {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //  Render 3D content
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,(GLfloat)width/(GLfloat)height,1.0,100.0);    // create 3D perspective projection matrix
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    camera();
    glTranslatef(-6,-2,0);
    glColor3f(1,0,0);
    glutSolidSphere(5,50,50);
    glPopMatrix();

    // Render 2D content
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width,height, 0);                 // create 2D orthographic projection matrix
    glMatrixMode(GL_MODELVIEW);
    glColor3f(1,1,1);
    glBegin(GL_LINES);
        glVertex2f( centreX,centreY );          // coordinate of center of the sphere in orthographic projection
        glVertex2f( cursorX,cursorY );
    glEnd();

    glutSwapBuffers();
}    

void camera(void) {
    glRotatef(0.0,1.0,0.0,0.0);
    glRotatef(0.0,0.0,1.0,0.0);
    glTranslated(0,0,-20);
}    

void init(void) {
    glEnable (GL_DEPTH_TEST);
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_COLOR_MATERIAL);
}    

void reshape(int w, int h) {
    width=w; height=h;
}    

void passive(int x1,int y1) {
    cursorX=x1; cursorY=y1;
}



我可以找出centreX和centreY的值。

I can,t figure out the values for centreX and centreY. Anyway I can get the correct values to draw the line?

推荐答案

您可能有兴趣使用像 gluProject 从您的对象坐标到屏幕上的实际(投影)位置。一旦你有了对象的屏幕坐标,很容易从一个点到另一个点绘制一条线。

You may be interested in using something like gluProject to go from your object coordinates to the actual (projected) position on screen. Once you have the screen coordinates of the object, it's easy to draw a line from one point to another.

在这种情况下,你需要投射中心点的球体。对于更复杂的对象,我发现,投影对象的边界框的所有角,然后采取这些角的屏幕空间位置的范围是有意义的。

In this case, you'll want to project the centre point of the sphere. For more complex objects I've found that it makes sense to project all of the corners of the object's bounding box and then take the extents of the screenspace position of those corners.

显然,为了从屏幕位置(例如,您在窗口中单击)到世界位置,您将需要使用其伴随函数 gluUnProject

Obviously, in order to go from a screen position (say, where you clicked in the window) to a world position, you'll want to use its companion function, gluUnProject.

请注意,从 gluProject 产生的坐标不一定直接对应窗口位置;您可能必须翻转Y坐标。

Note that the coordinates that come out of gluProject do not necessarily correspond directly to the window position; you might have to flip the "Y" coordinate.

查看关于如何解决问题的一些其他想法的GDSE讨论

这篇关于将坐标从3D透视投影映射到OpenGL在C ++中的2D正交投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆