gpt4 book ai didi

java - 测试了一些 LWJGL OpenGL,不会生成盒子。我应该怎么办?

转载 作者:行者123 更新时间:2023-12-01 14:01:44 25 4
gpt4 key购买 nike

可以在 OpenGL ES 1.0 和 2.0 上运行。

目标:制作一个盒子,然后显示它

在 Win 7 上无法与 LWJGL 一起使用。加载一个绿色框(应该如此),然后开始显示一堆非常粗的白线,这些线不会保留为框。有很多闪烁。这是一张图片。

enter image description here

这是 Windows 的代码。

Main.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();
}
}
}

}

Box.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;
}

}

最佳答案

我将演示如何使用 VBO 和 IBO 创建、渲染和处置 VAO,而不只是向您展示您在评论中要求的内容。

  • VAO <=> 顶点数组对象
  • VBO <=> 顶点缓冲区对象
  • IBO <=>索引缓冲区对象

创建 VAO、VBO 和 IBO

vao_handlevbo_handleibo_handle是3个包含id/handle的整数,这3个变量在下面的整个代码中使用。

  • vbo_data <=> 包含顶点的FloatBuffer
  • ibo_data <=> 包含索引的 IntBuffer

下面的代码中使用了上述两个变量。

// Creating the VBO
vbo_handle = glGenBuffers();

glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);
glBufferData(GL_ARRAY_BUFFER, vbo_data, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Creating the IBO
ibo_handle = glGenBuffers();

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_handle );
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ibo_data, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// Creating the VAO
vao_handle = glGenVertexArrays();

glBindVertexArray(vao_handle);

glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);

glEnableVertexAttribArray(INDEX); // Place your own INDEX value in the parenthesis
glVertexAttribPointer(INDEX, SIZE, TYPE, NORMALIZED, STRIDE, OFFSET); // Replace all the VARIABLES with the values which fit to your VAO, VBO and IBO

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_handle);

glBindVertexArray(0);

/*
* Remember that the INDEX given in the
* glEnableVertexAttribArray() and in
* glVertexAttribPointer() is the same
* INDEX used in Shaders (GLSL)
*
* If the INDEX is 0 then in GLSL it
* should look like this
* GLSL: layout(location = 0) in vec3 position;
*
* ^ we can ignore this if you aren't
* using Shaders, though keep it in mind
* since we might need it in the future
*/

渲染 VAO

glBindVertexArray(vao_handle);
glDrawElements(MODE, SIZE, TYPE, OFFSET); // Again replace the variables, so it fits to your VAO, VBO and IBO
glBindVertexArray(0);

处置 VAO、VBO 和 IBO

这就是删除不同缓冲区的方法,当您关闭程序或在某个时候不再需要它们时,这是一件好事。

glDeleteVertexArrays(vao_handle); // Deletes the VAO
glDeleteBuffers(vbo_handle); // Deletes the VBO
glDeleteBuffers(ibo_handle); // Deletes the IBO

vao_handle = -1;
vbo_handle = -1;
ibo_handle = -1;

关于java - 测试了一些 LWJGL OpenGL,不会生成盒子。我应该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306621/

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