LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: dangerdoc on April 23, 2013, 17:32:37

Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 23, 2013, 17:32:37
Hello,
I have been playing with 2-d rendering to make the main menu for my video game, and I have encountered some problems... I have a class that renders a quad, with a method called rendermenu():

public void rendermenu() {

       GL11.glBegin(GL11.GL_QUADS);
       GL11.glColor3f(0, 255, 0);
       GL11.glVertex2f(0, 0);
       GL11.glColor3f(255, 0, 0);
       GL11.glVertex2f(display.width(), 0);
       GL11.glColor3f(0, 0, 255);
       GL11.glVertex2f(display.width(), display.height());
       GL11.glColor3f(255, 255, 255);
       GL11.glVertex2f(0, display.height());
       GL11.glEnd();

   }

You will notice that it is not the lwjgl display class as it is not Display. I have a class called display (for display controller) with methods width() and height():

public static int width(){
       return Display.getWidth();
   }
   
   public static int height(){
       return Display.getHeight();
   }

I would expect this code to make a quad that covers the whole screen, but it seems that the height() function is not accurate....
(http://i1154.photobucket.com/albums/p524/dangerdoc44/menutest_zps402bc254.png) (http://s1154.photobucket.com/user/dangerdoc44/media/menutest_zps402bc254.png.html)
What am I doing wrong?
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 23, 2013, 18:23:43
I set Display.setResizable() to true and when I resize it, the vertexes appear to move (the colors shift) but there is black area around the box that does get rendered. I have absolutely no idea what I am doing wrong!
Title: Re: Problems with Display.getHeight()
Post by: spasi on April 23, 2013, 19:57:11
Works fine here. Please check your glViewport values, the project matrix (should be orthographic) and the modelview matrix (should be identity). If everything looks ok, please make a simple demo that reproduces the issue and post the full source for it.
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 23, 2013, 20:20:19
I have spent a bit googling and looking at code... The glViewport values are as so:

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

But I don't know how to set the project matrix or the modelview matrix...

This is the closest to those things that i found:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glOrtho(0.0f, height(), width(), 0.0f, 1.0f, -1.0f);


Can somebody jump in and tell me how to set these values?
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 24, 2013, 00:56:22
I tried putting a texture on it and it had the same result... I also tried changing the GL11.glOrtho() and it was an improvement.

Here is the change I made (it is called once during init):

GL11.glOrtho(0.0f, display.width(), display.height(), 0.0f, 0.0f, 1.0f);

Now, it shows like this:
(http://i1154.photobucket.com/albums/p524/dangerdoc44/menutest1_zpsd82d4d87.png) (http://s1154.photobucket.com/user/dangerdoc44/media/menutest1_zpsd82d4d87.png.html)

As you can see, still a border and I am clueless as to how I should fix it.

When I resize the window (i set it to re-sizable) it shows up like this:
(http://i1154.photobucket.com/albums/p524/dangerdoc44/menutest1resized_zpsf13af3cb.png) (http://s1154.photobucket.com/user/dangerdoc44/media/menutest1resized_zpsf13af3cb.png.html)

Any ideas? I have the same glViewport settings as last time.
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 24, 2013, 01:49:36
Ok, I have an example here of a compact version of my code (includes everything that would have been run).

Main class:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ComponentEvent;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class tffs {

    mainmenu mainmenu;
    display display;

    public static void main(String[] args) {
        tffs tffs = new tffs();
        tffs.mainloop();
    }

    public void mainloop() {
        mainmenu = new mainmenu();
        display = new display();
        display.create(800, 600);
        display.make2D();
        initOGL();
        mainmenu.init();
        while (!display.icr() == true) {
            checkclose();
            mainmenu.update();
            display.update();
        }

        display.destroy();
        System.exit(1);
    }

    public void checkclose() {

        if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            System.out.print("exiting");
            System.exit(0);
        }

    }

    public void initOGL() {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glOrtho(0.0f, display.height(), display.width(), 0.0f, -1.0f, 1.0f);
        //GL11.glMatrixMode(GL11.GL_PROJECTION);
        //GL11.glLoadIdentity();
        //GL11.glViewport(0, 0, display.width(), display.height());

    }
}


Here is the display controller:
import java.awt.Dimension;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class display {

    public void create(int width, int height) {

        try {
            Display.setDisplayMode(new DisplayMode(width, height));
            Display.create();
            Display.setResizable(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

    }

    public void destroy() {
        Display.destroy();
    }

    public boolean icr() {
        return (Display.isCloseRequested());
    }

    public void update() {
        Display.update();
    }
   
    public static int width(){
        System.out.println(Display.getWidth());
        return Display.getWidth();
       
    }
   
    public static int height(){
        System.out.println(Display.getHeight());
        return Display.getHeight();
    }

    public static void make2D() {
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
        GL11.glViewport(0, 0, width(), height());
    }

    protected static void make3D() {
        GL11.glDisable(GL11.GL_BLEND);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity(); // Reset The Projection Matrix
        GLU.gluPerspective(45.0f, ((float) Display.getWidth() / (float) Display.getHeight()), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window

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


And here is the mainmenu class that does all the rendering:
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

class mainmenu extends tffs {
    Texture texture;
    int width;
    int height;
    display display = new display();
    world world;
    int seed;
    int gamemode = 0;
    boolean buttonpressed = false;

    public void mainmenu() {

        world = new world();

    }

    public int getSeed() {

        return seed;

    }
   
    public void init(){
       
        try{
            texture = TextureLoader.getTexture("PNG", this.getClass().getClassLoader().getResource("tffs/textures/stone.png").openStream());
        }catch(IOException e){
           
        }
       
    }

    public void update() {
       
        rendermenu();

    }

    public void rendermenu(){
        width=display.width();
        height=display.height();
        texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0f, 0f);
        GL11.glVertex2f(0, 0);
        GL11.glTexCoord2f(2f, 0f);
        GL11.glVertex2f(0, height);
        GL11.glTexCoord2f(2f, 2f);
        GL11.glVertex2f(width, height);
        GL11.glTexCoord2f(0f, 2f);
        GL11.glVertex2f(width, 0);
        GL11.glEnd();

    }
}

I have been changing the code... Can someone correct this code and tell me what I am doing wrong? Sorry for making this a long thread...

Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 24, 2013, 05:00:54
After more experimentation, I discovered that the quad only covers the whole screen when I have the frame set to a perfect square (800x800, 600x600, etc) IF I resize it to a perfect square.

If I init the screen to 800x800 it still has border, but if I resize it to another size then back to 800x800 it no longer has the border...

I fiddled with the glOrtho settings and it seemed to make it more normal.

Why does it have the black border when I render the quad inside the window, but go away after resizing it?
Title: Re: Problems with Display.getHeight()
Post by: spasi on April 24, 2013, 08:02:08
You need to use Display.wasResized() in your main loop. When it returns true, you have to update the current viewport and projection configuration.
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 24, 2013, 16:52:14
I added that to the loop as so:
if(Display.wasResized()){
    GL11.glViewport(0,0,width(),height());
}


It still has the border unless I resize it to a perfect square.
Title: Re: Problems with Display.getHeight()
Post by: spasi on April 24, 2013, 16:55:52
The PROJECTION matrix has to change as well:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(...); // use new width/height here
glMatrixMode(GL_MODELVIEW);
Title: Re: Problems with Display.getHeight() [Solved!]
Post by: dangerdoc on April 24, 2013, 23:18:25
I wondered if it was that I had set it to resizable. I removed just that line and it worked.

Note to devs:

If I set it to resizable then it messes it up. Thanks spasi!

Dang it, still has that problem... It doesn't render to the top of the screen unless it is a perfectly square Display. Here is my render code:
GL11.glRectd(0, 0, width, height);

These are the init OGL settings:
            Display.setDisplayMode(new DisplayMode(width, height));
            Display.create();
            GL11.glEnable(GL11.GL_TEXTURE_2D);


And these are the gl settings called per loop:
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0.0f, display.height(), 0.0f, display.width(), -1.0f, 1.0f);
        GL11.glViewport(0, 0, width(), height());
        GL11.glMatrixMode(GL11.GL_MODELVIEW);


The reason I call those gl settings for each loop is because I plan on switching between 2-d and 3-d for the gui, though for this test I only render in 2-d..

Will anything I have shown mess it up?
Title: Re: Problems with Display.getHeight()
Post by: spasi on April 26, 2013, 11:13:34
Please post code for a fully working (without any dependencies to your own code) test case that reproduces the issue.
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on April 27, 2013, 18:14:33
All right I'll start working on that.
Title: Re: Problems with Display.getHeight()
Post by: Redkin on April 27, 2013, 20:42:59
So i don't know if you have tried it like this, but here is my version of doing this which works pretty well

               
               if (Display.wasResized()) {
                    GL11.glOrtho(0.0f, with(), height(), 0.0f, 1.0f, -1.0f);
                    GL11.glMatrixMode(GL11.GL_PROJECTION);
                    GL11.lLoadIdentity(); //I think you should try putting it at the end, but i cant really help you else because that never happened to me
                }
Title: Re: Problems with Display.getHeight()
Post by: quew8 on April 27, 2013, 21:29:34
That code looks ... odd.
You set the current matrix to an orthographic projection.
Then you make the projection matrix the current matrix.
Then you load the identity matrix into the projection matrix.
Wrong order?
Title: Re: Problems with Display.getHeight()
Post by: Redkin on April 28, 2013, 06:22:41
I'm sorry, that it is that strange... I had been quite tired when i wrote it and it even was from my phone  :-\
Title: Re: Problems with Display.getHeight()
Post by: dangerdoc on May 16, 2013, 00:43:34
Hey guys, sorry I have not replied for so long... I have been very busy in life and have not had time for this. I will continue to try to work on this when i have more time. Thanks for replying.

I am not at my developing computer, but from memory, I have set the matrix to ortho, don't know if I made the projection matrix current, and I loaded the identity. I am not sure if I did this at the end or the middle. I will try with it at the end. Thanks for bearing with me.