由于某种原因,即使我改变宽度和高度,窗口总是保持相同的大小。我认为问题是视口的问题,因为窗口中的可视区域会正确更改,但窗口部分中始终存在未使用的黑色区域。我可能已经非常苛刻地提出了这个问题,但任何帮助都会非常感激。
我在下面放了一个窗口的截图..
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();
}
}
答案 0 :(得分:1)
setDisplayMode仅以初始大小创建窗口。但是当调整大小时,必须将更改应用于OpenGL。
我总是建议忘记“一次性OpenGL初始化”的想法。恕我直言,最佳做法是设置直接影响绘图操作的所有状态,并且视口和投影确实属于该状态,在绘图代码中! InitGL
中的整个代码应该是在renderGL
的开头。 width
和height
是由窗口大小调整处理程序设置的变量,或通过查询窗口状态确定的。