LWJGL Forum

Programming => OpenGL => Topic started by: skyress3000 on August 20, 2014, 16:35:27

Title: OpenGL not rendering in fullscreen
Post by: skyress3000 on August 20, 2014, 16:35:27
I'm following the tutorials on the lwjgl wiki, but when I came to the last one I found this problem: my code won't render anything when it is fullscreen. It has no problem setting the display mode etc, but the blue square just won't show up. Here's my code:

import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;

public class OpenGLTesting {
//position of quad
float x = 400, y = 300;
//angle of quad rotation
float rotation = 0;

//time at last frame
long lastFrame;

int fps;
long lastFPS;

boolean vsync;

public void startYoFace() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}

initGL();
getDelta();
lastFPS = getTime();

while(!Display.isCloseRequested()) {
int delta = getDelta();

update(delta);
renderGL();

Display.update();
Display.sync(60);
}

Display.destroy();
}

public void update(int delta) {
rotation += 0.15f * delta;

if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;

if(Keyboard.isKeyDown(Keyboard.KEY_UP)) y += 0.35f * delta;
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y -= 0.35f * delta;

while(Keyboard.next()) {
if(Keyboard.getEventKeyState()) {
if(Keyboard.getEventKey() == Keyboard.KEY_F) {
setDisplayMode(800, 600, !Display.isFullscreen());
}
else if(Keyboard.getEventKey() == Keyboard.KEY_V) {
vsync = !vsync;
Display.setVSyncEnabled(vsync);
}
}
}

//keep quad on the screen
if(x < 0) x = 0;
if(x > 800) x = 800;
if(y < 0) y = 0;
if(y > 600) y = 600;

updateFPS();
}

public void setDisplayMode(int width, int height, boolean fullscreen) {
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 frequency against the
//original display mode then it's probably bes 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);
}

Display.setDisplayMode(targetDisplayMode);
Display.setFullscreen(fullscreen);
} catch(LWJGLException johnTheExceptionWhoIsABitchAndDeservesToDie) {
System.out.println("Unable to setup mode: " + width + "x" + height + " fs=" + fullscreen + johnTheExceptionWhoIsABitchAndDeservesToDie);
}
}

public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;

return delta;
}

public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public void updateFPS() {
if(getTime() - lastFPS > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}

public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

public void renderGL() {
if(Display.isFullscreen()) System.out.println("fullscreen...");
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

GL11.glColor3f(0.5f, 0.5f, 1.0f);

GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(rotation, 0f, 0f, 1f);
GL11.glTranslatef(-x, -y, 0);

GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x - 50, y - 50);
GL11.glVertex2f(x + 50, y - 50);
GL11.glVertex2f(x + 50, y + 50);
GL11.glVertex2f(x - 50, y + 50);
GL11.glEnd();
GL11.glPopMatrix();
}

public static void main(String[] args) {
OpenGLTesting test = new OpenGLTesting();
test.startYoFace();
}
}
Title: Re: OpenGL not rendering in fullscreen
Post by: Kai on November 04, 2014, 15:04:50
Try a glViewport(0, 0, 800, 600); at the very beginning of your renderGL method. Whenever I played with resizing windows in whatever window manager I used, mostly I saw nothing because of an ill defined GL viewport transform.