1
votes

I am trying out the LWJGL library, but I am a little confused. When I try to render a quad with 2d vertices: glVertex2f(0, 0); glVertex2f(0, 1000); glVertex2f(1000, 1000); glVertex2f(1000, 0);, then everything seems fine, but when I use the code below, I only see a black screen. Am I using the wrong coordinates, so it doesn't show up on the screen, or is something else wrong?

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.opengl.GL11.*;

public class main {

    public void start() throws LWJGLException {
        int height = 600;
        int width = 800;
        DisplayMode standard = new DisplayMode(width, height);
        Display.setDisplayMode(standard);
        Display.create();

        // init OpenGL here
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, 0, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            // clear the screen and depth buffer
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // set the color of the quad (R,G,B,A)
            glColor3f(0.5f, 0.5f, 1.0f);

            // draw quad
            glBegin(GL_QUADS);
            glVertex3f(0, 0, 10);
            glVertex3f(0, 1000, 10);
            glVertex3f(1000, 1000, 10);
            glVertex3f(1000, 0, 10);
            glEnd();

            Display.update();
        }

        Display.destroy();
    }

    public static void main(String[] argv) throws LWJGLException {
        main displayExample = new main();
        displayExample.start();
    }
}
1

1 Answers

0
votes

I probably did something wrong with the view. I managed to make a functioning 3d cube. The keys Q,W,E respectively add numbers to the x, y and z coordinates of the eye-point (I did not take FPS into account, so you might adapt eye* += 0.1) and A, S, and D distract numbers from x,y,z.

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

public class main {
    float eyex, eyey, eyez;

    public void start() throws LWJGLException {
        int height = 600;
        int width = 800;
        eyex = 0;
        eyey = 0;
        eyez = 0;
        DisplayMode standard = new DisplayMode(width, height);
        Display.setDisplayMode(standard);
        Display.create();


        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        glMatrixMode(GL_PROJECTION);
        gluPerspective(90f, (float) width / (float) height, 1f, 10000f);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            glLoadIdentity();
            gluLookAt(eyex, eyey, eyez, 50f, 50f, 50f, 0f, 0f, 1f);
            glClear(GL_COLOR_BUFFER_BIT);
            glClear(GL_DEPTH_BUFFER_BIT);

            glBegin(GL_QUADS);
            glColor3f(1,0,0);
            glVertex3f(0,0,0);
            glVertex3f( 100,0,0);
            glVertex3f( 100, 100,0);
            glVertex3f(0, 100,0);
            glColor3f(0,100,0);
            glVertex3f(0,0,0);
            glVertex3f(0,0, 100);
            glVertex3f(0, 100, 100);
            glVertex3f(0, 100,0);
            glColor3f(0,0,100);
            glVertex3f( 100,0, 100);
            glVertex3f( 100,0,0);
            glVertex3f( 100, 100,0);
            glVertex3f( 100, 100, 100);
            glColor3f(1,1,0);
            glVertex3f(0,0,0);
            glVertex3f( 100,0,0);
            glVertex3f( 100,0, 100);
            glVertex3f(0,0, 100);
            glColor3f(0,1,1);
            glVertex3f(0,0, 100);
            glVertex3f( 100,0, 100);
            glVertex3f( 100, 100, 100);
            glVertex3f(0, 100, 100);
            glColor3f(1,1,1);
            glVertex3f(0, 100, 100);
            glVertex3f(0, 100,0);
            glVertex3f( 100, 100,0);
            glVertex3f( 100, 100, 100);
            glEnd();
            pollInput();
            Display.update();
        }

        Display.destroy();
    }

    void pollInput() {
        if (Mouse.isButtonDown(0)) {
            int x = Mouse.getX();
            int y = Mouse.getY();

            System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("SPACE KEY IS DOWN");
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
            eyex += 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            eyey += 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
            eyez += 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            eyex -= 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            eyey -= 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            eyez -= 0.1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
            eyex = 0;
            eyey = 0;
            eyez = 0;
        }

    }

    public static void main(String[] argv) throws LWJGLException {
        main displayExample = new main();
        displayExample.start();
    }
}