gpt4 book ai didi

java - LWJGL 中未绘制线条

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:39 25 4
gpt4 key购买 nike

我正在尝试画一条线 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com