I am started to deal with OpenGL. My application is written in Java using SWT as windowing system.
Using http://lwjgl.org/ and SWT, I am able to use OpenGL in my SWT canvas. I wrote the following simple OpenGL code in my canvas paint listener:
// clear to background color
GL11.glClearColor(.3f, .5f, .8f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// draw rectangle
GL11.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glBegin(GL11.GL_POLYGON);
GL11.glVertex3f(0.1f, 0.1f, 0.0f);
GL11.glVertex3f(0.1f, 0.9f, 0.0f);
GL11.glVertex3f(0.9f, 0.9f, 0.0f);
GL11.glVertex3f(0.9f, 0.1f, 0.0f);
GL11.glEnd();
GL11.glFlush();
I want know to add a resize listener on my canvas in order to always have my rectangle in the center of the window. How should I do that?
You need to manually set your viewport size by calling
glViewport()every time canvas size changes. After that your screen will have dimensions specified byglOrtho().Also, your matrices are a mess. Projection matrix is used for projection only and modelview for other transformations (rotation, scaling, moving, etc.).