设置正交投影矩阵?

时间:2010-11-05 05:53:44

标签: opengl coordinates orthographic

我无法设置尺寸为4x2的正交投影矩阵,其中(0,0)位于中心,( - 2,-1)位于左下角,(2,1)位于顶部右上角。

我使用glutInitWindowSize(600, 300);初始化窗口大小。 在我的重塑函数中,我使用glViewport(0, 0, w, h);来设置视口。 同样在我的重塑功能中,我使用gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0);来设置正交。

但是,当我移动鼠标以查看世界坐标​​时,左下角为(-1,-1),右上角为(1,1)。我在这里做错了吗?我假设因为我在gluOrtho2D调用中分别将底部和顶部设置为-2和2,它应该给我正确的坐标。

如果你发现任何可能出错的事,请帮帮我。

到目前为止,这是我的代码,请忽略arm变量和绘图函数。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>

#if 0 /*unix*/
#include <GL/glut.h>
#endif

#if 1 /*apple */
#include <GLUT/glut.h>
#include <OPENGL/gl.h>
#include <OPENGL/glext.h>
#endif

#if 0 /*windows*/
#include <io.h>
#include <fcntl.h>
#include <glut.h>
#endif

int GW, GH;
bool animfore, animupper;
float foreangle, upperangle;

using namespace std;

void drawShoulder();
void drawUpper();
void drawFore();

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    //draw shoulder
    glPushMatrix();
        drawShoulder();
        //draw upper arm
        glPushMatrix();
            drawUpper();
            //draw forearm
            glPushMatrix();
                drawFore();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutPostRedisplay();
    glutSwapBuffers();
}

void drawShoulder() {
    //cout << "Enters" << endl;

    glBegin(GL_POLYGON);
        glColor3f((float)30/255, (float)0/255, (float)30/255);
        glVertex2f(-2.0, -0.4);
        glVertex2f(-2.0, -1.0);
        glVertex2f(-1.0, -1.0);
        glVertex2f(-1.0, -0.4);
    glEnd();
}

void drawUpper() {

}

void drawFore() {

}

void reshape(GLsizei w, GLsizei h) {
    GW = w;
    GW = h;
    glViewport(0, 0, w, h);
    glLoadIdentity();
    gluOrtho2D(-(float)w/h, (float)w/h, -1.0, 1.0);
    cout << "Enters" << endl;
}

void keyboard(unsigned char key, int x, int y) {
    switch(key) {
        case 'q' : case 'Q' :
            exit(EXIT_SUCCESS);
            break;
    }
}

//control arm animations
void idle() {
    if(animupper) {

    }
    if(animfore) {

    }
}
float p2w_x(int gx) {
    return (float)2.*GW/(GW*GH-GH)*gx-(float)GW/GH;
}

float p2w_y(int gy) {
    int py = GH-1-gy;
    return (float)2./(GH-1.)*py-1;
}

void mouseMove(int x, int y) {
    cout << "(" << p2w_x(x) << "," << p2w_y(y) << ")" << endl;
}

int main(int argc, char **argv) {
    // global variable intializations
    GW = 600;
    GH = 300;
    animfore, animupper = true;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);

    // initialization
    glutInitWindowSize(600, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Robot Arm");
    glClearColor(1.0, 1.0, 1.0, 1.0);

    // callback functions
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutMotionFunc(mouseMove);

    glutMainLoop();
}

2 个答案:

答案 0 :(得分:0)

在设置任何投影矩阵之前使用glMatrixMode(GL_PROJECTION),因为每个矩阵运算都在当前矩阵堆栈中运行。

然后,对于绘图操作,请使用glMatrixMode(GL_MODELVIEW)。

答案 1 :(得分:0)

您的问题是打印的值与您想要的坐标系不匹配吗?投影manrix(glOrtho和朋友)只对你指定给openGL的坐标产生影响 - 并且大约与你的窗口工具包如何将鼠标位置传回你的应用程序有关。

由你来解决由GLUT给出的窗口坐标到你想要的格式/坐标系统。听起来你的p2w_y和p2w_x函数没有编码来做你想要的吗?

相关问题