bump mapping

This commit is contained in:
Vadim Lopatin 2016-04-06 09:28:07 +03:00
parent a8dec6c632
commit f5db3da85f
5 changed files with 60 additions and 9 deletions

View File

@ -148,7 +148,7 @@ class UiWidget : VerticalLayout, CellVisitor {
Node3d cubeNode = new Node3d("cubes", cubeDrawable);
_scene.addChild(cubeNode);
{
debug(fbximport) {
// test FBX import
FbxModelImport importer;
string src = loadTextResource("suzanne.fbx");
@ -166,10 +166,17 @@ class UiWidget : VerticalLayout, CellVisitor {
//suzanneMaterial.specular = true;
Model suzanneDrawable = new Model(suzanneMaterial, importer.mesh);
suzanneNode = new Node3d("suzanne", suzanneDrawable);
//suzanneNode.translate(vec3(3, 4, 5));
suzanneNode.translate(vec3(2, 3, -5));
_scene.addChild(suzanneNode);
brickNode = new Node3d("brick");
brickNode.translate(vec3(-2, 3, -3));
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
brickNode.drawable = new Model(brickMaterial, brickMesh);
_scene.addChild(brickNode);
_minerMesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.NORMAL, VertexElementType.COLOR, VertexElementType.TEXCOORD0));
_world = new World();
_world.setCell(0, 11, 10, 2);
@ -210,6 +217,7 @@ class UiWidget : VerticalLayout, CellVisitor {
Node3d dirLightNode;
Node3d suzanneNode;
Node3d brickNode;
float rotationX;
float rotationY;

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 KiB

View File

@ -4,6 +4,8 @@ res/mdpi/cr3_logo.png
res/mdpi/tx_fabric.jpg
res/mdpi/crate.png
res/mdpi/blocks.png
res/mdpi/brick.png
res/mdpi/brickn.png
res/models/suzanne.obj
res/models/suzanne.fbx
res/shaders/colored.vert

View File

@ -25,24 +25,29 @@ class Material : RefCountedObject {
protected TextureRef _texture;
protected string _textureId;
protected TextureRef _bumpTexture;
protected string _bumpTextureId;
// colors
protected vec4 _diffuseColor = vec4(1, 1, 1, 1);
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;
/// 0 - specular is disabled, 1 .. 256 - specular exponent
protected float _specular = 0;
// TODO: more material properties
this() {
}
this(EffectId effectId, string textureId) {
this(EffectId effectId, string textureId, string bumpTextureId = null) {
_effectId = effectId;
_autoEffectParams = null;
_autoEffectId = effectId;
_textureId = textureId;
_bumpTextureId = bumpTextureId;
}
@property vec4 diffuseColor() { return _diffuseColor; }
@ -53,8 +58,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 float specular() { return _specular; }
@property Material specular(float a) { _specular = a; return this; }
@property EffectRef effect() {
if (_effect.isNull && !_autoEffectId.empty)
@ -105,10 +110,33 @@ class Material : RefCountedObject {
return this;
}
@property TextureRef bumpTexture() {
if (_bumpTexture.isNull && _bumpTextureId.length) {
_bumpTexture = GLTextureCache.instance.get(_bumpTextureId);
}
return _bumpTexture;
}
/// set texture
@property Material bumpTexture(TextureRef e) {
_bumpTexture = e;
return this;
}
/// set texture from resourceId
@property Material bumpTexture(string resourceId) {
if (_bumpTextureId == resourceId)
return this; // no change
_bumpTexture.clear();
_bumpTextureId = resourceId;
return this;
}
private AutoParams _lastParams;
private string _lastDefs;
string calcAutoEffectParams(Mesh mesh, LightParams * lights) {
AutoParams newParams = AutoParams(mesh, lights, _specular);
AutoParams newParams = AutoParams(mesh, lights, _specular, !bumpTexture.isNull);
if (newParams != _lastParams) {
_lastParams = newParams;
_lastDefs = _lastParams.defs;
@ -124,6 +152,10 @@ class Material : RefCountedObject {
texture.texture.setup();
texture.texture.setSamplerParams(true);
}
if (!bumpTexture.isNull) {
bumpTexture.texture.setup(1);
bumpTexture.texture.setSamplerParams(true);
}
// matrixes, positions uniforms
if (_effect.hasUniform(DefaultUniform.u_worldViewProjectionMatrix))
_effect.setUniform(DefaultUniform.u_worldViewProjectionMatrix, node.projectionViewModelMatrix);
@ -143,6 +175,8 @@ class Material : RefCountedObject {
_effect.setUniform(DefaultUniform.u_modulateColor, _modulateColor);
if (_effect.hasUniform(DefaultUniform.u_modulateAlpha))
_effect.setUniform(DefaultUniform.u_modulateAlpha, _modulateAlpha);
if (_effect.hasUniform(DefaultUniform.u_specularExponent))
_effect.setUniform(DefaultUniform.u_specularExponent, _specular);
// lighting uniforms
if (lights && !lights.empty) {
@ -197,7 +231,8 @@ struct AutoParams {
ubyte spotLightCount = 0;
bool vertexColor = false;
bool specular = false;
this(Mesh mesh, LightParams * lights, bool specular) {
bool bumpMapping = false;
this(Mesh mesh, LightParams * lights, float specular, bool bumpMapping) {
if (mesh)
vertexColor = mesh.hasElement(VertexElementType.COLOR);
if (lights) {
@ -205,7 +240,8 @@ struct AutoParams {
pointLightCount = cast(ubyte)lights.u_pointLightPosition.length;
spotLightCount = cast(ubyte)lights.u_spotLightPosition.length;
}
this.specular = specular;
this.specular = specular > 0.01;
this.bumpMapping = bumpMapping;
}
string defs() {
char[] buf;
@ -235,6 +271,11 @@ struct AutoParams {
buf ~= ";";
buf ~= "SPECULAR";
}
if (bumpMapping) {
if (buf.length)
buf ~= ";";
buf ~= "BUMPED";
}
return buf.dup;
}
}