LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: technik3k on September 12, 2015, 17:08:04

Title: Recizing Window
Post by: technik3k on September 12, 2015, 17:08:04
Is there a way, for the program, to resize a lwjgl3 window, and also when a window is resized (By user or program) how do I change, I think it's called viewport, so that openGL diplayed on the hole screen.
Title: Re: Recizing Window
Post by: quew8 on September 12, 2015, 17:56:41
Yes.

To resize a window use glfwSetWindowSize(): http://www.glfw.org/docs/latest/group__window.html#ga371911f12c74c504dd8d47d832d095cb (http://www.glfw.org/docs/latest/group__window.html#ga371911f12c74c504dd8d47d832d095cb)

To set the correct viewport, it is best to act on frambuffer resize events which can be done by setting the framebuffer size callback with glfwSetFramebufferSizeCallback(): http://www.glfw.org/docs/latest/group__window.html#ga3203461a5303bf289f2e05f854b2f7cf (http://www.glfw.org/docs/latest/group__window.html#ga3203461a5303bf289f2e05f854b2f7cf). If you really want to work on window resize events then there is the window size callback too: http://www.glfw.org/docs/latest/group__window.html#gaa40cd24840daa8c62f36cafc847c72b6 (http://www.glfw.org/docs/latest/group__window.html#gaa40cd24840daa8c62f36cafc847c72b6).

If you need help setting up callbacks ask as I can't seem to find a general tutorial on using them in LWJGL3.
Title: Re: Recizing Window
Post by: technik3k on September 13, 2015, 22:58:00
I know Callbackes. I mean When the window is recized how do I have my textures projected on the hole screen. When I recize my window the textures show only on the part seen befor resizeing the window.
Title: Re: Recizing Window
Post by: quew8 on September 13, 2015, 23:04:14
You can set the dimensions of the viewport using glViewport(). The viewport is the area of the window into which OpenGL draws and it is specified in pixels.
Title: Re: Recizing Window
Post by: technik3k on September 13, 2015, 23:45:13
So waht does glOrtho do than
Title: Re: Recizing Window
Post by: quew8 on September 14, 2015, 00:01:26
glOrtho() specifies a projection matrix.

Using the camera analogy, the projection matrix is the position & lens of the camera and the viewport is the position and size of the photo the camera produces.

There's more info on the various different parts of the OpenGL pipeline here: http://www.songho.ca/opengl/gl_transform.html (http://www.songho.ca/opengl/gl_transform.html)
Title: Re: Recizing Window
Post by: technik3k on September 14, 2015, 00:05:08
Also I played around with glViewport(). It streches out the textures to fit the window. that's not what i want. I mean when the window get larger the parts that are now reveled I can't draw on (The textures seem to go behined the new parts). And I want the the textures not to chane size.
Title: Re: Recizing Window
Post by: quew8 on September 14, 2015, 00:30:33
Not entirely sure what it is you need. Sounds like probably update the projection matrix and the viewport. Have a read of the link and it should be clear.
Title: Re: Recizing Window
Post by: technik3k on September 14, 2015, 01:05:16
I made some simple images to represent my program.
One is the normal window
One is the window resized and I try going past the new reveled point
And one is what happes with glViewport
Title: Re: Recizing Window
Post by: quew8 on September 14, 2015, 15:13:29
Still not sure what exactly what you want, and all the information you should need is in that link.

But try this code in your resize callback:

//width and height are the window's dimensions.
glViewport(0, 0, width, height);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);


That resizes the viewport to fill the entire window and resizes the projection to keep everything taking up the same space in the window.
Title: Re: Recizing Window
Post by: technik3k on September 15, 2015, 00:31:05
It didn't seem to work, the glOrtho messed suff up (Nothing was being drawn on the screen). My best guess is I have to do something after it, but om not chore what. I use glOrtho when i statrt my program and every thing works well. By the way this image might help you understand what I mean.
Title: Re: Recizing Window
Post by: quew8 on September 15, 2015, 17:39:32
Well you probably have the wrong matrix mode set. Try calling glMatrixMode(GL_PROJECTION) at the start of my previous code snippet and glMatrixMode (GL_MODELVIEW) at the end.
Title: Re: Recizing Window
Post by: technik3k on September 15, 2015, 19:58:06
Tried that already . Nothing. ;D
Title: Re: Recizing Window
Post by: quew8 on September 16, 2015, 10:42:22
Well then I'm not sure. Would need to know your code better to know what the problem is.
Title: Re: Recizing Window
Post by: technik3k on September 17, 2015, 00:40:20
Here's some of my Code:
Main:

errorCallback = Callbacks.errorCallbackPrint(System.err);
glfwSetErrorCallback(errorCallback);
glfwInit();

LanchWindow(800, 600, "Game");

glDisable(GL_DEPTH_TEST);

glEnable(GL_TEXTURE_2D);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glMatrixMode(GL_MODELVIEW);

glClearColor(0.5f, 0.0f, 1.0f, 1.0f);

int TankDown = loadTex("/home/dennis/Black Hole Breakers/Graphics/TankDown.png");
int TankSide = loadTex("/home/dennis/Black Hole Breakers/Graphics/TankSide.png");
int TankUp = loadTex("/home/dennis/Black Hole Breakers/Graphics/TankUp.png");
int Alien = loadTex("/home/dennis/Black Hole Breakers/Graphics/Alien.png");
Bullet = loadTex("/home/dennis/Black Hole Breakers/Graphics/Bullet.png");

initKey();

Keyboard_MouseManagerInit(windowID);

while(GLFW.glfwWindowShouldClose(windowID) == GL_FALSE)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Move(100, 2);

Shoot(1, 5, 800, 600, 50);

AlienAPI(2);

glfwPollEvents();

//Draws acording to direction (way)
//Simple if statements
//          \\
//            _|

glBindTexture(GL_TEXTURE_2D, TankUp);
glBegin(GL_QUADS);
{
glTexCoord2f(1,1);glVertex2f(x,y);
glTexCoord2f(0,1);glVertex2f(x+100,y);
glTexCoord2f(0,0);glVertex2f(x+100,y+100);
glTexCoord2f(1,0);glVertex2f(x,y+100);
}
glEnd();

glBindTexture(GL_TEXTURE_2D, Alien);
glBegin(GL_QUADS);
{
glTexCoord2f(0,0);glVertex2f(Ax+100,Ay+100);
glTexCoord2f(1,0);glVertex2f(Ax,Ay+100);
glTexCoord2f(1,1);glVertex2f(Ax,Ay);
glTexCoord2f(0,1);glVertex2f(Ax+100,Ay);
}
glEnd();

glfwSwapBuffers(windowID);

try { Thread.sleep(5); } catch (InterruptedException e) {Thread.currentThread().interrupt();}
}

System.out.println("Player Got: "+PP);
System.out.println("Alien Got: "+AP);

System.exit(1);

The Launch Window function is:

private static void LanchWindow(int width, int height, String Title)
{
windowID = glfwCreateWindow(width, height, Title, MemoryUtil.NULL, MemoryUtil.NULL);

if(windowID == MemoryUtil.NULL) throw new IllegalStateException("Window Failed");

glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

glfwSetWindowPos(windowID, (GLFWvidmode.width(vidmode) - width) / 2, (GLFWvidmode.height(vidmode) - height) / 2);

glfwMakeContextCurrent(windowID);

glfwShowWindow(windowID);

GLContext.createFromCurrent();

glMatrixMode(GL_PROJECTION);

glOrtho(0, width, 0 , height, -1, 1);
         }

If you need any thing else just tell me. ;D
Title: Re: Recizing Window
Post by: quew8 on September 17, 2015, 09:01:35
Well then I can't see why my code would not be working.

BTW, gfwWindowHint() should be called before glfwCreateWindow(). It specifies hints to create the window with.
Title: Re: Recizing Window
Post by: technik3k on September 18, 2015, 01:23:29
I fuigured it out ! ! !
I did't have my code in the right order. I payed orund with it and I got it. Here it is:

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Thanks for all the help ! ! !  ;D :o ::)
Title: Re: Recizing Window
Post by: quew8 on September 18, 2015, 09:02:52
To clarify, you moved "glMatrixMode(GL_PROJECTION)" to after "glViewport()" and that is what made it work?