trying to get lighting working

This commit is contained in:
Vadim Lopatin 2016-04-05 10:54:30 +03:00
parent 1a1e904f2e
commit 9633cd9465
4 changed files with 52 additions and 24 deletions

View File

@ -114,12 +114,16 @@ class UiWidget : VerticalLayout, CellVisitor {
_scene.activeCamera = _cam;
Node3d dirLightNode = new Node3d();
dirLightNode = new Node3d();
//dirLightNode.lookAt(vec3(-5, -5, -5), vec3(0, 0, 0), vec3(0, 1, 0));
//dirLightNode.rotateY(-15);
dirLightNode.rotateY(-15);
//dirLightNode.rotateX(20);
dirLightNode.light = Light.createPoint(vec3(1, 0.5, 0.5), 5); //Light.createDirectional(vec3(1, 0.5, 0.5));
dirLightNode.light.enabled = false;
dirLightNode.translateX(2);
dirLightNode.translateY(3);
dirLightNode.translateZ(3);
dirLightNode.light = Light.createPoint(vec3(110, 110.5, 110.5), 15); //Light.createDirectional(vec3(1, 0.5, 0.5));
//dirLightNode.light = Light.createDirectional(vec3(11, 10.5, 10.5));
dirLightNode.light.enabled = true;
_scene.addChild(dirLightNode);
int x0 = 0;
@ -138,7 +142,7 @@ class UiWidget : VerticalLayout, CellVisitor {
_mesh.addCubeMesh(vec3(x0+ i * 2 + 1.0f, y0+-i * 2 + 1.0f, z0+ i * 2 + 1.0f), 0.2f, vec4(i / 12, 1 - i / 12, i / 12, 1));
_mesh.addCubeMesh(vec3(x0+ -i * 2 - 1.0f, y0+-i * 2 - 1.0f, z0+ -i * 2 - 1.0f), 0.2f, vec4(1 - i / 12, i / 12, i / 12, 1));
}
Material cubeMaterial = new Material(EffectId("textured.vert", "textured.frag", "SPECULAR"), "crate");
Material cubeMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "crate");
Model cubeDrawable = new Model(cubeMaterial, _mesh);
Node3d cubeNode = new Node3d("cubes", cubeDrawable);
_scene.addChild(cubeNode);
@ -147,9 +151,10 @@ class UiWidget : VerticalLayout, CellVisitor {
string src = loadTextResource("suzanne.obj");
importer.parse(src);
Log.d("suzanne mesh:", importer.mesh.dumpVertexes(20));
Material suzanneMaterial = new Material(EffectId("colored.vert", "colored.frag", "SPECULAR"), null); //"SPECULAR"
suzanneMaterial.ambientColor = vec3(0.0, 0.0, 0.0);
suzanneMaterial.diffuseColor = vec4(1.0, 0.7, 0.7, 1.0);
Material suzanneMaterial = new Material(EffectId("colored.vert", "colored.frag", null), null); //"SPECULAR"
//suzanneMaterial.ambientColor = vec3(0.5, 0.5, 0.5);
suzanneMaterial.diffuseColor = vec4(1.0, 0.7, 0.5, 1.0);
//suzanneMaterial.specular = true;
Model suzanneDrawable = new Model(suzanneMaterial, importer.mesh);
Node3d suzanneNode = new Node3d("suzanne", suzanneDrawable);
//suzanneNode.translate(vec3(3, 4, 5));
@ -194,6 +199,8 @@ class UiWidget : VerticalLayout, CellVisitor {
//destroy(w);
}
Node3d dirLightNode;
float rotationX;
float rotationY;
float rotationZ;
@ -287,6 +294,13 @@ class UiWidget : VerticalLayout, CellVisitor {
_cam.rotateX(rotationX);
_cam.rotateY(rotationY);
_cam.rotateZ(rotationZ);
//Log.d("camPosition: ", _scene.cameraPosition);
//Log.d("camDirection: ", _scene.forwardVectorWorld);
//Log.d("lightPosition: ", dirLightNode.light.position);
//Log.d("lightDirection: ", dirLightNode.light.direction);
//Log.d("lightColor: ", dirLightNode.light.color);
//_cam.translate(vec3(-1, -1.5, -1)); // - angle/1000
//_cam.translate(vec3(0, 0, -1.1)); // - angle/1000
//_cam.translate(vec3(0, 3, - angle/1000)); //

View File

@ -156,6 +156,7 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
compatibilityFixes(sourceCode, type);
Log.d("compileShader: glsl = ", glslversion, ", type: ", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : (type == GL_FRAGMENT_SHADER ? "GL_FRAGMENT_SHADER" : "UNKNOWN")));
//Log.v("Shader code:\n", sourceCode);
GLuint shader = glCreateShader(type);
const char * psrc = sourceCode.toStringz;
glShaderSource(shader, 1, &psrc, null);
@ -320,11 +321,11 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
}
override void setUniform(string uniformName, const vec2[] vec) {
checkgl!glUniform2fv(getUniformLocation(uniformName), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform2fv(getUniformLocation(uniformName), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(DefaultUniform id, const vec2[] vec) {
checkgl!glUniform2fv(getUniformLocation(id), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform2fv(getUniformLocation(id), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(string uniformName, vec2 vec) {
@ -344,11 +345,11 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
}
override void setUniform(string uniformName, const vec3[] vec) {
checkgl!glUniform3fv(getUniformLocation(uniformName), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform3fv(getUniformLocation(uniformName), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(DefaultUniform id, const vec3[] vec) {
checkgl!glUniform3fv(getUniformLocation(id), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform3fv(getUniformLocation(id), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(string uniformName, vec4 vec) {
@ -360,11 +361,11 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
}
override void setUniform(string uniformName, const vec4[] vec) {
checkgl!glUniform4fv(getUniformLocation(uniformName), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform4fv(getUniformLocation(uniformName), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(DefaultUniform id, const vec4[] vec) {
checkgl!glUniform4fv(getUniformLocation(id), cast(int)vec.length, vec[0].vec.ptr);
checkgl!glUniform4fv(getUniformLocation(id), cast(int)vec.length, cast(const(float)*)vec.ptr);
}
override void setUniform(string uniformName, ref const(mat4) matrix) {
@ -376,11 +377,11 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
}
override void setUniform(string uniformName, const(mat4)[] matrix) {
checkgl!glUniformMatrix4fv(getUniformLocation(uniformName), cast(int)matrix.length, false, matrix[0].m.ptr);
checkgl!glUniformMatrix4fv(getUniformLocation(uniformName), cast(int)matrix.length, false, cast(const(float)*)matrix.ptr);
}
override void setUniform(DefaultUniform id, const(mat4)[] matrix) {
checkgl!glUniformMatrix4fv(getUniformLocation(id), cast(int)matrix.length, false, matrix[0].m.ptr);
checkgl!glUniformMatrix4fv(getUniformLocation(id), cast(int)matrix.length, false, cast(const(float)*)matrix.ptr);
}
override void setUniform(string uniformName, float v) {
@ -392,11 +393,11 @@ class GLProgram : dlangui.graphics.scene.mesh.GraphicsEffect {
}
override void setUniform(string uniformName, const float[] v) {
checkgl!glUniform1fv(getUniformLocation(uniformName), cast(int)v.length, v.ptr);
checkgl!glUniform1fv(getUniformLocation(uniformName), cast(int)v.length, cast(const(float)*)v.ptr);
}
override void setUniform(DefaultUniform id, const float[] v) {
checkgl!glUniform1fv(getUniformLocation(id), cast(int)v.length, v.ptr);
checkgl!glUniform1fv(getUniformLocation(id), cast(int)v.length, cast(const(float)*)v.ptr);
}
/// returns true if effect has uniform

View File

@ -115,13 +115,17 @@ class Effect : GLProgram {
@property override string vertexSource() {
_visitedIncludes = null;
_visitedIncludes[_id.vertexShaderName] = true; // mark as included
return preProcessSource(loadVertexSource(_id.vertexShaderName));
string res = preProcessSource(loadVertexSource(_id.vertexShaderName));
//Log.v("vertexSource:", res);
return res;
}
@property override string fragmentSource() {
_visitedIncludes = null;
_visitedIncludes[_id.fragmentShaderName] = true; // mark as included
return preProcessSource(loadVertexSource(_id.fragmentShaderName));
string res = preProcessSource(loadVertexSource(_id.fragmentShaderName));
//Log.v("fragmentSource:", res);
return res;
}
override bool initLocations() {

View File

@ -27,10 +27,12 @@ class Material : RefCountedObject {
// colors
protected vec4 _diffuseColor = vec4(1, 1, 1, 1);
protected vec3 _ambientColor = vec3(0, 0, 0);
protected vec3 _ambientColor = vec3(0.2, 0.2, 0.2);
protected vec4 _modulateColor = vec4(1, 1, 1, 1);
protected float _modulateAlpha = 1;
protected bool _specular = false;
// TODO: more material properties
this() {
@ -51,6 +53,8 @@ class Material : RefCountedObject {
@property Material modulateColor(vec4 color) { _modulateColor = color; return this; }
@property float modulateAlpha() { return _modulateAlpha; }
@property Material modulateColor(float a) { _modulateAlpha = a; return this; }
@property bool specular() { return _specular; }
@property Material specular(bool a) { _specular = a; return this; }
@property EffectRef effect() {
if (_effect.isNull && !_autoEffectId.empty)
@ -104,7 +108,7 @@ class Material : RefCountedObject {
private AutoParams _lastParams;
private string _lastDefs;
string calcAutoEffectParams(Mesh mesh, LightParams * lights) {
AutoParams newParams = AutoParams(mesh, lights, false);
AutoParams newParams = AutoParams(mesh, lights, _specular);
if (newParams != _lastParams) {
_lastParams = newParams;
_lastDefs = _lastParams.defs;
@ -125,8 +129,11 @@ class Material : RefCountedObject {
_effect.setUniform(DefaultUniform.u_worldViewProjectionMatrix, node.projectionViewModelMatrix);
if (_effect.hasUniform(DefaultUniform.u_cameraPosition))
_effect.setUniform(DefaultUniform.u_cameraPosition, node.cameraPosition);
if (_effect.hasUniform(DefaultUniform.u_worldViewMatrix))
if (_effect.hasUniform(DefaultUniform.u_worldViewMatrix)) {
_effect.setUniform(DefaultUniform.u_worldViewMatrix, node.worldViewMatrix);
//Log.d("DefaultUniform.u_worldViewMatrix: ", node.worldViewMatrix);
//Log.d("DefaultUniform.u_worldViewMatrix * 3,3,3: ", node.worldViewMatrix * vec3(3,3,3));
}
if (_effect.hasUniform(DefaultUniform.u_ambientColor))
_effect.setUniform(DefaultUniform.u_ambientColor, _ambientColor);
if (_effect.hasUniform(DefaultUniform.u_diffuseColor))
@ -137,8 +144,10 @@ class Material : RefCountedObject {
_effect.setUniform(DefaultUniform.u_modulateAlpha, _modulateAlpha);
if (lights && !lights.empty) {
if (lights.u_directionalLightDirection.length) {
if (_effect.hasUniform(DefaultUniform.u_directionalLightDirection))
if (_effect.hasUniform(DefaultUniform.u_directionalLightDirection)) {
_effect.setUniform(DefaultUniform.u_directionalLightDirection, lights.u_directionalLightDirection);
//Log.d("DefaultUniform.u_directionalLightDirection: ", lights.u_directionalLightDirection);
}
if (_effect.hasUniform(DefaultUniform.u_directionalLightColor))
_effect.setUniform(DefaultUniform.u_directionalLightColor, lights.u_directionalLightColor);
}