在 OpenGL ES 1.0 和 2.0 上工作。
目标:制作一个盒子,然后展示它
在 Win 7 上无法与 LWJGL 一起使用。加载一个绿色框(应该如此),然后开始显示一堆非常粗的白线,这些白线不会保持为框。有很多闪烁。这是一张图片。
这是windows的代码。
主.java
package play.box;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Main {
public static final boolean VSYNC = true;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final boolean FULLSCREEN = false;
protected boolean running = false;
public static void main(String[] args) {
try {
start();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public static void start() throws LWJGLException {
Display.setTitle("Display example");
Display.setResizable(false);
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setVSyncEnabled(VSYNC);
Display.setFullscreen(FULLSCREEN);
Display.create();
// Setup OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(-3, 3, -2, 2, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
new Renderer().run();
}
}
渲染器.java
package play.box;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Renderer implements Runnable {
public Renderer() {
}
@Override
public void run() {
while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
// Rendering //
/*GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex2f(0.0f, 1.0f);
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex2f(1.0f, 1.0f);
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glVertex2f(1.0f, -1.0f);
GL11.glEnd();*/
Box box = new Box();
box.draw();
// End of Rendering //
Display.update();
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
盒子.java
package play.box;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import org.lwjgl.opengl.GL11;
public class Box {
private float verticies[] = {
-1.0f, 1.0f, // Left Top (0)
-1.0f, -1.0f, // Left Bottom (1)
1.0f, -1.0f, // Right Bottom (2)
1.0f, 1.0f // Right Top (4)
};
private short indicies[] = {
0, 1, 2,
2, 3, 0
};
private FloatBuffer vertBuff;
private ShortBuffer indexBuff;
public Box() {
this.setupBuffers();
}
private void setupBuffers() {
ByteBuffer bBuff = ByteBuffer.allocateDirect(this.verticies.length * 4);
bBuff.order(ByteOrder.nativeOrder());
this.vertBuff = bBuff.asFloatBuffer();
this.vertBuff.put(this.verticies);
this.vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(this.indicies.length * 2);
pbBuff.order(ByteOrder.nativeOrder());
this.indexBuff = pbBuff.asShortBuffer();
this.indexBuff.put(this.indicies);
this.indexBuff.position(0);
}
public void draw() {
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glVertexPointer(2, GL11.GL_FLOAT, this.vertBuff);
GL11.glDrawElements(GL11.GL_TRIANGLES, this.indexBuff);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
}
}
更新代码:
package play.box;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Box {
private float verticies[] = {
-1.0f, 1.0f, // Left Top (0)
-1.0f, -1.0f, // Left Bottom (1)
1.0f, -1.0f, // Right Bottom (2)
1.0f, 1.0f // Right Top (4)
};
private short indicies[] = {
0, 1, 2,
2, 3, 0
};
private FloatBuffer vertBuff;
private ShortBuffer indexBuff;
private int vbo_handle;
private int ibo_handle;
private int vao_handle;
private String vShaderStr =
"attribute vec4 vPosition; \n"
+ "void main() { \n"
+ " gl_Position = vPosition;\n"
+ "} \n";
private String fShaderStr =
"precision mediump float; \n"
+ "void main() { \n"
+ " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
+ "} \n";
private int vertexShader;
private int fragmentShader;
private int programObject;
public Box() {
}
public void create() {
ByteBuffer bBuff = ByteBuffer.allocateDirect(this.verticies.length * 4);
bBuff.order(ByteOrder.nativeOrder());
this.vertBuff = bBuff.asFloatBuffer();
this.vertBuff.put(this.verticies);
//this.vertBuff.flip();
this.vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(this.indicies.length * 2);
pbBuff.order(ByteOrder.nativeOrder());
this.indexBuff = pbBuff.asShortBuffer();
this.indexBuff.put(this.indicies);
//this.indexBuff.flip();
this.indexBuff.position(0);
// Create VBO
this.vbo_handle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.vertBuff, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
// Create IBO
this.ibo_handle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, this.indexBuff, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
// Create VAO
this.vao_handle = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao_handle);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_handle);
GL20.glEnableVertexAttribArray(0);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo_handle);
GL30.glBindVertexArray(0);
// Setup Shaders
this.vertexShader = this.loadShader(GL20.GL_VERTEX_SHADER, this.vShaderStr);
this.fragmentShader = this.loadShader(GL20.GL_FRAGMENT_SHADER, this.fShaderStr);
// Setup Program
int program = GL20.glCreateProgram();
if(program == 0) {
return;
}
GL20.glAttachShader(program, this.vertexShader);
GL20.glAttachShader(program, this.fragmentShader);
GL20.glBindAttribLocation(program, 0, "vPosition");
GL20.glLinkProgram(program);
if(GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) == 0) {
System.out.println("Error Creating Program: " + GL20.glGetProgramInfoLog(program, Integer.MAX_VALUE));
GL20.glDeleteProgram(program);
return;
}
this.programObject = program;
}
public void draw() {
this.create();
GL20.glUseProgram(this.programObject);
GL30.glBindVertexArray(vao_handle);
GL11.glDrawElements(GL11.GL_TRIANGLES, 2, GL11.GL_FLOAT, 0);
GL30.glBindVertexArray(0);
this.dispose();
}
public void draw(boolean useVAO) {
if(useVAO) {
this.draw();
} else {
this.create();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle);
GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDrawElements(GL11.GL_TRIANGLES, this.indicies.length, GL11.GL_UNSIGNED_SHORT, 0L);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
this.dispose();
}
}
public void dispose() {
GL30.glDeleteVertexArrays(vao_handle);
GL30.glDeleteVertexArrays(vbo_handle);
GL30.glDeleteVertexArrays(ibo_handle);
this.vao_handle = -1;
this.vbo_handle = -1;
this.ibo_handle = -1;
}
private int loadShader(int type, String shaderSrc) {
int shader;
shader = GL20.glCreateShader(type);
if(shader == 0) {
return 0;
}
GL20.glShaderSource(shader, shaderSrc);
GL20.glCompileShader(shader);
if(GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == 0) {
System.out.println("Error Loading Shader: " + GL20.glGetShaderInfoLog(shader, Integer.MAX_VALUE));
GL20.glDeleteShader(shader);
return 0;
}
return shader;
}
}