質問

I am making a platformer which I started developing with default java functions, but now am switching to OpenGL for everything. I have done everything like I always do with OpenGL, and what I did works fine in my other OpenGL projects. Now my problem is that LWJGL/OpenGL is scaling my textures in a very strange way.

enter image description here

It seems to be related to my screen's aspect ratio. (8:5)

I already had to flip the screen to make it the right way round, but as you can see the text is working fine, it's just the textured rect, and it isn't even straight on the bottom.

Here are the most important snippets from the two classes which actually use OpenGL: Metamorph.java (main class)

    public static void initGL()
    {
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glMatrixMode(GL_MODELVIEW);
            glClearColor(0, 0, 0, 1);
            glDisable(GL_DEPTH_TEST);
    }

    public void render()
    {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glLoadIdentity();
            glScalef(1.0f, -1.0f, 1.0f);
            glTranslatef(0f, -720f, 0f);
            //glScalef(1280f/800, 720f/500, 1f);

            renderer.render();

            Display.update();
            Display.sync(60);
    }

Renderer.java (rendering stuff)

    private void renderMainMenuWithGL()
    {
            //System.out.println("Main Menu!");

            glColor4f(1, 1, 1, 1);

            try
            {
                    Texture bg = loadTexture("mockery");
                    bg.bind();
            } catch (Exception e)
            {
                    e.printStackTrace();
            }

            //drawQuad(0, 0, 1280, 720, 0, 0, 1280, 720);
            glPushMatrix();
            {
                    glBegin(GL_QUADS);
                    glTexCoord2f(0, 0);glVertex2f(0, 0);
                    glTexCoord2f(0, 1);glVertex2f(0, 720);
                    glTexCoord2f(1, 1);glVertex2f(1280, 720);
                    glTexCoord2f(1, 0);glVertex2f(1280, 0);
                    glEnd();
            }
            glPopMatrix();

            TrueTypeFont f = loadFont(MAINFONT, Font.PLAIN, 50);
            TrueTypeFont fb = loadFont(MAINFONT, Font.PLAIN, 48);

            int sel = -1;
            if(Mouse.getX() > 1000 && Mouse.getX() < 1240 && Mouse.getY() > 282.5F && Mouse.getY()< 737.5F)
                    sel = Math.round((Mouse.getY() - 337.5F)/75F);

            if(sel == 0)
                    drawStringRight(fb, 1240, 350, "Story", new Color(0xff516b6b));
            else
                    drawStringRight(f, 1240, 350, "Story", new Color(0xff516b6b));
    }

    private void drawStringRight(TrueTypeFont f, int x, int y, String s, Color c)
    {
            glPushMatrix();
            f.drawString(x-f.getWidth(s), y, s, c);
            glPopMatrix();
    }

I am also open to advice on file structure/what I did wrong elsewhere, but keep in mind this is heavily WIP

役に立ちましたか?

解決

The only possible problem I can see from what you have posted is the scaling and translating you are doing prior to rendering. You should not need to do this with your projection matrix setup the way it is. Other possibilities are that either the dimensions are not really 1280x720 as you think or you have modified one of the matrices further in the code you have not posted. I would try setting both the modelview and projection matrices to the identity matrix and then use glOrtho as you have above immediatly before drawing your quad, and use Display.getWidth and Display.getHeight instead of 1280/720 for the vertex coords.

This works fine for me:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2i(0, 0);
glTexCoord2f(1,0);
glVertex2i(Display.getWidth(), 0);
glTexCoord2f(1,1);
glVertex2i(Display.getWidth(), Display.getHeight());
glTexCoord2f(0,1);
glVertex2i(0, Display.getHeight());
glEnd();

If this still doesnt work, make sure that the viewport is also set to the entire display: glViewport(0,0,Display.getWidth(),Display.getHeight()).

Aswell, I notice that you are using glPushMatrix() and glPopMatrix before and after drawing, which does nothing and is not needed. Push and pop are used to save the current projection or modelview matrix and then reload it later, so that you can apply transformations inbetween and undo them when needed.

他のヒント

You're setting the projection matrix such that the coordinates of the corners of the window are (0,0) through (1680, 1050), and those are mapped into an area that covers 1280x800 pixels. Then you're drawing a 1280x720 image into it, so the screen coordinates of the image come out to span only 800x500 pixels. If you use 1280x720 in your glOrtho() call, I believe it will fix the issue. That is, you want the size of the window, not the size of the display in that call.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top