Skip to content

Commit

Permalink
Merge pull request #62 from Circular-Studios/develop
Browse files Browse the repository at this point in the history
Version 0.2.5
  • Loading branch information
Tyler Wozniak committed Feb 22, 2014
2 parents 713b8c7 + e7fb24b commit fdd3e53
Show file tree
Hide file tree
Showing 31 changed files with 885 additions and 1,180 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"derelict-gl3": "~master",
"derelict-fi": "~master",
"derelict-assimp3": "~master",
"dyaml": "~master"
"dyaml": "~master",
"gl3n" : "~master"
},
"targetName": "dash",
"targetType": "executable",
Expand Down
1 change: 1 addition & 0 deletions setup-windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dub fetch --local derelict-gl3 --version=~master
dub fetch --local derelict-fi --version=~master
dub fetch --local derelict-assimp3 --version=~master
dub fetch --local dyaml --version=~master
dub fetch --local gl3n --version=~master
2 changes: 1 addition & 1 deletion source/components/assets.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static:
/**
* Get the asset with the given type and name.
*/
final T get( T )( string name ) if( is( T : Component ) )
final T get( T )( string name ) if( is( T == Mesh ) || is( T == Texture ) || is( T == Material ) )
{
static if( is( T == Mesh ) )
{
Expand Down
78 changes: 30 additions & 48 deletions source/components/camera.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,59 @@
module components.camera;
import core.properties, core.gameobject;
import components.component;
import graphics.shaders.shader;
import math.matrix, math.vector;
import graphics.shaders;

import gl3n.linalg;

import std.signals, std.conv;

final class Camera : Component
{
public:
/**
* The view matrix of the camera.
*/
mixin DirtyProperty!( "Matrix!4", "viewMatrix", "updateViewMatrix" );

public:
mixin Signal!( string, string );

this( GameObject owner )
this( )
{
super( owner );

owner.transform.connect( &this.setMatrixDirty );
super( null );
_viewMatrixIsDirty = true;
}

override void update() { }
override void draw( Shader shader ) { }
override void shutdown() { }

static Matrix!4 lookAtLH( Vector!3 cameraPosition, Vector!3 cameraTarget, Vector!3 cameraUpVector )
final @property ref mat4 viewMatrix()
{
auto zaxis = ( cameraTarget - cameraPosition ).normalize();
auto xaxis = ( cameraUpVector % zaxis ).normalize();
auto yaxis = zaxis % xaxis;
if( _viewMatrixIsDirty )
updateViewMatrix();

auto newMatrix = new Matrix!4();

newMatrix.matrix[0][0] = xaxis.x;
newMatrix.matrix[1][0] = xaxis.y;
newMatrix.matrix[2][0] = xaxis.z;
newMatrix.matrix[0][1] = yaxis.x;
newMatrix.matrix[1][1] = yaxis.y;
newMatrix.matrix[2][1] = yaxis.z;
newMatrix.matrix[0][2] = zaxis.x;
newMatrix.matrix[1][2] = zaxis.y;
newMatrix.matrix[2][2] = zaxis.z;
newMatrix.matrix[3][0] = -( xaxis * cameraPosition );
newMatrix.matrix[3][1] = -( yaxis * cameraPosition );
newMatrix.matrix[3][2] = -( zaxis * cameraPosition );
newMatrix.matrix[3][3] = 1.0f;

return newMatrix;
return _viewMatrix;
}

private:
mat4 _viewMatrix;
bool _viewMatrixIsDirty;
final void setMatrixDirty( string prop, string newVal )
{
_viewMatrixIsDirty = true;
}
final void updateViewMatrix()
{
auto up = owner.transform.rotation.matrix * Vector!3.up;
auto lookAt = ( owner.transform.rotation.matrix * Vector!3.forward ) + owner.transform.position;

Vector!3[3] axes;
axes[ 2 ] = ( lookAt - owner.transform.position ).normalize();
axes[ 0 ] = up.cross( axes[ 2 ] ).normalize();
axes[ 1 ] = axes[ 2 ].cross( axes[ 0 ] ).normalize();

for( uint ii = 0; ii < 3; ++ii )
{
for( uint jj = 0; jj < 3; ++jj )
viewMatrix.matrix[ jj ][ ii ] = axes[ ii ][ jj ];
viewMatrix.matrix[ 3 ][ ii ] = -axes[ ii ].dot( owner.transform.position );
}
//Assuming pitch & yaw are in radians
float cosPitch = cos( owner.transform.rotation.pitch );
float sinPitch = sin( owner.transform.rotation.pitch );
float cosYaw = cos( owner.transform.rotation.yaw );
float sinYaw = sin( owner.transform.rotation.yaw );

vec3 xaxis = vec3( cosYaw, 0.0f, -sinYaw );
vec3 yaxis = vec3( sinYaw * sinPitch, cosPitch, cosYaw * sinPitch );
vec3 zaxis = vec3( sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw );

_viewMatrix.clear( 0.0f );
_viewMatrix[ 0 ] = xaxis.vector ~ -( xaxis * owner.transform.position );
_viewMatrix[ 1 ] = yaxis.vector ~ -( yaxis * owner.transform.position );
_viewMatrix[ 2 ] = zaxis.vector ~ -( zaxis * owner.transform.position );
_viewMatrix[ 3 ] = [ 0, 0, 0, 1 ];

_viewMatrixIsDirty = false;
}
}
8 changes: 2 additions & 6 deletions source/components/component.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
module components.component;
import core.properties, core.gameobject;
import graphics.shaders.shader;
import graphics.shaders;

/**
* Interface for components to implement.
Expand All @@ -19,10 +19,6 @@ abstract class Component
* Function called on update.
*/
abstract void update();
/**
* Function calledn on draw.
*/
abstract void draw( Shader shader );
/**
* Function called on shutdown.
*/
Expand All @@ -31,5 +27,5 @@ abstract class Component
/**
* The GameObject that owns this component.
*/
mixin Property!( "GameObject", "owner", "protected" );
mixin Property!( "GameObject", "owner", "public" );
}
50 changes: 50 additions & 0 deletions source/components/lights.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module components.lights;
import core.properties;
import components.component;
import graphics.shaders;

import gl3n.linalg;

class Light : Component
{
public:
mixin Property!( "vec3", "color", "public" );

this( vec3 color )
{
super( null );

this.color = color;
}

override void update()
{

}

override void shutdown()
{

}

}

class AmbientLight : Light
{
this( vec3 color )
{
super( color );
}
}

class DirectionalLight : Light
{
public:
mixin Property!( "vec3", "direction" );

this( vec3 color, vec3 direction )
{
this.direction = direction;
super( color );
}
}
16 changes: 0 additions & 16 deletions source/components/lights/directional.d

This file was deleted.

34 changes: 0 additions & 34 deletions source/components/lights/light.d

This file was deleted.

52 changes: 39 additions & 13 deletions source/components/material.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module components.material;
import core.properties;
import components;
import graphics.shaders.shader, graphics.shaders.glshader;
import graphics.graphics, graphics.shaders;
import utility.config;

import yaml;
import derelict.opengl3.gl3;
import derelict.opengl3.gl3, derelict.freeimage.freeimage;
import std.variant, std.conv;

final class Material : Component
Expand Down Expand Up @@ -41,19 +41,45 @@ public:
}

override void update() { }
override void draw( Shader shader ) { }
override void shutdown() { }
}

final class Texture
{
public:
mixin Property!( "uint", "width" );
mixin Property!( "uint", "height" );
mixin Property!( "uint", "glID" );

this( string filePath )
{
filePath ~= "\0";
auto imageData = FreeImage_ConvertTo32Bits( FreeImage_Load( FreeImage_GetFileType( filePath.ptr, 0 ), filePath.ptr, 0 ) );

width = FreeImage_GetWidth( imageData );
height = FreeImage_GetHeight( imageData );

glGenTextures( 1, &_glID );
glBindTexture( GL_TEXTURE_2D, glID );
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_BGRA, //FreeImage loads in BGR format because fuck you
GL_UNSIGNED_BYTE,
cast(GLvoid*)FreeImage_GetBits( imageData ) );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

FreeImage_Unload( imageData );
glBindTexture( GL_TEXTURE_2D, 0 );
}

final void bind( Shader shader )
void shutdown()
{
GLint textureLocation = glGetUniformLocation( (cast(GLShader)shader).programID, "diffuseTexture\0" );
glUniform1i( textureLocation, 0 );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, diffuse.glID );

textureLocation = glGetUniformLocation( (cast(GLShader)shader).programID, "normalTexture\0" );
glUniform1i( textureLocation, 1 );
glActiveTexture( GL_TEXTURE1 );
glBindTexture( GL_TEXTURE_2D, normal.glID );
glBindTexture( GL_TEXTURE_2D, 0 );
glDeleteBuffers( 1, &_glID );
}
}
Loading

0 comments on commit fdd3e53

Please sign in to comment.