java - LWJGL 中未绘制线条

标签 java opengl lwjgl

我正在尝试画一条线 LWJGL 3。我可以在 render() 方法中更改框的背景颜色,但无法画线。

代码如下:

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() 包围你的渲染方法,我相当确定

关于java - LWJGL 中未绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192621/

相关文章:

java - LWJGL - 生成图 block 后 ArrayList 为空,并且鼠标单击时无法添加四边形

java - 渲染透明 2D 图像时 LWJGL (OpenGL) 中的视觉伪影

java - LWJGL 在屏幕上绘制彩色文本问题

java - Maven安装-环境变量

Java JTextField 无法更新

c++ - OpenGL:指定将什么值写入深度缓冲区?

c - OpenGL 扩展,如何在 C 和 glsl 中正确使用它们

java - 带有 Json 解析的 ArrayAdapter

java - 属性的 fastxml 序列化

opengl - Superbible第6版未定义平台错误