gpt4 book ai didi

java - 一个 VBO 中有多个 Cube

转载 作者:行者123 更新时间:2023-11-30 04:05:03 26 4
gpt4 key购买 nike

所以,我已经想出了如何实现这一点。我的阵列看起来很困惑。这个问题很难解释,所以我提供一些图片和代码。

编辑:问题是 for 循环和 x1、y1、z1 变量由 float 控制

public void create() {
setup();
render();
}

private void makeCube() {
texture = glGenBuffers();
cube = glGenBuffers();

FloatBuffer cubeBuffer;
FloatBuffer textureBuffer;

ArrayList<Float> cubeList = new ArrayList<>();
ArrayList<Float> textureList = new ArrayList<>();

float[] cubeFloat;
float[] textureFloat;

for (float x1 = 0; x1 < tileSize * width; x1 += tileSize) {
for (float y1 = 0; y1 < tileSize * height; y1 += tileSize) {
for (float z1 = -tileSize * depth; z1 < 0; z1 += tileSize) {
float highX = x1 + tileSize;
float highY = y1 + tileSize;
float highZ = z1 + tileSize;

float[] cubeData = new float[]{
/*Front Face*/
x1, y1, z1,
highX, y1, z1,
highX, highY, z1,
x1, highY, z1,
/*Back Face*/
x1, y1, highZ,
highX, y1, highZ,
highX, highY, highZ,
x1, highY, highZ,
/*Left Face*/
x1, y1, z1,
x1, y1, highZ,
x1, highY, highZ,
x1, highY, z1,
/*Right Face*/
highX, y1, z1,
highX, y1, highZ,
highX, highY, highZ,
highX, highY, z1,
/*Bottom Face*/
x1, y1, z1,
x1, y1, highZ,
highX, y1, highZ,
highX, y1, z1,
/*Top Face*/
x1, highY, z1,
x1, highY, highZ,
highX, highY, highZ,
highX, highY, z1};

for (float f : cubeData) {
cubeList.add(f);
}
}
}
}

cubeFloat = compileFloat(cubeList);

for (int i = 0; i < (width * height * depth); i++) {
float[] textureData = new float[]{
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1};

for (Float f : textureData) {
textureList.add(f);
}
}

textureFloat = compileFloat(textureList);

textureBuffer = asFloatBuffer(textureFloat);
glBindBuffer(GL_ARRAY_BUFFER, texture);
glBufferData(GL_ARRAY_BUFFER, textureBuffer, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

cubeBuffer = asFloatBuffer(cubeFloat);
glBindBuffer(GL_ARRAY_BUFFER, cube);
glBufferData(GL_ARRAY_BUFFER, cubeBuffer, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

private float[] compileFloat(ArrayList<Float> list) {
float[] f = new float[list.size()];

for (int i = 0; i < list.size(); i++) {
f[i] = list.get(i);
}

return f;
}

private void renderCube() {
textures.get(0).bind();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, cube);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, texture);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

glDrawArrays(GL_QUADS, 0, 24 * (width * height * depth));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

private void render() {
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();

renderCube();

Display.update();
Display.sync(30);
}

Display.destroy();
System.exit(0);
}

private void setup() {
try {
Display.setDisplayMode(new DisplayMode(frameWidth, frameHeight));
Display.setTitle("3D Project");
Display.setVSyncEnabled(vSync);
Display.create();
} catch (LWJGLException ex) {
Logger.getLogger(Camera.class.getName()).log(Level.SEVERE, null, ex);
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
//glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glLoadIdentity();

loadTextures();

makeCube();

}

制作 3x3x1 数组会产生以下结果:

3x3x1

尽管制作 2x2x1 数组效果非常好。 2x2x1

所以我认为这是出了问题,我不知道如何解决它。 (这应该是 5x5x1. Problem

最佳答案

感谢您解释 tileSize 使用的值。值 0.1 无法以任何精度的浮点表示,它退化为以 2 为基数的重复分数,就像每个人都熟悉以 10 为基数的 1/3 一样。现在,如果您继续将这个不完全是0.1的数字添加到某个东西上,最终结果将与您的预期明显不同。

一般来说,最好避免使用浮点表达式进行循环控制。您可能会认为,因为编译器很乐意接受诸如 0.1 这样的浮点常量,所以一切都很好,并且它可以准确地表示该数字,但如果您不了解 float 学的机制那么像这样奇怪的事情就会发生。

如果您好奇,可以找到我提到的问题的更详细解释 here .

关于java - 一个 VBO 中有多个 Cube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20986180/

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