Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting a orthographic projection matrix?
    primarykey
    data
    text
    <p>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.</p> <p>I use <code>glutInitWindowSize(600, 300);</code> to initialize the window size. In my reshape function, I use <code>glViewport(0, 0, w, h);</code> to set the viewport. Also in my reshape function, I use <code>gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0);</code> to set the ortho.</p> <p>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.</p> <p>Please help me out if you see anything that might be wrong.</p> <p>Here is my code so far, please ignore the arm variables and drawing functions.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; #include &lt;stdlib.h&gt; #include &lt;iostream&gt; #if 0 /*unix*/ #include &lt;GL/glut.h&gt; #endif #if 1 /*apple */ #include &lt;GLUT/glut.h&gt; #include &lt;OPENGL/gl.h&gt; #include &lt;OPENGL/glext.h&gt; #endif #if 0 /*windows*/ #include &lt;io.h&gt; #include &lt;fcntl.h&gt; #include &lt;glut.h&gt; #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 &lt;&lt; "Enters" &lt;&lt; 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 &lt;&lt; "Enters" &lt;&lt; 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 &lt;&lt; "(" &lt;&lt; p2w_x(x) &lt;&lt; "," &lt;&lt; p2w_y(y) &lt;&lt; ")" &lt;&lt; endl; } int main(int argc, char **argv) { // global variable intializations GW = 600; GH = 300; animfore, animupper = true; glutInit(&amp;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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload