Search code examples
javaopenglrotationscale

Problems with OpenGL Rotation and Scale


I just started trying out some OpenGL with java. I downloaded the LWJGL and the Slick-Util Library. Now I'm trying to paint some images on the screen which is working quite fine. But I have to big problems:

  1. When i rotate my image and it's about 45° you can see some bits of the same image at the corners like it's a spritesheet with the same image, which get rotated.

  2. How do I scale my image? It's pretty small and the glScale() func scales the image itself, but not the space where it's printed. So if the image has a size of 16*16 pixels and i scale it up i just see a part of the the scaled image in the 16*16pixels

Here's my code for the OpenGL:

public class Widget {

String name;
int angle;

public Texture image_texture;
public String image_path ;
public int image_ID;

public int cord_x = 0;
public int cord_y = 0;


static LinkedList<Widget> llwidget = new LinkedList<Widget>();

public Widget(String path){
    llwidget.add(this);

    image_path = path;
    try {
        image_texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(image_path), GL_NEAREST);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    };
    image_ID = image_texture.getTextureID();

    glEnable(GL_TEXTURE_2D);              
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);         

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glViewport(0,0,Display.getWidth(),Display.getHeight());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, Display.getWidth(),Display.getHeight(), 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

}

void render(){



    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(0.5f,0.5f,0.0f);

    //! Graphics bug 
    glRotatef(angle,0.0f,0.0f,1.0f);
    //

    glTranslatef(-0.5f,-0.5f,0.0f);

    //! Doesn't work
    glScalef(2f, 2f, 2f);
    //

    glMatrixMode(GL_MODELVIEW);
    Color.white.bind();
    image_texture.bind();



    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex2f(cord_x, Display.getHeight() - cord_y);
    glTexCoord2f(1,0);
    glVertex2f(cord_x+image_texture.getTextureWidth(),Display.getHeight() - cord_y);
    glTexCoord2f(1,1);
    glVertex2f(cord_x+image_texture.getTextureWidth(),Display.getHeight() - cord_y+image_texture.getTextureHeight());
    glTexCoord2f(0,1);
    glVertex2f(cord_x,Display.getHeight() - cord_y+image_texture.getTextureHeight());


    glEnd();


}


}

Solution

  • Question 1: Calling glScalef(2f, 2f, 2f) in GL_TEXTURE mode results in a zoom of the texture inside a quad which has the size determined by your glVertex2f calls. This could lead to unwanted artifacts at the edge of the quad.

    Question 2: The main problem at this is that the program is not in a state which allows to translate your quad coordinates when glScalef(2f, 2f, 2f) got called. After calling glMatrixMode(GL_TEXTURE) all following matrix operations affect the texture matrix. To get a zoom effect of the quad which you draw with glVertex2f you need to get in GL_MODELVIEW Mode before calling glScale2f.

    Alternative: With glVertex2f you determine on which coordinates you draw the quad. The parameters of glVertex2f are coordinates on which to draw vertices. So altering this params would also do the job.