Search code examples
javaopengllwjgl

Displaying 3D triangle using LWJGL 3


I've managed to display a two-dimensional triangle in a OpenGL 4.2 window using LWJGL 3. Here's the code I used:

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;

public class TestClass
{
    private static long window;
    private static int WIDTH = 1280;
    private static int HEIGHT = 720;

    public static void main (String[] args)
    {
        if (glfwInit() == GL_FALSE)
        {
            System.out.println ("GLFW initialization failed.");
            System.exit (1);
        }

        glfwWindowHint (GLFW_RESIZABLE, GL_FALSE);
        window = glfwCreateWindow (WIDTH, HEIGHT, "GLFW Window", MemoryUtil.NULL, MemoryUtil.NULL);
        glfwMakeContextCurrent (window);
        glfwSwapInterval (1);
        GL.createCapabilities();
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity();
        glOrtho (0, 12, 12, 0, 1, -1);
        glClearColor (0, 0.7f, 1, 0);
        glMatrixMode (GL_MODELVIEW);
        glfwShowWindow (window);

        while (glfwWindowShouldClose (window) == GL_FALSE)
        {
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glBegin (GL_TRIANGLES);
            glColor3f (1, 0, 0.7f);
            glVertex3f (6, 4, 0); // Vertex one
            glColor3f (1, 0, 0.7f);
            glVertex3f (4, 8, 0); // Vertex two
            glColor3f (1, 0, 0.7f);
            glVertex3f (8, 8, 0); // Vertex three
            glEnd();

            glfwSwapBuffers (window);
            glfwPollEvents();
        }

        glfwDestroyWindow (window);
        glfwTerminate();
    }
}

If I set the Z value of any of my three vertices to something greater than 1 or less than -1, the triangle partially disappears around that vertex. And when I do set the Z value to something between 1 and -1, I can see no difference between that and having the value equal 0. I'm a bit stuck here. Could somebody provide an explanation of how to get this triangle to exist on a plane that is not completely parallel to the viewing angle?

Cheers, Nebula


Solution

  • this happens because you aren't using a perspective view, rather an orthogonal view. When you call glOrtho(), you request that the matrix that is used fro projection be set to an orthogonal matrix, which does not account for depth and does not do a perspective calculation. This is why you do not see a change in the triangle. as for the vertices disappearing in when the value exceeds the range of (-1, 1), that is because you set the last two parameters of your glOrtho() call to 1 and -1 respectively. this tells openGL to discard vertices outside this range (because of the way normalized device coordinates and matrix math work together, which you will probably learn about if you continue using openGL and work with later releases).

    Now, in LWJGL 3, the glu (Utility package) was removed. This makes it a bit harder to initialize a perspective matrix for the fixed pipeline. here is a good post on how to do it, but since its written for c++, I'll provide you with the java equivalent.

    // Replaces gluPerspective. Sets the frustum to perspective mode.
    // fov     - Field of vision in degrees in the y direction
    // aspect   - Aspect ratio of the viewport
    // zNear    - The near clipping distance
    // zFar     - The far clipping distance
    private static void perspectiveGL(float fov, float aspect, float zNear, float zFar) {
        float fH = (float) Math.tan(fov / 360 * Math.PI) * zNear;
        float fW = fH * aspect;
        glFrustum( -fW, fW, -fH, fH, zNear, zFar );
    }
    

    The glFrustum() call is the important part of this function and is what creates a perspective matrix, however, its parameters aren't very user friendly.

    Hope this helped!