Re: Problems with Display.getHeight()

Started by dangerdoc, April 23, 2013, 17:32:37

Previous topic - Next topic

dangerdoc

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....

What am I doing wrong?
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

dangerdoc

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!
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

spasi

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.

dangerdoc

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?
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

dangerdoc

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:


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:


Any ideas? I have the same glViewport settings as last time.
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

dangerdoc

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...

“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

dangerdoc

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?
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

spasi

You need to use Display.wasResized() in your main loop. When it returns true, you have to update the current viewport and projection configuration.

dangerdoc

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.
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

spasi

The PROJECTION matrix has to change as well:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(...); // use new width/height here
glMatrixMode(GL_MODELVIEW);

dangerdoc

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?
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

spasi

Please post code for a fully working (without any dependencies to your own code) test case that reproduces the issue.

dangerdoc

All right I'll start working on that.
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla

Redkin

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
                }

quew8

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?