2010-11-05 18 views
0

我無法設置尺寸爲4x2的正投影矩陣,其中(0,0)位於中心,(-2,-1)位於左下角,(2,1)位於右上角角。創建以(0,0)爲中心的4x2 2D座標系?

我使用glutInitWindowSize(600, 300);來初始化窗口大小。 在我的整形功能中,我使用glViewport(0, 0, w, h);來設置視口。 另外在我的整形功能中,我使用gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0);來設置ortho。

但是,當我移動鼠標以查看世界座標​​時,左下角爲(-1,-1),右上角爲(1,1)。有什麼我在這裏做錯了嗎?我假設,因爲我在gluOrtho2D調用中分別將底部和頂部設置爲-2和2,它應該給我正確的座標。

如果您發現任何可能有誤的地方,請幫助我。

這是我的代碼到目前爲止,請忽略臂變量和繪圖功能。

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

回答

1

嘗試設置glMatrixMode(GL_PROJECTION)你讓你的鄰電話前,然後將其設置回GL_MODELVIEW當你做你的轉換。