Domanda

I'm trying to make a simple button in OpenGL/LWJGL,

I can render my 2D QUAD correctly, but when i implement the texture, only about 3/4 parts of the whole quad gets textured, like this: https://dl.dropboxusercontent.com/u/60223805/glerror1.png

and if i remove the texture coords i get this: https://dl.dropboxusercontent.com/u/60223805/glerror2.png

     none.bind();

     co.Enable2D_GUI();
     GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(co.width/2-200, co.height/2);
    GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(co.width/2+none.getTextureWidth(),co.height/2);
    GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(co.width/2+none.getTextureWidth(), co.height/2+none.getTextureHeight());
    GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(co.width/2-200, co.height/2+none.getTextureHeight());
     GL11.glEnd();
     co.Disable2D_GUI();

where none is an Texture (from slick-util library) and the functions Enable2D_GUI and Disable2D_GUI just enables and disable ortho and stuff.

What can be wrong? I'm very new to OpenGL so im sorry if my question is a bit nooby

This is my Enable2D_GUI and Disable2D_GUI functions:

    public void Enable2D_GUI() {

     GL11.glMatrixMode (GL11.GL_PROJECTION);
     GL11.glPushMatrix();
     GL11.glLoadIdentity ();
     GL11.glOrtho (0, width, height, 0, 1, -1);
     GL11.glDisable(GL11.GL_DEPTH_TEST);
     GL11.glMatrixMode (GL11.GL_MODELVIEW);
     GL11.glPushMatrix();
    GL11.glLoadIdentity();
}

public void Disable2D_GUI() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glPopMatrix();
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glPopMatrix();

    GL11.glDisable(GL11.GL_BLEND);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
}

Now when I test it with a 3D QUAD it doesnt work either, same result. This is my OpenGL init code:

           GL11.glEnable(GL11.GL_TEXTURE_2D);
       GL11.glShadeModel(GL11.GL_SMOOTH);
       GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f); 
       GL11.glClearDepth(1.0); 
       GL11.glEnable(GL11.GL_DEPTH_TEST);
       GL11.glDepthFunc(GL11.GL_LEQUAL); 

       GL11.glViewport(0, 0, width, height);

       GL11.glMatrixMode(GL11.GL_PROJECTION); 
       GL11.glLoadIdentity();

       GLU.gluPerspective(
             45.0f,
             (float)width/(float)height,
             0.5f,
             50.0f);


       GL11.glMatrixMode(GL11.GL_MODELVIEW);
       GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
È stato utile?

Soluzione

You're misusing the glPushMatrix and glPopMatrix and also not adding or performing state switches in the correct order.

In you init code you need to change it to the following.

GL11.glViewport(0, 0, width, height);

GL11.glMatrixMode(GL11.GL_PROJECTION); 
GL11.glLoadIdentity();

GLU.gluPerspective(45.0f, (float) width / (float) height, 0.5f, 50.0f);

GL11.glMatrixMode(GL11.GL_PROJECTION); 
GL11.glLoadIdentity();

GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL_SMOOTH);
GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);

You don't have to perform state switches after the glLoadMatrix and glLoadIdentity though it would be better doing so. 1 The code is more readable. 2 Some things gets reset after calling glLoadIdentity though just the stuff about and within the Matrix itself.

Then you also need to fix your Enable2D_GUI and Disable2D_GUI to the following.

Enable2D_GUI

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0f, 1f, -1f);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

GL11.glDisable(GL11.GL_DEPTH_TEST);

Disable2D_GUI

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);

Aleast of what I know you can not use the glPushMatrix and glPopMatrix in between glMatrixMode and glLoadIdentity calls. I could be mistaking Then I also added the glLoadIdentity calls instead to reset the Matrices.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top