How can I render an easy triangle with LWJGL 3

Started by fabs04, November 20, 2014, 14:31:10

Previous topic - Next topic

fabs04

Hi!

Yesterday I noticed that there is a new version of LWJGL. I tried it with the introduction on the website of LWJGL. It worked perfectly, but then i tried to render an easy triangle how i know it from LWJGL 2. It didn't work.

How can I do this?
Do I have to use VBOs or is there even a new rendering mode in LWJGL 3?

Sry

I hope somebody can help me...  :)

Many thanks in advance and best greetings from Austria.


My whole program:

import org.lwjgl.Sys;
import org.lwjgl.opengl.*;
import org.lwjgl.system.glfw.*;
 
import java.nio.ByteBuffer;
 
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.system.glfw.GLFW.*;
 
public class Main {
 
    private long window;
 
    public void execute() {
        Sys.getVersion();
 
        try {
            init();
            loop();
            glfwDestroyWindow(window);
        } finally {
            glfwTerminate();
        }
    }
 
    private void init() {
        glfwSetErrorCallback(ErrorCallback.Util.getDefault());
 
        if ( glfwInit() != GL11.GL_TRUE )
            throw new IllegalStateException("Unable to initialize GLFW");
 
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
 
        int WIDTH = 800;
        int HEIGHT = 600;
 
        window = glfwCreateWindow(WIDTH, HEIGHT, "Game", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");
 
        WindowCallback.set(window, new WindowCallbackAdapter() {
            @Override
            public void key(long window, int key, int scancode, int action, int mods) {
                if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
                    glfwSetWindowShouldClose(window, GL_TRUE);
            }
        });
 
        ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(
            window,
            (GLFWvidmode.width(vidmode) - WIDTH) / 2,
            (GLFWvidmode.height(vidmode) - HEIGHT) / 2
        );
 
        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
 
        glfwShowWindow(window);
    }
 
    private void loop() {
        GLContext.createFromCurrent();
 
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        while ( glfwWindowShouldClose(window) == GL_FALSE ) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
            glBegin(GL_TRIANGLES);
            	glVertex2f(200, 100);
            	glVertex2f(300, 400);
            	glVertex2f(100, 400);
            glEnd();
            
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    }
 
    public static void main(String[] args) {
        new Main().execute();
    }
}

lightbringer

Servus!

Fixed function pipeline is pretty much deprecated in modern OpenGL but it is still there. I don't recommend that you use it for anything except testing.

That said, your problem here are your actual coordinates. You did not set up any projection so AFAIK your window coordinates are normalized (-1.0 to 1.0).

Try this:
glVertex2f(0.200f, 0.100f);
glVertex2f(0.300f, 0.400f);
glVertex2f(0.100f, 0.400f);

fabs04

Thank you very much for your fast answer. You were exactly right. You helped me a lot.

Can you answer one more question? What is a "fixed function pipeline" and what are alternatives? Is VBO an alternative? I didn't find anything which is understandable for me, because my English is not so good.

lightbringer

VBOs, VAOs, and shaders are basically the "modern way". If you want to program modern OpenGL, you need good tutorials or a good book which is not ten years old.

The 'go-to' books for OpenGL are the OpenGL Superbible (currently 6th edition) and the OpenGL Programming Guide (currently 8th edition). The latest editions are updated to cover the "modern way" of programming OpenGL. Superbible for instance leaves out the old stuff completely.

Or you could try some German books. Check Amazon.de. I have not read any in German but "Grundlagen der 3D-Programmierung: Mathematik und Praxis mit OpenGL" sounds promising. The reviewers say that it covers the difference between old and modern OpenGL and explains both. And it's even in Java (although they use JOGL).

fabs04

Thank you very much!!!  ;D

I will have a look at these books.