Skip to content

Commit

Permalink
Added non-NPR backend and shadows support
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Aug 11, 2017
1 parent 67fb0ab commit ab7f2b4
Show file tree
Hide file tree
Showing 17 changed files with 768 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/dagon/core/application.d
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class Application: EventListener
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

glEnable(GL_POLYGON_OFFSET_FILL);

checkGLError();
}
Expand Down
4 changes: 3 additions & 1 deletion src/dagon/core/interfaces.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module dagon.core.interfaces;

public import dagon.graphics.rc;

interface Drawable
{
void update(double dt);
void render();
void render(RenderingContext* rc);
}
2 changes: 1 addition & 1 deletion src/dagon/graphics/animmodel.d
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class Actor: Owner, Drawable
}
}

void render()
void render(RenderingContext* rc)
{
if (swapZY)
{
Expand Down
62 changes: 36 additions & 26 deletions src/dagon/graphics/genericmaterial.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ enum int SF_PCF5 = 2;

interface GenericMaterialBackend
{
void attach(GenericMaterial mat);
void bind(RenderingContext* rc);
void unbind();
void bind(GenericMaterial mat, RenderingContext* rc);
void unbind(GenericMaterial mat);
}

class FixedPipelineBackend: Owner, GenericMaterialBackend
{
GenericMaterial material;
//GenericMaterial material;

MaterialInput* idiffuse;
MaterialInput* ispecular;
Expand All @@ -39,11 +38,14 @@ class FixedPipelineBackend: Owner, GenericMaterialBackend
{
super(o);
}

/*
void attach(GenericMaterial mat)
{
material = mat;

}
*/
void bind(GenericMaterial mat, RenderingContext* rc)
{
idiffuse = "diffuse" in mat.inputs;
ispecular = "specular" in mat.inputs;
ishadeless = "shadeless" in mat.inputs;
Expand All @@ -52,12 +54,6 @@ class FixedPipelineBackend: Owner, GenericMaterialBackend
ibrightness = "brightness" in mat.inputs;
iroughness = "roughness" in mat.inputs;
ifogEnabled = "fogEnabled" in mat.inputs;
}

void bind(RenderingContext* rc)
{
if (!material)
return;

glPushAttrib(GL_ENABLE_BIT);

Expand All @@ -81,6 +77,11 @@ class FixedPipelineBackend: Owner, GenericMaterialBackend
glActiveTextureARB(GL_TEXTURE0_ARB);
idiffuse.texture.bind();

//Vector4f ambientColor = rc.environment.ambientConstant;
//Vector4f diffuseColor = Vector4f(1.0f, 1.0f, 1.0f, a);
//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientColor.arrayof.ptr);
//glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseColor.arrayof.ptr);

Vector4f diffuseColor = Vector4f(1.0f, 1.0f, 1.0f, a);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseColor.arrayof.ptr);
}
Expand Down Expand Up @@ -138,10 +139,16 @@ class FixedPipelineBackend: Owner, GenericMaterialBackend
}
}

void unbind()
void unbind(GenericMaterial mat)
{
if (!material)
return;
idiffuse = "diffuse" in mat.inputs;
ispecular = "specular" in mat.inputs;
ishadeless = "shadeless" in mat.inputs;
iemit = "emit" in mat.inputs;
ialpha = "alpha" in mat.inputs;
ibrightness = "brightness" in mat.inputs;
iroughness = "roughness" in mat.inputs;
ifogEnabled = "fogEnabled" in mat.inputs;

if (idiffuse.texture)
{
Expand All @@ -157,11 +164,9 @@ class FixedPipelineBackend: Owner, GenericMaterialBackend

class GenericMaterial: Material
{
GenericMaterialBackend backend;
protected GenericMaterialBackend _backend;
protected FixedPipelineBackend fixedBackend;

//Environment environment;

this(Owner o)
{
super(o);
Expand All @@ -184,24 +189,29 @@ class GenericMaterial: Material
setInput("fogEnabled", true);

fixedBackend = New!FixedPipelineBackend(this);
fixedBackend.attach(this);
_backend = fixedBackend;
}

// TODO: choose backend based on global settings
backend = fixedBackend;
GenericMaterialBackend backend()
{
return _backend;
}

//environment = env;
void backend(GenericMaterialBackend b)
{
_backend = b;
}

override void bind(RenderingContext* rc)
{
if (backend)
backend.bind(rc);
if (_backend)
_backend.bind(this, rc);
}

override void unbind()
{
if (backend)
backend.unbind();
if (_backend)
_backend.unbind(this);
}
}

31 changes: 30 additions & 1 deletion src/dagon/graphics/light.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dagon.logics.stdbehaviour;

class Light: Drawable
{
bool isParallel = false;
Vector4f position;
Color4f color;
//Color4f ambientColor;
Expand Down Expand Up @@ -55,7 +56,7 @@ class Light: Drawable
{
}

void render()
void render(RenderingContext* rc)
{
if (debugDraw)
{
Expand Down Expand Up @@ -85,6 +86,22 @@ Light pointLight(
quadraticAttenuation);
}

Light parallelLight(
Vector3f dir,
Color4f color,
float constantAttenuation = 0.5f,
float linearAttenuation = 0.01f,
float quadraticAttenuation = 0.0f)
{
auto l = New!Light(
Vector4f(-dir.x, -dir.y, -dir.z, 0.0f),
color,
constantAttenuation, linearAttenuation,
quadraticAttenuation);
l.isParallel = true;
return l;
}

enum maxLightsPerReceiver = 5;

class LightReceiver: Behaviour
Expand Down Expand Up @@ -186,12 +203,24 @@ class LightManager: Owner
return light;
}

Light addParallelLight(Vector3f dir, Color4f color)
{
Light light = parallelLight(dir, color);
lights.append(light);
//lightsToDelete.append(light);
return light;
}

void calcLightBrightness(Light light, Vector3f objPos)
{
if (!light.enabled)
{
light.brightness = 0.0f;
}
else if (light.isParallel)
{
light.brightness = float.max;
}
else
{
Vector3f d = (light.position.xyz - objPos);
Expand Down
3 changes: 2 additions & 1 deletion src/dagon/graphics/particles.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dagon.logics.entity;
import dagon.logics.behaviour;
import dagon.graphics.texture;
import dagon.graphics.view;
import dagon.graphics.rc;

struct Particle
{
Expand Down Expand Up @@ -288,7 +289,7 @@ class ParticleSystem: Behaviour
glEnd();
}

override void render()
override void render(RenderingContext* rc)
{
glPushMatrix();
// Get rid of entity's rotation/scaling - we are gonna work in world space
Expand Down
3 changes: 3 additions & 0 deletions src/dagon/graphics/rc.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dlib.math.matrix;
import derelict.opengl.gl;
import dagon.core.event;
import dagon.graphics.environment;
import dagon.graphics.material;

struct RenderingContext
{
Expand All @@ -23,6 +24,7 @@ struct RenderingContext

EventManager eventManager;
Environment environment;
Material overrideMaterial;

void init(EventManager emngr, Environment env = null)
{
Expand All @@ -37,6 +39,7 @@ struct RenderingContext
normalMatrix = Matrix3x3f.identity;
eventManager = emngr;
environment = env;
overrideMaterial = null;
}

void apply()
Expand Down
Loading

0 comments on commit ab7f2b4

Please sign in to comment.