Posted by
Chaz on
Feb 23, 2015; 12:53pm
URL: https://forum.jogamp.org/How-to-add-colors-or-normals-to-custom-object-tp4034048p4034054.html
No no no! I was wrong. I saw shape of the terrain just because my light was in point mode with light fading. And different parts of shape has different colours. But my normals still does not works.
In your example I see shaders and do not see any VBO and DrawArray call so I guess it useful for me.
Now I changed my code to this.
private void Import(String path) throws Exception
{
byte[] bytes = Files.readAllBytes(Paths.get(path));
int resolution = (int)Math.sqrt(bytes.length/4);
float[] floats=new float[resolution*resolution];
ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder()).asFloatBuffer().get(floats);
this.heightStickLength=this.widthStickLength=resolution;
this.heightScale=1;
minHeight=Float.MAX_VALUE;
maxHeight = Float.MIN_VALUE;
for(int i = 0; i<floats.length; i++)
{
floats[i]=floats[i]*600f;
}
for(float f : floats)
{
if(f>maxHeight) maxHeight=f;
if(f<minHeight) minHeight=f;
}
CreateColliderShape(floats);
System.out.println("Max height: "+maxHeight+" Min height: "+minHeight);
float step = 2000/513f;
float optiStep=1;
fBuff = Buffers.newDirectFloatBuffer(resolution*resolution*3*4/(int)optiStep);
nBuff = Buffers.newDirectFloatBuffer(fBuff.capacity()/3);
for(int i =0; i<resolution-1; i+=optiStep)
{
for(int j =0; j<resolution-1; j+=optiStep)
{
float x1=i*step,y1=floats[(j)*resolution+(resolution-1-i)],z1=j*step;
float x2=i*step+step,y2=floats[(j)*resolution+(resolution-2-i)],z2=j*step;
float x3=i*step+step,y3=floats[(j+1)*resolution+(resolution-2-i)],z3=j*step+step;
float x4=i*step,y4=floats[(j+1)*resolution+(resolution-1-i)],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);
float n = 0.5f;
nBuff.put(n);
nBuff.put(n);
nBuff.put(n);
nBuff.put(n);
}
}
fBuff.rewind();
nBuff.rewind();
}
public void CreateBuffer(GL2 gl)
{
this.gl=gl;
targetsBuffer = Buffers.newDirectIntBuffer(2);
gl.glGenBuffers(2, targetsBuffer);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, targetsBuffer.get(0));
gl.glBufferData(GL.GL_ARRAY_BUFFER, fBuff.capacity() * Buffers.SIZEOF_FLOAT, fBuff, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, targetsBuffer.get(1));
gl.glBufferData(GL.GL_ARRAY_BUFFER, nBuff.capacity() * Buffers.SIZEOF_FLOAT, nBuff, GL.GL_STATIC_DRAW);
CreateVAO();
}
public void CreateVAO()
{
IntBuffer id = Buffers.newDirectIntBuffer(1);
gl.glGenVertexArrays(1,id);
gl.glBindVertexArray(id.get(0));
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, targetsBuffer.get(0));
gl.glVertexAttribPointer(0, 3, GL2.GL_FLOAT,false,0,0);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,targetsBuffer.get(1));
gl.glVertexAttribPointer(1, 3, GL2.GL_FLOAT,false,0,0);
}
@Override
public void Draw(){
gl.glEnable(GL2.GL_NORMALIZE);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, targetsBuffer.get(0));
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,targetsBuffer.get(1));
gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, fBuff.capacity()/3);
}
As you can see, I don't calculate any normals, just assign the same normal for each vertex. But it looks strange. Because normals did not assigned for some part of terrain.
Watch screenshot please.
http://puu.sh/g9eFY/54d10e7f98.png