private void Import(String path) throws Exception
{
byte[] bytes = Files.readAllBytes(Paths.get(path)); //loading heightmap from binary file
int resolution = (int)Math.sqrt(bytes.length/4); //figure out dimension of map (data has float type, that's why here is 4)
float[] floats=new float[resolution*resolution]; //initialize an array that will contain the heights
ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder()).asFloatBuffer().get(floats); // transform byte array to float array
for(int i = 0; i<floats.length; i++)
{
floats[i]=floats[i]*600f; // denormalize heights (max height == 600)
}
float step = 100/513f; //find a distance to be between points 100 - a map width, and 513 - the number of heights in its
float optiStep=1; //some optimization~..
fBuff = Buffers.newDirectFloatBuffer(resolution*resolution*3*4/(int)optiStep); //create an array that will contain the coordinates of the vertices of quads
for(int i =0; i<resolution-1; i+=optiStep)
{
for(int j =0; j<resolution-1; j+=optiStep) // some calculations
{
float x1=i*step,y1=floats[i*resolution+j],z1=j*step;
float x2=i*step+step,y2=floats[(i+1)*resolution+j],z2=j*step;
float x3=i*step+step,y3=floats[(i+1)*resolution+(j+1)],z3=j*step+step;
float x4=i*step,y4=floats[i*resolution+(j+1)],z4=j*step+step;
fBuff.put(x1); // пихаю координаты вертексов квадов в тот массив
fBuff.put(y1);
fBuff.put(z1);
fBuff.put(x2);
fBuff.put(y2);
fBuff.put(z2);
fBuff.put(x3);
fBuff.put(y3);
fBuff.put(z3);
fBuff.put(x4);
fBuff.put(y4);
fBuff.put(z4);
}
}
fBuff.rewind(); // translate carriage to zero position
}
之后我必须在视频卡上创建一个缓冲区。
public void CreateBuffer(GL2 gl)
{
this.gl=gl;
targetsBuffer = Buffers.newDirectIntBuffer(1);
gl.glGenBuffers(1, targetsBuffer);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, targetsBuffer.get(0));
gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
gl.glBufferData(GL.GL_ARRAY_BUFFER, fBuff.capacity()*Buffers.SIZEOF_FLOAT, fBuff, GL.GL_STATIC_DRAW);
gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
}
然后我创建了一个方法,它将在main函数中调用以显示内容。
public void Draw(){
gl.glBindBuffer(GL.GL_ARRAY_BUFFER,targetsBuffer.get(0));
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0 );
gl.glBindBuffer(GL.GL_ARRAY_BUFFER,0);
gl.glDrawArrays(GL2.GL_QUADS, 0, fBuff.capacity() / 3);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}
之后我可以看到所需的物体图像。但是我需要顺时针旋转这个物体。我想,我需要在数组构建周期中改变一些东西,但没有任何帮助。 那么我需要做些什么来旋转我的物体呢?
答案 0 :(得分:0)
如果您的网格方向如下所示
x1,y1,z1 x2,y2,z2
X4,Y4,Z4。 X3,Y3,Z3
你想要顺时针旋转90度,你只需要以不同的方式分配值
X4,Y4,Z4。 X1,Y1,Z1
X3,Y3,Z3。 x2,y2,z2