Hello,
I’ve been experimenting with lighthing on a surface mesh made with ofVbo (I need to use such object to modify vertices and colours on compute shaders for a project) and while experimenting I realised that there might be some problem with how lighting is calculated.
Take the following basic example, just a plane mesh that I make via the following for loop, setting up normals, indices and colours:
originalColor = ofFloatColor::yellowGreen;
myIndices = new ofIndexType[myWidth * myHeight * 6];
int iteratorIndices = 0;
for (int y = 0; y < myHeight; y += 1) {
for (int x = 0; x < myWidth; x += 1) {
myVertices.push_back((float)x - myWidth / 2);
myVertices.push_back((float)y - myHeight / 2);
myVertices.push_back((float)0);
myVertices.push_back((float)1);
myNormals.push_back((float)0);
myNormals.push_back((float)0);
myNormals.push_back((float)1);
myNormals.push_back((float)1);
myVertColours.push_back(originalColor);
if (x < myWidth - 1 && y < myHeight - 1) {
myIndices[iteratorIndices] = x + y * myWidth;
myIndices[iteratorIndices + 1] = (x + 1) + y * myWidth;
myIndices[iteratorIndices + 2] = x + (y + 1) * myWidth;
myIndices[iteratorIndices + 3] = (x + 1) + y * myWidth;
myIndices[iteratorIndices + 4] = (x + 1) + (y + 1) * myWidth;
myIndices[iteratorIndices + 5] = x + (y + 1) * myWidth;
iteratorIndices += 6;
}
}
}
vertexBuffer.allocate(myVertices, GL_DYNAMIC_DRAW);
colorBuffer.allocate(myVertColours, GL_DYNAMIC_DRAW);
normalBuffer.allocate(myNormals, GL_DYNAMIC_DRAW);
myVBO.setVertexBuffer(vertexBuffer, 4, 4 * sizeof(float));
myVBO.setColorBuffer(colorBuffer, sizeof(ofFloatColor));
myVBO.setNormalBuffer(normalBuffer, 4);
myVBO.setIndexData(myIndices, myWidth * myHeight * 6, GL_STATIC_DRAW);
I’m using buffer just to be able to bind them and modify them on compute shaders but in this example I am just drawing the ofVbo straight away without drawing it.
Notice how the normals are just set up with 4 dimensions (somehow doesn’t work with just 3) but what matters here is that z = 1 so the normal is facing upward perpendicularily to the surface.
The ofLight is just setup this way:
pointLight.setSpecularColor(ofColor(255.f, 255.f, 255.f));
pointLight.setPosition(myWidth / 2, myHeight / 2, 0);
Then the draw function is pretty easy:
ofBackgroundGradient(ofColor(64), ofColor(0));
ofEnableLighting();
pointLight.enable();
cam.begin();
myVBO.drawElements(GL_TRIANGLES, (myWidth - 1) * (myHeight - 1) * 6, 0);
cam.end();
ofDisableLighting();
Here’s what I see when running the program:
Any idea why those lines appear while they have no reason to?
Thanks
This is a sort of a corrected and light version of:
I just managed to narrow down and isolate the problem to ofVbo and ofLight.
Full code available here:
ofApp.zip (2.0 KB)
1 post - 1 participant
