Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl 高度贴图生成完成一半_Opengl_Perlin Noise - Fatal编程技术网

Opengl 高度贴图生成完成一半

Opengl 高度贴图生成完成一半,opengl,perlin-noise,Opengl,Perlin Noise,目前,我正在尝试制作一个噪声生成的高度图,并用opengl显示它。我在跟踪,但我的高度图似乎不起作用。它似乎只产生(或显示)它应该产生的一半 这是具有颜色法线的高度贴图: 正如你所看到的,虽然这应该是一个正方形,但它看起来是矩形的,有一条未完成的边 这是我的高度图生成代码: public class HeightMap extends GameModel { private static final float START_X = -0.5f; private static

目前,我正在尝试制作一个噪声生成的高度图,并用opengl显示它。我在跟踪,但我的高度图似乎不起作用。它似乎只产生(或显示)它应该产生的一半

这是具有颜色法线的高度贴图:

正如你所看到的,虽然这应该是一个正方形,但它看起来是矩形的,有一条未完成的边

这是我的高度图生成代码:

public class HeightMap extends GameModel {

    private static final float START_X = -0.5f;
    private static final float START_Z = -0.5f;

    public HeightMap(float minY, float maxY, float persistence, int width, int height) {
        super(createMesh(minY, maxY, persistence, width, height));
    }

    protected static Mesh createMesh(final float minY, final float maxY, final float persistence, final int width,
            final int height) {
        SimplexNoise noise = new SimplexNoise(128, persistence, 2);// Utils.getRandom().nextInt());

        float xStep = Math.abs(START_X * 2) / width;
        float zStep = Math.abs(START_Z * 2) / height;

        List<Float> positions = new ArrayList<>();
        List<Integer> indices = new ArrayList<>();

        for (int x = 0; x < width; x++) {
            for (int z = 0; z < height; z++) {
                // scale from [-0.5, 0.5] to [minY, maxY]
                float heightY = (float) ((noise.getNoise(x, z) + 0.5f) * (maxY - minY) + minY);

                positions.add(START_X + x * xStep);
                positions.add(heightY);
                positions.add(START_Z + z * zStep);

                // Create indices
                if (x < width - 1 && z < height - 1) {
                    int leftTop = z * width + x;
                    int leftBottom = (z + 1) * width + x;
                    int rightBottom = (z + 1) * width + x + 1;
                    int rightTop = z * width + x + 1;

                    indices.add(leftTop);
                    indices.add(leftBottom);
                    indices.add(rightTop);

                    indices.add(rightTop);
                    indices.add(leftBottom);
                    indices.add(rightBottom);
                }
            }
        }

        float[] verticesArr = Utils.listToArray(positions);
        float[] colorArr = new float[positions.size()];
        for (int i = 0; i < colorArr.length; i += 3) {
            colorArr[i] = (float) i / colorArr.length;
            colorArr[i + 1] = (float) .25f;
            colorArr[i + 2] = (float) 0;
        }
        int[] indicesArr = indices.stream().mapToInt((i) -> i).toArray();

        float[] normalArr = calcNormals(verticesArr, width, height);

        return new Mesh(verticesArr, colorArr, normalArr, indicesArr);
    }

    private static float[] calcNormals(float[] posArr, int width, int height) {
        Vector3f v0 = new Vector3f();
        Vector3f v1 = new Vector3f();
        Vector3f v2 = new Vector3f();
        Vector3f v3 = new Vector3f();
        Vector3f v4 = new Vector3f();
        Vector3f v12 = new Vector3f();
        Vector3f v23 = new Vector3f();
        Vector3f v34 = new Vector3f();
        Vector3f v41 = new Vector3f();
        List<Float> normals = new ArrayList<>();
        Vector3f normal = new Vector3f();
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (row > 0 && row < height - 1 && col > 0 && col < width - 1) {
                    int i0 = row * width * 3 + col * 3;
                    v0.x = posArr[i0];
                    v0.y = posArr[i0 + 1];
                    v0.z = posArr[i0 + 2];

                    int i1 = row * width * 3 + (col - 1) * 3;
                    v1.x = posArr[i1];
                    v1.y = posArr[i1 + 1];
                    v1.z = posArr[i1 + 2];
                    v1 = v1.sub(v0);

                    int i2 = (row + 1) * width * 3 + col * 3;
                    v2.x = posArr[i2];
                    v2.y = posArr[i2 + 1];
                    v2.z = posArr[i2 + 2];
                    v2 = v2.sub(v0);

                    int i3 = (row) * width * 3 + (col + 1) * 3;
                    v3.x = posArr[i3];
                    v3.y = posArr[i3 + 1];
                    v3.z = posArr[i3 + 2];
                    v3 = v3.sub(v0);

                    int i4 = (row - 1) * width * 3 + col * 3;
                    v4.x = posArr[i4];
                    v4.y = posArr[i4 + 1];
                    v4.z = posArr[i4 + 2];
                    v4 = v4.sub(v0);

                    v1.cross(v2, v12);
                    v12.normalize();

                    v2.cross(v3, v23);
                    v23.normalize();

                    v3.cross(v4, v34);
                    v34.normalize();

                    v4.cross(v1, v41);
                    v41.normalize();

                    normal = v12.add(v23).add(v34).add(v41);
                    normal.normalize();
                } else {
                    normal.x = 0;
                    normal.y = 1;
                    normal.z = 0;
                }
                normal.normalize();
                normals.add(normal.x);
                normals.add(normal.y);
                normals.add(normal.z);
            }
        }
        return Utils.listToArray(normals);
    }

}
public class HeightMap扩展了游戏模型{
专用静态最终浮点起始值X=-0.5f;
专用静态最终浮点数开始_Z=-0.5f;
公共高度映射(浮点最小值、浮点最大值、浮点持久性、整型宽度、整型高度){
super(createMesh(minY、maxY、persistence、width、height));
}
受保护的静态网格createMesh(最终浮点minY、最终浮点maxY、最终浮点持久性、最终整型宽度、,
最终整数高度){
SimplexNoise noise=新的SimplexNoise(128,持久性,2);//Utils.getRandom().nextInt());
float xStep=Math.abs(START_X*2)/width;
float zStep=Math.abs(START_Z*2)/高度;
列表位置=新的ArrayList();
列表索引=新的ArrayList();
对于(int x=0;xi.toArray();
float[]normalArr=calcNormals(垂直arr、宽度、高度);
返回新网格(verticesArr、colorArr、normalArr、IndicateSarr);
}
专用静态浮点[]calcNormals(浮点[]posArr,整型宽度,整型高度){
向量3f v0=新向量3f();
Vector3f v1=新Vector3f();
Vector3f v2=新Vector3f();
Vector3f v3=新Vector3f();
Vector3f v4=新Vector3f();
Vector3f v12=新Vector3f();
Vector3f v23=新Vector3f();
Vector3f v34=新Vector3f();
Vector3f v41=新Vector3f();
列表法线=新的ArrayList();
向量3f normal=新向量3f();
对于(int row=0;row0&&行<高度-1&&列>0&&列<宽度-1){
int i0=行*宽*3+列*3;
v0.x=posArr[i0];
v0.y=posArr[i0+1];
v0.z=posArr[i0+2];
int i1=行*宽*3+(列-1)*3;
v1.x=posArr[i1];
v1.y=posArr[i1+1];
v1.z=posArr[i1+2];
v1=v1.sub(v0);
int i2=(行+1)*宽度*3+列*3;
v2.x=posArr[i2];
v2.y=posArr[i2+1];
v2.z=posArr[i2+2];
v2=v2.sub(v0);
int i3=(行)*宽*3+(列+1)*3;
v3.x=posArr[i3];
v3.y=posArr[i3+1];
v3.z=posArr[i3+2];
v3=v3.sub(v0);
INTI4=(第1行)*宽度*3+列*3;
v4.x=posArr[i4];
v4.y=posArr[i4+1];
v4.z=posArr[i4+2];
v4=v4.sub(v0);
v1.交叉(v2,v12);
v12.normalize();
v2.交叉(v3,v23);
v23.normalize();
v3.交叉(v4,v34);
v34.normalize();
v4.交叉(v1,v41);
v41.normalize();
normal=v12.add(v23.add(v34.add)(v41);
normal.normalize();
}否则{
正常。x=0;
正常,y=1;
正常,z=0;
}
normal.normalize();
法线。添加(normal.x);
normals.add(normal.y);
法线.add(normal.z);
}
}
返回Utils.listToArray(法线);
}
}

胡乱猜测:您使用的是16位索引,但指定索引缓冲区的大小,就像它是8位索引一样(通过将缓冲区的大小设置为缓冲区中的元素数)

从屏幕截图上判断,显然看起来只有上半部分被绘制,所以我假设问题在于索引缓冲区或绘制调用的创建

也许你在抽签调用中使用了错误的索引数?就像你用了三角形的数目?或者假设三角形风扇的编号而不是三角形

为了获得更多信息,您必须发布实际的绘制调用和缓冲区对象的生成

或者至少是助教