我正在尝试绘制一条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;
}
}
}
答案 0 :(得分:0)
你需要用glPushMatrix()和glPopMatrix()包围你的渲染方法我很相信