0
\$\begingroup\$

I have had a problem with OpenGL where glOrthof is not being applied, leading to my frame having the default 1:1:1 ratio. Here is the code initializing it:

    public void reshape(GLAutoDrawable glAutoDrawable, int i, int i1, int i2, int i3) {
        GL2 gl=glAutoDrawable.getGL().getGL2();
        if(window.getWidth()!=screenWidth||window.getHeight()!=screenHeight)window.setSize(screenWidth,screenHeight);
        unitsTall=window.getHeight()/(window.getWidth()/unitsTall);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, unitsWide, 0.0f, unitsTall, 0.0f, 1.0f);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
    }

unitsWide is equal to 100. Here is the code for drawing the rectangle:

    public static void drawRect(float x,float y,float width,float height){
        GL2 gl=Render.getGL2();
        gl.glRotatef(-rotation,0,0,1);//Rotation needed to be reversed
        gl.glColor4f(red,green,blue,alpha);
        gl.glBegin(GL2.GL_QUADS);
        gl.glVertex2f(x,y);
        gl.glVertex2f(x+width,y);
        gl.glVertex2f(x+width,y+height);
        gl.glVertex2f(x,y+height);
        gl.glEnd();
        gl.glFlush();
        gl.glRotatef(rotation,0,0,1);
    }

The "Render" class is the class with all of the main methods for OpenGL. Here is the code that I used for drawing the rectangle:

Graphics.drawRect(0,0,0.5f,1);

Finally, here is a screenshot of what it looks like when this code executes:

Image of the game window, the bottom left of the rectangle is (0,0)

I have looked around and have not found any other problems like this. Please tell me what I might be doing wrong.

\$\endgroup\$
  • \$\begingroup\$ You'll probably want to not learn OpenGL 1. It's limiting and isn't always well supported. Try more modern versions \$\endgroup\$ – Bálint Mar 28 at 23:06
  • \$\begingroup\$ @Bálint While I do know that I am using a more out of date version of OpenGL, it is what is going to get me started into dealing with it. I know that there are newer, better, and faster ways to make this same code, but right now I just want to learn the basics before going on to the harder parts of OpenGL in my next game. This is also what the tutorial I followed taught me. Besides, I'm not going to be doing anything too advanced. \$\endgroup\$ – ImTheSquid Mar 29 at 13:18
1
\$\begingroup\$
unitsTall=window.getHeight()/(window.getWidth()/unitsTall

You probably intend to divide by unitsWide here; if you check the value of unitsTall in your debugger you'll likely find that it's 0, so you're actually getting a division by 0, which is messing up your matrix.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.