问题
For some reason the size if the window always stays the same size even when i change the width and the height. I think the problem is something to the viewport as the viewable area in the window changes correctly but there's always a black area in the part of the window that is not used. I've probably expalained this really badly but any help would be much appreciated.
I've put a screenshot of the window below..
public class DisplayExample
{
private int width = 800;
private int height = 600;
private int colourDepth = 32;
private String windowTitle = "My First Window";
public boolean closeRequested = false;
private boolean fullscreen = false;
private float rtri = 0.0f;
private float rquad = 0.0f;
public void start()
{
createWindow(windowTitle, width, height, colourDepth, fullscreen);
initGL();
while (!closeRequested)
{
pollInput();
updateLogic();
renderGL();
Display.update(); // flushes OpenGL pipeline and swaps back and
// front buffers. perhaps waits for v-sync.
}
cleanUp();
}
/**
* Initialise OpenGL
*/
private void initGL()
{
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f,
100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glShadeModel(GL11.GL_SMOOTH); // Smooth shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}
/**
* Update Logic
*/
private void updateLogic()
{
}
/**
* Render OpenGL
*/
private void renderGL()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear
// The
// Screen
// And
// The
// Depth
// Buffer
GL11.glLoadIdentity(); // Reset The View
GL11.glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into
// The Screen 6.0
GL11.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
GL11.glBegin(GL11.GL_TRIANGLES); // Drawing Using Triangles
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
GL11.glEnd(); // Finished Drawing The Triangle
GL11.glLoadIdentity(); // Reset The View
GL11.glTranslatef(1.5f, 0.0f, -2.5f); // Move Right 3 Units
GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
//TOP
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left
//FRONT
GL11.glColor3f(1.0f, 0.5f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
//LEFT
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left
//RIGHT
GL11.glColor3f(1.0f, 1.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left
//BACK
GL11.glColor3f(0.0f, 0.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left
//BOTTOM
GL11.glColor3f(1.0f, 0.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Left
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Top Right
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
GL11.glEnd(); // Done Drawing The Quad
rtri += 0.5f;
rquad += -0.5f;
}
/**
* Poll Input
*/
public void pollInput()
{
// scroll through key events
while (Keyboard.next())
{
if (Keyboard.getEventKeyState())
{
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
closeRequested = true;
else if (Keyboard.getEventKey() == Keyboard.KEY_F1)
{
fullscreen = !fullscreen;
System.out.println(fullscreen);
setDisplayMode(width, height, fullscreen);
}
}
}
if (Display.isCloseRequested())
{
closeRequested = true;
}
}
private void createWindow(String title, int width, int height,
int colourDepth, boolean fullscreeen)
{
try
{
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++)
{
DisplayMode current = modes[i];
if (current.isFullscreenCapable())
{
Display.setDisplayMode(current);
}
else
Display.setDisplayMode(new DisplayMode(width, height));
}
Display.setFullscreen(fullscreeen);
Display.setTitle(title);
Display.setVSyncEnabled(true);
Display.create();
}
catch (LWJGLException e)
{
Sys.alert("Error", "Initialisation failed!\n\n" + e.getMessage());
}
}
private void cleanUp()
{
Display.destroy();
}
public void setDisplayMode(int width, int height, boolean fullscreen)
{
// return if requested DisplayMode is already set
if ((Display.getDisplayMode().getWidth() == width)
&& (Display.getDisplayMode().getHeight() == height)
&& (Display.isFullscreen() == fullscreen))
{
return;
}
try
{
DisplayMode targetDisplayMode = null;
if (fullscreen)
{
DisplayMode[] modes = Display.getAvailableDisplayModes();
int freq = 0;
for (int i = 0; i < modes.length; i++)
{
DisplayMode current = modes[i];
if ((current.getWidth() == width)
&& (current.getHeight() == height))
{
if ((targetDisplayMode == null)
|| (current.getFrequency() >= freq))
{
if ((targetDisplayMode == null)
|| (current.getBitsPerPixel() > targetDisplayMode
.getBitsPerPixel()))
{
targetDisplayMode = current;
freq = targetDisplayMode.getFrequency();
}
}
// if we've found a match for bpp and frequence against
// the
// original display mode then it's probably best to go
// for this one
// since it's most likely compatible with the monitor
if ((current.getBitsPerPixel() == Display
.getDesktopDisplayMode().getBitsPerPixel())
&& (current.getFrequency() == Display
.getDesktopDisplayMode().getFrequency()))
{
targetDisplayMode = current;
break;
}
}
}
}
else
{
targetDisplayMode = new DisplayMode(width, height);
}
if (targetDisplayMode == null)
{
System.out.println("Failed to find value mode: " + width + "x"
+ height + " fs=" + fullscreen);
return;
}
Display.setDisplayMode(targetDisplayMode);
Display.setFullscreen(fullscreen);
}
catch (LWJGLException e)
{
System.out.println("Unable to setup mode " + width + "x" + height
+ " fullscreen=" + fullscreen + e);
}
}
public static void main(String[] argv)
{
DisplayExample displayExample = new DisplayExample();
displayExample.start();
}
}
回答1:
The setDisplayMode only creates the window at an initial size. But when it gets resized the change must be applied to OpenGL.
I always recommend to forget about the idea of a "one time OpenGL initialization". IMHO the best practice is to set all state that directly affects drawing operations, and viewport and projection do belong to that state, in the drawing code! The whole code you have in InitGL
should be at the beginning of renderGL
. width
and height
are variables to be set by the window resizing handler, or determined by querying window state.
来源:https://stackoverflow.com/questions/8249972/lwjgl-opengl-window-size-always-stays-the-same-despite-changing-width-and-height