我正在尝试LWJGL库,但我有点困惑。当我尝试渲染具有2d顶点的四边形时:glVertex2f(0,0); glVertex2f(0,1000); glVertex2f(1000,1000); glVertex2f(1000,0);,然后一切似乎都很好,但是当我使用下面的代码时,我只看到一个黑屏。我使用了错误的坐标,因此它没有显示在屏幕上,或者是其他错误?
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.opengl.GL11.*;
public class main {
public void start() throws LWJGLException {
int height = 600;
int width = 800;
DisplayMode standard = new DisplayMode(width, height);
Display.setDisplayMode(standard);
Display.create();
// init OpenGL here
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 0, -1);
glMatrixMode(GL_MODELVIEW);
while (!Display.isCloseRequested()) {
// clear the screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the color of the quad (R,G,B,A)
glColor3f(0.5f, 0.5f, 1.0f);
// draw quad
glBegin(GL_QUADS);
glVertex3f(0, 0, 10);
glVertex3f(0, 1000, 10);
glVertex3f(1000, 1000, 10);
glVertex3f(1000, 0, 10);
glEnd();
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) throws LWJGLException {
main displayExample = new main();
displayExample.start();
}
}
答案 0 :(得分:0)
我可能对视图做错了。我设法制作了一个正常运行的3D立方体。键Q,W,E分别在眼点的x,y和z坐标上添加数字(我没有考虑FPS,所以你可以调整 eye * + = 0.1 )和A,S和D分散了x,y,z的数字。
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
public class main {
float eyex, eyey, eyez;
public void start() throws LWJGLException {
int height = 600;
int width = 800;
eyex = 0;
eyey = 0;
eyez = 0;
DisplayMode standard = new DisplayMode(width, height);
Display.setDisplayMode(standard);
Display.create();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(90f, (float) width / (float) height, 1f, 10000f);
glMatrixMode(GL_MODELVIEW);
while (!Display.isCloseRequested()) {
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, 50f, 50f, 50f, 0f, 0f, 1f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f( 100,0,0);
glVertex3f( 100, 100,0);
glVertex3f(0, 100,0);
glColor3f(0,100,0);
glVertex3f(0,0,0);
glVertex3f(0,0, 100);
glVertex3f(0, 100, 100);
glVertex3f(0, 100,0);
glColor3f(0,0,100);
glVertex3f( 100,0, 100);
glVertex3f( 100,0,0);
glVertex3f( 100, 100,0);
glVertex3f( 100, 100, 100);
glColor3f(1,1,0);
glVertex3f(0,0,0);
glVertex3f( 100,0,0);
glVertex3f( 100,0, 100);
glVertex3f(0,0, 100);
glColor3f(0,1,1);
glVertex3f(0,0, 100);
glVertex3f( 100,0, 100);
glVertex3f( 100, 100, 100);
glVertex3f(0, 100, 100);
glColor3f(1,1,1);
glVertex3f(0, 100, 100);
glVertex3f(0, 100,0);
glVertex3f( 100, 100,0);
glVertex3f( 100, 100, 100);
glEnd();
pollInput();
Display.update();
}
Display.destroy();
}
void pollInput() {
if (Mouse.isButtonDown(0)) {
int x = Mouse.getX();
int y = Mouse.getY();
System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
System.out.println("SPACE KEY IS DOWN");
}
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
eyex += 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
eyey += 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
eyez += 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
eyex -= 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
eyey -= 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
eyez -= 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
eyex = 0;
eyey = 0;
eyez = 0;
}
}
public static void main(String[] argv) throws LWJGLException {
main displayExample = new main();
displayExample.start();
}
}