3
votes

I've just started learning LWJGL/OpenGL to create a 3D game, so I started small and tried to make a simple display of a line (2D). The window loads up fine, the right size, but nothing on the display is drawn. There is a blank screen and no changes I seem to make to the code affect what is drawn on the screen. This could be because I have a lack of understanding of how to do this correctly or it could be a problem in my code/with my computer. I've tried adding and removing various things to my initialization code such as glEnable(GL_TEXTURE_2D) and glDisable(GL_TEXTURE_2D), I have tried calling pushMatrix and popMatrix in the render code and I have tried defining the viewport in the GL11 initialization code but nothing seems to be working. My code is the following:

import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;

public class Main {
    public Main(String[] args) throws LWJGLException{
        Logging.info("Starting up "+Info.title+" v"+Info.version+"!");
        initDisplay();
        initGL11();
        startGameLoop();
    }
    public static void main(String[] args) throws LWJGLException{
        new Main(args);
    }
    public void initDisplay(){
        try {
            Logging.info("Initializing display...");
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.setTitle(Info.title);
            Display.create();
            Logging.info("Display initialized!");
        } catch (LWJGLException e) {
            Logging.severe(e);
            Display.destroy();
        }
    }
    public void initGL11() throws LWJGLException{
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0f, 800f, 600f, 0f, 0f, 1f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    public void startGameLoop(){
        while(!Display.isCloseRequested()){
            glBegin(GL_LINE);
                glColor3f(1f,0f,0f);
                glVertex2f(0f,0f);
                glVertex2f(1f,1f);
            glEnd();

            Display.update();
            Display.sync(60);
        }
        Logging.info("Closing down!");
        Display.destroy();
        System.exit(0);
    }
}

Logging and Info classes are just static references to some classes that control the System.out and store static variables. These do not effect LWJGL or OpenGL so I did not see much point in including them.

In my build path, I have lwjgl.jar, lwjgl_util.jar and slick_util.jar and the natives are linked to lwjgl.jar correctly. I get no errors upon starting the project and the output is normal. I am oblivious to why I am still getting a black screen. If its any use, I am following this tutorial series to create my project and it seems to work fine in the video.

2

2 Answers

1
votes
glOrtho(0f, 800f, 600f, 0f, 0f, 1f);
...
glVertex2f(0f,0f);
glVertex2f(1f,1f);

Try submitting geometry that isn't coincident with your near clipping plane.

Or adjust your near clipping plane:

glOrtho(0f, 800f, 600f, 0f, -1f, 1f);

I'd also recommend clearing the framebuffer occasionally.

1
votes

You have forgotten to be calling glClear(GL_COLOR_BUFFER_BIT). It should always be called before you draw.