PDA

View Full Version : Color in Sphere



Phinehas
09-27-2010, 05:04 AM
Hi,

I try to create a sphere based on the tutorial http://learningwebgl.com/blog/?p=1253. (http://learningwebgl.com/blog/?p=1253) Since I want to use color instead of texture. I try to update a little bit (in red)and set the color to red. However, the result is not what I expected. The color is not red. It shows the multi-color in sphere. Is there any mistakes in my coding?

The following is my coding:

function initBuffers() {
var latitudeBands = 30;
var longitudeBands = 30;
var radius = 2;

var vertexPositionData = [];
var normalData = [];
var vertexColorData = [];
for (var latNumber = 0; latNumber <= latitudeBands; latNumber++) {
var theta = latNumber * Math.PI / latitudeBands;
var sinTheta = Math.sin(theta);
var cosTheta = Math.cos(theta);

for (var longNumber = 0; longNumber <= longitudeBands; longNumber++) {
var phi = longNumber * 2 * Math.PI / longitudeBands;
var sinPhi = Math.sin(phi);
var cosPhi = Math.cos(phi);

var x = cosPhi * sinTheta;
var y = cosTheta;
var z = sinPhi * sinTheta;
var u = 1 - (longNumber / longitudeBands);
var v = 1 - (latNumber / latitudeBands);

normalData.push(x);
normalData.push(y);
normalData.push(z);
vertexColorData = vertexColorData.concat("1.0, 0.0, 0.0, 1.0");
vertexPositionData.push(radius * x);
vertexPositionData.push(radius * y);
vertexPositionData.push(radius * z);
}
}
var indexData = [];
for (var latNumber = 0; latNumber < latitudeBands; latNumber++) {
for (var longNumber = 0; longNumber < longitudeBands; longNumber++) {
var first = (latNumber * (longitudeBands + 1)) + longNumber;
var second = first + longitudeBands + 1;
indexData.push(first);
indexData.push(second);
indexData.push(first + 1);

indexData.push(second);
indexData.push(second + 1);
indexData.push(first + 1);
}
}

vertexNormalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normalData), gl.STATIC_DRAW);
vertexNormalBuffer.itemSize = 3;
vertexNormalBuffer.numItems = normalData.length / 3;

vertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexColorData), gl.STATIC_DRAW);
vertexColorBuffer.itemSize = 4;
vertexColorBuffer.numItems = vertexColorData.length / 3;

vertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexPositionData), gl.STATIC_DRAW);
vertexPositionBuffer.itemSize = 3;
vertexPositionBuffer.numItems = vertexPositionData.length / 3;

vertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), gl.STREAM_DRAW);
vertexIndexBuffer.itemSize = 1;
vertexIndexBuffer.numItems = indexData.length;
}


function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0);
loadIdentity();

mvTranslate([0, 0.0, z]);
mvRotate(xRot, [0, 1, 0]);
mvRotate(yRot, [1, 0, 0]);

mvPushMatrix();
mvTranslate([0, 0, 0]);

gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositio nAttribute, vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAt tribute, vertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalA ttribute, vertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, vertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

mvPopMatrix();
}

strattonbrazil
09-27-2010, 07:01 AM
I don't think you're calculating the color item size correctly. If you're using three XYZ values for position, you divide by three for the number of items. Now if you're working with RGBA values (alpha included), why wouldn't you divide by four? I believe OpenGL is misaligning your colors.

You have this for your colors, which says make it red (1,0,0,1) for every four vertices, but you say use three channels, so the first one is red (1,0,0), the second one is (1,1,0), etc.

1,0,0,1,1,0,0,1,etc.

Just for readability, you might want to change every numItems from this


vertexNormalBuffer.itemSize = 3;
vertexNormalBuffer.numItems = normalData.length / 3;

to


vertexNormalBuffer.itemSize = 3;
vertexNormalBuffer.numItems = normalData.length / vertexNormalBuffer.itemSize;

Phinehas
09-27-2010, 07:15 AM
Does it mean I should change "vertexColorBuffer.numItems = vertexColorData.length / 3" to "vertexColorBuffer.numItems = vertexColorData.length / 4"? If I do it, the result is the same as before.

And I found one crazy problem, if I comment "vertexColorData = vertexColorData.concat("1.0, 0.0, 0.0, 1.0");", it also can fill the multi-color in the sphere. Are my coding of vertexColorData and its buffer wrong?

Phinehas
09-29-2010, 02:17 AM
I still cannot create a sphere. Can anyone help me to solve this problem?

Phinehas
09-29-2010, 06:20 PM
Hi,

I try to comment some buffer to see the result. I am wondering why I comment the color buffer but it still can generate the multi-color sphere (attachment). Can anyone help me to solve the problem?


gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositio nAttribute, vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

// gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
// gl.vertexAttribPointer(shaderProgram.vertexColorAt tribute, vertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalA ttribute, vertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, vertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);