fix lighting

This commit is contained in:
Vadim Lopatin 2016-04-25 13:39:46 +03:00
parent b912b6ba83
commit 13ee465cef
9 changed files with 132 additions and 58 deletions

View File

@ -180,6 +180,7 @@ class UiWidget : VerticalLayout, CellVisitor {
brickNode.translate(vec3(-2, 2, -3)); brickNode.translate(vec3(-2, 2, -3));
Mesh brickMesh = Mesh.createCubeMesh(vec3(0, 0, 0), 0.8, vec4(0.8, 0.8, 0.8, 1)); Mesh brickMesh = Mesh.createCubeMesh(vec3(0, 0, 0), 0.8, vec4(0.8, 0.8, 0.8, 1));
Material brickMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "brick", "brickn"); // with bump mapping Material brickMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "brick", "brickn"); // with bump mapping
//brickMaterial.specular = true;
brickNode.drawable = new Model(brickMaterial, brickMesh); brickNode.drawable = new Model(brickMaterial, brickMesh);
_scene.addChild(brickNode); _scene.addChild(brickNode);
@ -204,7 +205,7 @@ class UiWidget : VerticalLayout, CellVisitor {
updateMinerMesh(); updateMinerMesh();
Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks"); Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks");
minerMaterial.textureLinear = false; //minerMaterial.textureLinear = false;
Model minerDrawable = new Model(minerMaterial, _minerMesh); Model minerDrawable = new Model(minerMaterial, _minerMesh);
Node3d minerNode = new Node3d("miner", minerDrawable); Node3d minerNode = new Node3d("miner", minerDrawable);
_scene.addChild(minerNode); _scene.addChild(minerNode);

View File

@ -84,65 +84,103 @@ public:
} }
/* // pos, normal, color, tx
0 1
3 2
/* North, z=-1
Y^
0 | 1
X<-----x-----
3 | 2
*/ */
immutable float inv = -1.0f;
// pos, normal, color, tx
static const float[VERTEX_COMPONENTS * 4] face_vertices_north = static const float[VERTEX_COMPONENTS * 4] face_vertices_north =
[ [
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0*inv, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0*inv, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0*inv, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0*inv, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
]; ];
/* South, z=1
Y^
0 | 1
-----x----->X
3 | 2
*/
static const float[VERTEX_COMPONENTS * 4] face_vertices_south = static const float[VERTEX_COMPONENTS * 4] face_vertices_south =
[ [
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0*inv, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0*inv, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0*inv, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0*inv, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
]; ];
/* West, x=-1
Y^
0 | 1
-----x----->Z
3 | 2
*/
static const float[VERTEX_COMPONENTS * 4] face_vertices_west = static const float[VERTEX_COMPONENTS * 4] face_vertices_west =
[ [
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0 -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0
]; ];
/* East, x=1
Y^
0 | 1
Z<-----x-----
3 | 2
*/
static const float[VERTEX_COMPONENTS * 4] face_vertices_east = static const float[VERTEX_COMPONENTS * 4] face_vertices_east =
[ [
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
]; ];
/* Up, y=1
0 | 1
-----x----->X
3 | 2
Zv
*/
static const float[VERTEX_COMPONENTS * 4] face_vertices_up = static const float[VERTEX_COMPONENTS * 4] face_vertices_up =
[ [
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
]; ];
/* Down, y=-1
0 | 1
X<-----x-----
3 | 2
Zv
*/
static const float[VERTEX_COMPONENTS * 4] face_vertices_down = static const float[VERTEX_COMPONENTS * 4] face_vertices_down =
[ [
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.5, -0.5,-0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -0.5, -0.5,-0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
]; ];
static const ushort[6] face_indexes = static const ushort[6] face_indexes =
[ [
0, 1, 2, 2, 1, 3 2, 1, 0, 0, 3, 2 // CCW
]; ];
static const ushort[6] face_indexes_back = static const ushort[6] face_indexes_back =

View File

@ -404,24 +404,24 @@ struct Position {
pos = position; pos = position;
direction = dir; direction = dir;
} }
Vector2d calcPlaneCoords(Vector3d v) { //Vector2d calcPlaneCoords(Vector3d v) {
v = v - pos; // v = v - pos;
switch (direction.dir) with(Dir) { // switch (direction.dir) with(Dir) {
default: // default:
case NORTH: // case NORTH:
return Vector2d(v.x, v.y); // return Vector2d(v.x, v.y);
case SOUTH: // case SOUTH:
return Vector2d(-v.x, v.y); // return Vector2d(-v.x, v.y);
case EAST: // case EAST:
return Vector2d(v.z, v.y); // return Vector2d(v.z, v.y);
case WEST: // case WEST:
return Vector2d(-v.z, v.y); // return Vector2d(-v.z, v.y);
case UP: // case UP:
return Vector2d(-v.z, v.x); // return Vector2d(-v.z, v.x);
case DOWN: // case DOWN:
return Vector2d(v.z, v.x); // return Vector2d(v.z, v.x);
} // }
} //}
void turnLeft() { void turnLeft() {
direction.turnLeft(); direction.turnLeft();
} }

View File

@ -86,7 +86,8 @@ class UiWidget : VerticalLayout, CellVisitor {
dirLightNode.translateX(2); dirLightNode.translateX(2);
dirLightNode.translateY(3); dirLightNode.translateY(3);
dirLightNode.translateZ(0); dirLightNode.translateZ(0);
dirLightNode.light = Light.createPoint(vec3(12, 12, 12), 25); //Light.createDirectional(vec3(1, 0.5, 0.5)); dirLightNode.light = Light.createPoint(vec3(1.5, 1.5, 1.5), 25); //Light.createDirectional(vec3(1, 0.5, 0.5));
//dirLightNode.light = Light.createDirectional(vec3(1, 0.5, 0.5));
dirLightNode.light.enabled = true; dirLightNode.light.enabled = true;
_scene.addChild(dirLightNode); _scene.addChild(dirLightNode);
@ -134,9 +135,9 @@ class UiWidget : VerticalLayout, CellVisitor {
updateMinerMesh(); updateMinerMesh();
Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks"); Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks");
minerMaterial.ambientColor = vec3(0.05,0.05,0.05); minerMaterial.ambientColor = vec3(0.1,0.1,0.1);
minerMaterial.textureLinear = false; minerMaterial.textureLinear = false;
minerMaterial.specular = true; minerMaterial.specular = 10;
Model minerDrawable = new Model(minerMaterial, _minerMesh); Model minerDrawable = new Model(minerMaterial, _minerMesh);
Node3d minerNode = new Node3d("miner", minerDrawable); Node3d minerNode = new Node3d("miner", minerDrawable);
_scene.addChild(minerNode); _scene.addChild(minerNode);

View File

@ -1616,6 +1616,19 @@ struct mat4 {
return vec3(m[8], m[9], m[10]); return vec3(m[8], m[9], m[10]);
} }
void transformVector(ref vec3 v) const {
transformVector(v.x, v.y, v.z, 0, v);
}
void transformPoint(ref vec3 v) const {
transformVector(v.x, v.y, v.z, 1, v);
}
void transformVector(float x, float y, float z, float w, ref vec3 dst) const {
dst.x = x * m[0] + y * m[4] + z * m[8] + w * m[12];
dst.y = x * m[1] + y * m[5] + z * m[9] + w * m[13];
dst.z = x * m[2] + y * m[6] + z * m[10] + w * m[14];
}
static __gshared const mat4 IDENTITY; static __gshared const mat4 IDENTITY;
} }

View File

@ -1314,13 +1314,15 @@ class GLVertexBuffer : VertexBuffer {
// specify index buffer // specify index buffer
checkgl!glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); checkgl!glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
int offset = 0; int offset = 0;
//Log.v("=== enableAttributes for ", _format);
for(int i = 0; i < _format.length; i++) { for(int i = 0; i < _format.length; i++) {
int loc = effect.getVertexElementLocation(_format[i].type); int loc = effect.getVertexElementLocation(_format[i].type);
if (loc >= 0) { if (loc >= 0) {
//Log.v("setting attrib pointer for type ", _format[i].type, " offset=", offset, " location=", loc);
checkgl!glVertexAttribPointer(loc, _format[i].size, GL_FLOAT, cast(ubyte)GL_FALSE, _format.vertexSize, cast(char*)(offset)); checkgl!glVertexAttribPointer(loc, _format[i].size, GL_FLOAT, cast(ubyte)GL_FALSE, _format.vertexSize, cast(char*)(offset));
checkgl!glEnableVertexAttribArray(loc); checkgl!glEnableVertexAttribArray(loc);
} else { } else {
//Log.d("Attribute location not found for ", _format[i].type); //Log.v("Attribute location not found for ", _format[i].type);
} }
offset += _format[i].byteSize; offset += _format[i].byteSize;
} }

View File

@ -44,9 +44,9 @@ class Light : RefCountedObject {
@property Light node(Node3d n) { _node = n; return this; } @property Light node(Node3d n) { _node = n; return this; }
/// direction in world coordinates /// direction in world coordinates
@property vec3 direction() { return _node ? _node.forwardVectorWorld : vec3(0, 0, 1); } @property vec3 direction() { return _node ? _node.forwardVectorView : vec3(0, 0, 1); }
/// position in world coordinates /// position in world coordinates
@property vec3 position() { return _node ? _node.translationWorld : vec3(0, 0, 0); } @property vec3 position() { return _node ? _node.translationView : vec3(0, 0, 0); }
@property float range() const { return 1.0; } @property float range() const { return 1.0; }
@property void range(float v) { assert(false); } @property void range(float v) { assert(false); }

View File

@ -201,7 +201,7 @@ struct VertexElement {
/// Vertex format elements list /// Vertex format elements list
struct VertexFormat { struct VertexFormat {
private VertexElement[] _elements; private VertexElement[] _elements;
private byte[VertexElementType.max + 1] _elementOffset = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; private byte[16] _elementOffset = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
private int _vertexSize; // vertex size in floats private int _vertexSize; // vertex size in floats
/// make using element list /// make using element list
this(inout VertexElement[] elems...) { this(inout VertexElement[] elems...) {

View File

@ -179,6 +179,14 @@ class Node3d : Transform {
return translation; return translation;
} }
/// returns translation vector (position) of this node in view space
@property vec3 translationView() {
vec3 translation;
worldMatrix.getTranslation(translation);
viewMatrix.transformPoint(translation);
return translation;
}
/** /**
* Gets the inverse transpose world matrix corresponding to this node. * Gets the inverse transpose world matrix corresponding to this node.
* *
@ -218,4 +226,15 @@ class Node3d : Transform {
@property vec3 forwardVectorWorld() { @property vec3 forwardVectorWorld() {
return worldMatrix.forwardVector; return worldMatrix.forwardVector;
} }
/**
* Returns the forward vector of the Node in view space.
*
* @return The forward vector in view space.
*/
@property vec3 forwardVectorView() {
return worldMatrix.forwardVector;
vec3 vector = worldMatrix.forwardVector;
viewMatrix.transformVector(vector);
return vector;
}
} }