How to add colors or normals to custom object?
Posted by Chaz on Feb 22, 2015; 2:45pm
URL: https://forum.jogamp.org/How-to-add-colors-or-normals-to-custom-object-tp4034048.html
Yo. I have next task: load heightmap and visualize it like a 3D object. Actually I already did it using simple GL_ARRAY_BUFFER and DrawArrays.
But my image totally has not any normals, or seems that all normals are the same. (Lighting is enabled).
I tried example where was used glVertexAttribPointer, but i got fatal error when try assign color's data.
Here is my 3 main functions.
public void CreateBuffer(GL2 gl) // creating buffers with vertex and color data
{
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, cBuff.capacity() * Buffers.SIZEOF_FLOAT, cBuff, GL.GL_STATIC_DRAW); // here I got exception (fatal error)
CreateVAO();
}
public void CreateVAO() // creating something that should help me to colorize my object
{
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.glEnableVertexAttribArray(0);
gl.glEnableVertexAttribArray(1);
gl.glBindVertexArray(0);
gl.glDrawArrays(GL2.GL_QUADS, 0, fBuff.capacity() / 3);
gl.glDisableVertexAttribArray(0);
gl.glDisableVertexAttribArray(1);
}
So what I do wrong?
UPD: Actually I understood where is was 1 of my faults - i have to call rewind() for cBuff. And I had change Draw function to this
public void Draw(){
gl.glBindVertexArray(1);
gl.glEnableVertexAttribArray(0);
gl.glEnableVertexAttribArray(1);
gl.glDrawArrays(GL2.GL_QUADS, 0, fBuff.capacity() / 3);
gl.glDisableVertexAttribArray(0);
gl.glDisableVertexAttribArray(1);
}
Now I don't got exceptions, but I still don't see colors on my object. Here is function in which I initialize and filling the buffers.
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);
cBuff = Buffers.newDirectFloatBuffer(resolution*resolution*3*4/(int)optiStep);
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); cBuff.put(1);
fBuff.put(y1); cBuff.put(0);
fBuff.put(z1); cBuff.put(0);
fBuff.put(x2); cBuff.put(1);
fBuff.put(y2); cBuff.put(0);
fBuff.put(z2); cBuff.put(0);
fBuff.put(x3); cBuff.put(1);
fBuff.put(y3); cBuff.put(0);
fBuff.put(z3); cBuff.put(0);
fBuff.put(x4); cBuff.put(1);
fBuff.put(y4); cBuff.put(0);
fBuff.put(z4); cBuff.put(0);
}
}
fBuff.rewind();
cBuff.rewind();
}