设置正投影投影矩阵? [英] Setting a orthographic projection matrix?

查看:248
本文介绍了设置正投影投影矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble setting a orthographical projection matrix of dimensions 4x2 with (0,0) being in the center, (-2, -1) being at the bottom left corner, and (2, 1) being at the top right corner.

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

I use glutInitWindowSize(600, 300); to initialize the window size. In my reshape function, I use glViewport(0, 0, w, h); to set the viewport. Also in my reshape function, I use gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0); to set the ortho.

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

However, when I move my mouse around to see the world coordinates, the bottom left corner is (-1, -1) and the top right corner is (1, 1). Is there something I am doing wrong here? I am assuming since I am setting bottom and top to -2 and 2 respectively in the gluOrtho2D call, it should give me the right coordinates.

如果您发现任何错误的地方,请帮助我.

Please help me out if you see anything that might be wrong.

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

Here is my code so far, please ignore the arm variables and drawing functions.

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

推荐答案

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

Use glMatrixMode(GL_PROJECTION) before setting any projection matrix, since each matrix operation operates in the current matrix stack.

然后进行绘图操作,请使用glMatrixMode(GL_MODELVIEW).

Then for drawing operations, use glMatrixMode(GL_MODELVIEW).

这篇关于设置正投影投影矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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