简体   繁体   中英

Line not drawing in LWJGL

I'm trying to draw a line LWJGL 3. I can change the background colors in the box render() method but I can't draw a line.

Here's the code:

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.opencl.*;

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

public class Main {

    // The window handle
    private long window;

    enum GameState {
        MAIN, GAME, PAUSED;
    }

    GameState state;
    GLFWKeyCallback keyCallback;
    Box box;

    public void run() { 
        try {
            init();
            loop();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void init() { 
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

        int WIDTH = 300;
        int HEIGHT = 300;

        window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        glfwSetKeyCallback(window, keyCallback = new Input());

        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(
            window,
            (vidmode.width() - WIDTH) / 2,
            (vidmode.height() - HEIGHT) / 2
        );

        glfwMakeContextCurrent(window);
        glfwShowWindow(window);

        state = GameState.MAIN;
        box = new Box(100, 100, 10, 10, 10);
    }

    private void loop() {
        GL.createCapabilities();

        // Set the clear color
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        // Run the rendering loop until the user has attempted to close
        // the window or has pressed the ESCAPE key.
        while ( !glfwWindowShouldClose(window) ) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer

            render();
            update();
            updateKeys();
        }
    }

    void update() {
        // Poll for window events. The key callback above will only be
        // invoked during this call.
        glfwPollEvents();

        switch(state){
            case MAIN:
                break;
            case GAME:
                break;
        }
    }

    void updateKeys() {
        switch(state) {
            case MAIN:
                if(Input.keys[GLFW_KEY_SPACE]) { state = GameState.GAME; }
                break;
            case GAME:
                if(Input.keys[GLFW_KEY_UP]) {

                }
                break;
            default:
                break;
        } 
    }

    void render() {
        glfwSwapBuffers(window); // swap the color buffers
        switch(state) {
            case MAIN:
                break;
            case GAME:
                System.out.println("game rendering");
                box.render();
                break;
        }
    }


    public static void main(String[] args) {
        new Main().run();
    }

    class Box {
        int x, y;
        int dx, dy;

        float speed;
        float r, g, b;

        boolean up, down, left, right;

        void setUp(boolean b) { up = b; }
        void setDown(boolean b) { down = b; }
        void setLeft(boolean b) { left = b; }
        void setRight(boolean b) { right = b; } 

        Box(int x, int y, float r, float g, float b) {
            this.x = x;
            this.y = y;
            dx = 0;
            dy = 0;

            speed = 5;
            this.r = r;
            this.g = g;
            this.b = b;
        }

        void update() {
            if(up)
                y += (int) speed;
            if(down)
                y += (int) -speed;
            if(left)
                x = (int) -speed;
            if(right)
                x = (int) speed;

            x += dx;
            y += dy;

            dx = 0;
            dy = 0;
        }

        void render() { 
            glColor3f(1, 1, 1);
            glBegin(GL_LINES);
               glVertex2f(10, 10);
               glVertex2f(20, 20);
           glEnd();
        }
    }

    static class Input extends GLFWKeyCallback {
        public static boolean[] keys = new boolean[65535];

        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            keys[key] = action != GLFW_RELEASE;
        }
    }
}

你需要用glPushMatrix()和glPopMatrix()包围你的渲染方法我相当肯定

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM