English 中文(简体)
第3D条 进行协调,在开放式GL中筛选协调
原标题:Converting 3D coordinate to screen coordinate in OpenGL
  • 时间:2012-04-21 03:59:39
  •  标签:
  • c++
  • opengl

我正面临一个问题,即把3D协调起来,以进行协调。 我必须把现场中心改为屏幕协调。 我的法典在这里是:

//  OpenGL problem

#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;
double centerX,centerY,centerZ;
//GLfloat modelviewMatrix[16],projectionMatrix[16];
GLdouble modelviewMatrix[16],projectionMatrix[16];
GLint viewport[4];

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();

    glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
    glGetIntegerv(GL_VIEWPORT, viewport);
    // get 3D coordinates based on window coordinates  
    gluProject(-6, -2, 0, modelviewMatrix, projectionMatrix, viewport, &centerX, &centerY, &centerZ);

    // 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( centerX,centerY );          // 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;
}

我的代码是X中心和Y中心坐标不在屏幕上。 我必须从这个领域的中心与花点之间划线。 对该法典的任何修改是为了使其适当发挥作用?

问题回答

通常,我的职责是在3D/2D之间进行协调。 参见它是否为你工作。

void toggle2DMode(bool flag, GLint Width, GLint Height) const
{
        if(flag)
        {
                glMatrixMode(GL_PROJECTION);
                glPushMatrix();
                glLoadIdentity();

                glOrtho(0.0, Width, Height, 0.0, -1.0f, 1.0f);
                glMatrixMode(GL_MODELVIEW);
                glPushMatrix();
                glLoadIdentity();
                glDisable(GL_DEPTH_TEST);
                glEnable(GL_BLEND);
        }
        else
        {
                glMatrixMode(GL_PROJECTION);
                glPopMatrix();
                glMatrixMode(GL_MODELVIEW);
                glPopMatrix();
                glEnable(GL_DEPTH_TEST);
                glDisable(GL_BLEND);
        }
}

我的核查解决职能

void WINDOW_TEXT::calcResolution( GLshort & x , GLshort & y )
{
    GLdouble tx, ty, ttx, tty, z; // target 2d coords (z is  unused )

    GLdouble model_view[16];
    glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

    GLdouble projection[16];
    glGetDoublev(GL_PROJECTION_MATRIX, projection);

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);

    // get window coords based on 3D coordinates
    gluProject( up_right.x, up_right.y, up_right.z,
                model_view, projection, viewport,
                &tx, &ty, &z);

    gluProject( down_left.x, down_left.y, down_left.z,
                model_view, projection, viewport,
                &ttx, &tty, &z);

    x = (GLshort)(tx-ttx);
    y = (GLshort)(tty-ty);
};

“z”被忽视,你获得的纯粹解决办法储存在x, y





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签