Search code examples
javaopengllwjgl

glFrustum() completely unnecessary?


So I recently asked a question about rendering shapes in 3D space using OpenGL 4.2 and LWJGL 3. But after tinkering with the code for a few hours, I discovered that the glFrustum() and glOrtho() functions are completely unnecessary. I feel like this shouldn't be the case. Here's my code, which renders a colorful triangle that rotates around the Y axis.

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
{
    public static void main (String[] args)
    {
        if (glfwInit() == GL_FALSE)
        {
            System.out.println ("GLFW initialization failed.");
            System.exit (1);
        }

        glfwWindowHint (GLFW_RESIZABLE, GL_FALSE);
        long window = glfwCreateWindow (1280, 720, "OpenGL Window", MemoryUtil.NULL, MemoryUtil.NULL);
        glfwMakeContextCurrent (window);
        glfwSwapInterval (1);
        GL.createCapabilities();
        glfwShowWindow (window);

        while (glfwWindowShouldClose (window) == GL_FALSE)
        {
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glRotatef (1, 0, 1, 0);

            glBegin (GL_TRIANGLES);
            glColor3f (1, 0, 0);
            glVertex3f (0, 0.5f, 0);
            glColor3f (0, 1, 0);
            glVertex3f (-0.5f, -0.5f, 0);
            glColor3f (0, 0, 1);
            glVertex3f (0.5f, -0.5f, 0);
            glEnd();

            glfwSwapBuffers (window);
            glfwPollEvents();
        }

        glfwDestroyWindow (window);
        glfwTerminate();
    }
}

Can I get away with not bothering with any projection matrix, or will I run into problems down the road?

Cheers, Nebula


Solution

  • It depends on what you're doing. It you want to do more advanced openGL (2.0+), you'll need to switch to the programmable pipeline and write your own shaders. while that may seem hard, and it is at first, it pays off with the freedom that that provides. If you decide to work with the programable pipeline, you wont used the fixed pipeline functions, such as glOrtho() or glfrustum() and other immediate mode rendering calls like glBegin(GLenum) and glVertex3f(float, float, float). Also keep in mind that immediate mode is not compatible with the 3.0+ rendering pipeline.

    That all being said, that doesn't render immediate completely useless, It's still useful for laying down quick and dirty demos, but if you want to program graphically intensive applications such as games, you'll likely want at least openGL 3.0.

    If you want to learn advanced openGL, i can recommend The Official Guide to Learning OpengGL, which is what I've read, but it will require a small understanding of C++ (not too much tho). If you would like an easier start, there are some good youtube series on channels such as ThinMatrix's OpenGL tutorial which goes quite in depth with the LWJGL (although version 2 all the openGL calls remain the same), thebenybox who has multiple series including one on an OpenGL game engine.

    Hope this clarified some of your confusions!