Advertising (This ad goes away for registered users. You can Login or Register)

lwjgl/opengl window resizing

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

lwjgl/opengl window resizing

Post by Xian Nox » Wed Aug 01, 2012 1:24 pm

For some time now I have been trying to scale the contents of a lwjgl window.
The point of this code is to display two lines that start from the center of the screen and reach outwards. When first run, they display properly, but when the window is resized, the center point is supposed to be updated, but isn't.
Here is the relevant code:

Code: Select all

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import GraphicsSettings;
import WindowTitleList;

public class GLWindow {
	private int fps_cap = 60;
	private int width = 800, height = 600;
	private GLRenderable content;
	
	public GLWindow() {
		if(System.getProperty("os.name").startsWith("Windows"))
			System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "\\lib\\native\\windows");
		else if(System.getProperty("os.name").startsWith("Linux"))
			System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "/lib/native/linux");
		else {
			System.out.println("No native OpenGL support!");
			System.exit(0);
		}
		width  = GraphicsSettings.getInstance().getWindowSize().width;
		height = GraphicsSettings.getInstance().getWindowSize().height;
	}
	
	public void show() {
		try {
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.setTitle(WindowTitleList.getRandomWindowTitle());
			if(GraphicsSettings.getInstance().isResizable())
				Display.setResizable(true);
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		while (!Display.isCloseRequested()) {
			width = Display.getWidth();
			height = Display.getHeight();
			Display.setTitle(width + ":" + height);

			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |GL11.GL_DEPTH_BUFFER_BIT);     // Clear Screen And Depth Buffer
			set2DMode(0, width, 0, height);
			
			if(content!=null)	content.render();
			Display.update();
			Display.sync(fps_cap);
		}
		
		Display.destroy();
	}
	
	public static void set2DMode(float x, float width, float y, float height) {
		//GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glMatrixMode(GL11.GL_PROJECTION);    // Select The Projection Matrix
		GL11.glPushMatrix();                      // Store The Projection Matrix
		GL11.glLoadIdentity();                    // Reset The Projection Matrix
		GL11.glOrtho(x, width, y, height, -1, 1); // Set Up An Ortho Screen
		GL11.glMatrixMode(GL11.GL_MODELVIEW);     // Select The Modelview Matrix
		GL11.glPushMatrix();                      // Store The Modelview Matrix
		GL11.glLoadIdentity();                    // Reset The Modelview Matrix
	}
	public static void set3DMode() {
		GL11.glMatrixMode(GL11.GL_PROJECTION);    // Select The Projection Matrix
		GL11.glPopMatrix();                       // Restore The Old Projection Matrix
		GL11.glMatrixMode(GL11.GL_MODELVIEW);     // Select The Modelview Matrix
		GL11.glPopMatrix();                       // Restore The Old Projection Matrix
		//GL11.glEnable(GL11.GL_DEPTH_TEST);
	}
	
	public void setContents(GLRenderable c) {
		this.content = c;
		this.show();
	}

}

Code: Select all

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import GraphicsSettings;

public class GLIntroScreen implements GLRenderable {
	
	private int width, height;
	private int length_x, length_y;
	
	public GLIntroScreen() {
		width  = GraphicsSettings.getInstance().getWindowSize().width;
		height = GraphicsSettings.getInstance().getWindowSize().height;
	}
	
	@Override
	public void update(int delta) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render() {
		
		width  = Display.getWidth();
		height = Display.getHeight();
		
		// TODO Auto-generated method stub
		// Clear the screen and depth buffer
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

		// set the color of the quad (R,G,B,A)
		GL11.glColor3f(0.0f,0.0f,1.0f);

		GL11.glBegin(GL11.GL_LINE);
			GL11.glVertex2i(width/2,          height/2);
			GL11.glVertex2i(width/2-length_x, height/2);
		GL11.glEnd();
		
		GL11.glBegin(GL11.GL_LINE);
			GL11.glVertex2i(width/2,          height/2);
			GL11.glVertex2i(width/2+length_x, height/2);
		GL11.glEnd();

		GL11.glBegin(GL11.GL_LINE);
			GL11.glVertex2i(width/2, height/2);
			GL11.glVertex2i(width/2, height/2-length_y);
		GL11.glEnd();

		GL11.glBegin(GL11.GL_LINE);
			GL11.glVertex2i(width/2, height/2);
			GL11.glVertex2i(width/2, height/2+length_y);
		GL11.glEnd();
		
		if(length_x < width/3)	length_x++;
        if(length_y < height/5)	length_y++;
	}
}

Code: Select all

interface GLRenderable {
	void update(int delta);
	void render();
}
Note that this is my first time writing anything with OpenGL, so the code is pretty much a collection of code from various places that I think should work as intended.
Any hints/explanations on how to make it work are welcome.
Advertising

nigh
Posts: 1
Joined: Sat Jun 16, 2012 11:48 pm

Re: lwjgl/opengl window resizing

Post by nigh » Mon Sep 03, 2012 4:57 am

I'm not sure if you've solved this already since it was posted a month ago, but I think you need to update the viewport with a call to glViewport().

Try this for your set2DMode function.

Code: Select all

public static void set2DMode(float x, float width, float y, float height) {
		      //GL11.glDisable(GL11.GL_DEPTH_TEST);
		      GL11.glViewport((int)x, (int)y, (int)width, (int)height); //Add this line.
		      GL11.glMatrixMode(GL11.GL_PROJECTION);    // Select The Projection Matrix
		      GL11.glPushMatrix();                      // Store The Projection Matrix
		      GL11.glLoadIdentity();                    // Reset The Projection Matrix
		      GL11.glOrtho(x, width, y, height, -1, 1); // Set Up An Ortho Screen
		      GL11.glMatrixMode(GL11.GL_MODELVIEW);     // Select The Modelview Matrix
		      GL11.glPushMatrix();                      // Store The Modelview Matrix
		      GL11.glLoadIdentity();                    // Reset The Modelview Matrix
}
EDIT: You'll also need to reset the lengths of the lines if the window is sized down after they're already extended. Something like this should work.

Code: Select all

if (length_x*3 > width) {
    length_x = width/3;
}
if (length_y*5 > height) {
    length_y = height/5;
}
Advertising

Post Reply

Return to “Programming”