设置正交投影矩阵?

发布于 2024-09-30 17:03:15 字数 3052 浏览 6 评论 0原文

我在设置尺寸为 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();
}

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.

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-10-07 17:03:15

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

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

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

Then for drawing operations, use glMatrixMode(GL_MODELVIEW).

|煩躁 2024-10-07 17:03:15

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

您可以将 GLUT 给出的窗口坐标取消投影到您想要的格式/坐标系统中。听起来您的 p2w_y 和 p2w_x 函数没有被编码为执行您想要的操作?

Is your problem that the values being printed don't match up with the coordinate system you want? The projection manrix (glOrtho and friends) only have effect on the coordinates you specify to openGL - and have approx 0 to do with how your windowing toolkit communicates mouse positions back to your app.

It it up to you to un-project the window-coords given by GLUT into the format/coord system you want. It sounds like your p2w_y and p2w_x functions aren't coded to do what you want them to?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文