Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/prophile/xsera
Browse files Browse the repository at this point in the history
  • Loading branch information
adam000 committed Jul 6, 2010
2 parents 44d716b + 766ffe2 commit db81cef
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 26 deletions.
22 changes: 21 additions & 1 deletion Engine/Graphics/Graphics.cpp
Expand Up @@ -10,6 +10,27 @@
#include "ParticleSystem.h"
#include <list>

/*
A QUICK NOTE ON VERTEX ATTRIBUTES
On ATI hardware, you can use any numbers you want. However! On NVidia hardware,
the built-in attributes will collide with user-specified attributes if you try
to use both, and hilarity ensues. Here is a table of collisions:
gl_Vertex 0
gl_Normal 2
gl_Color 3
gl_SecondaryColor 4
gl_FogCoord 5
gl_MultiTexCoordN 8+N
We don't use gl_Color, gl_SecondaryColor or gl_FogCoord, nor do we use
gl_MultiTexCoordN where N >= 1. Therefore if we start off custom attributes at
1, the only gotcha is that we MUST skip 2.
*/

//#define DISABLE_WARP_EFFECTS

#include "Apollo.h"
Expand Down Expand Up @@ -778,7 +799,6 @@ void DrawObject3DAmbient ( std::string name, vec2 centre, float scale, float ang
obj->BindTextures();
//glUniform3f(UniformLocation("Ambient"), ambient.red(), ambient.green(), ambient.blue());
SetShader("3DBase");
glUniform1i(UniformLocation("tex"), 0);
glUniform1f(UniformLocation("specularScale"), obj->SpecularScale());
glUniform1f(UniformLocation("shininess"), obj->Shininess());
Matrices::SetViewMatrix(matrix2x3::Translate(centre));
Expand Down
45 changes: 45 additions & 0 deletions Engine/Graphics/ImageLoader.cpp
Expand Up @@ -107,6 +107,51 @@ GLuint CreateTexture ( SDL_Surface* surface, bool autofree, bool rectangle, bool
return texID;
}

static inline uint8_t heightAt(SDL_Surface* surface, int x, int y)
{
if (!surface) return 0;
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= surface->w) x = surface->w - 1;
if (y >= surface->h) y = surface->h - 1;
return *((unsigned char*)surface->pixels + surface->format->BytesPerPixel*(surface->w*y + x));
}

#define HEIGHTAT(x, y) *((unsigned char*)heightMap->pixels + bpp*(width*(y) + x))

SDL_Surface* CreateBumpMap(SDL_Surface* heightMap)
{
int width = heightMap ? heightMap->w : 1;
int height = heightMap ? heightMap->h : 1;
SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
short Vdiff, Hdiff;
float VdiffF, HdiffF;
float TC;
unsigned char L, R, U, D;
Uint32 rgba;
L = heightAt(heightMap, x - 1, y);
R = heightAt(heightMap, x + 1, y);
U = heightAt(heightMap, x, y - 1);
D = heightAt(heightMap, x, y + 1);
Vdiff = (short)D - (short)U;
Hdiff = (short)R - (short)L;
VdiffF = Vdiff / 255.0f;
HdiffF = Hdiff / 255.0f;
TC = sqrtf(VdiffF*VdiffF + HdiffF*HdiffF - 1.0f);
VdiffF += 1.0f; VdiffF *= 0.5f;
HdiffF += 1.0f; HdiffF *= 0.5f;
TC += 1.0f; TC *= 0.5f;
rgba = SDL_MapRGBA(surface->format, HdiffF, VdiffF, TC, heightAt(heightMap, x, y));
*((Uint32*)surface->pixels + y*width + x) = rgba;
}
}
return surface;
}

}

}
1 change: 1 addition & 0 deletions Engine/Graphics/ImageLoader.h
Expand Up @@ -15,6 +15,7 @@ namespace ImageLoader
SDL_Surface* Zip(SDL_Surface* colour, SDL_Surface* alpha);
SDL_Surface* LoadImage ( const std::string& path );
GLuint CreateTexture ( SDL_Surface* surface, bool autofree, bool rectangle = true, bool invert = false );
SDL_Surface* CreateBumpMap(SDL_Surface* heightMap);

}

Expand Down
15 changes: 9 additions & 6 deletions Engine/Graphics/Object3D.cpp
Expand Up @@ -2,6 +2,7 @@
#include "Matrix2x3.h"
#include "Utilities/ResourceManager.h"
#include "ImageLoader.h"
#include "Shaders.h"

namespace Graphics
{
Expand Down Expand Up @@ -374,32 +375,34 @@ void Object3D::BindTextures ()
void Object3D::Draw ( float scale, float angle, float bank )
{
// set up matrices
/*matrix2x3 transformation;
matrix2x3 transformation;
transformation *= matrix2x3::Scale(scale);
transformation *= matrix2x3::Rotation(angle);
Matrices::SetModelMatrix(transformation);*/
Matrices::SetModelMatrix(matrix2x3::Identity());
Matrices::SetModelMatrix(transformation);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
glPushMatrix();
glEnableClientState(GL_NORMAL_ARRAY);
//glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
glScalef(scale, scale, scale);
glRotatef(180.0f*(angle/M_PI), 0.0f, 0.0f, 1.0f);
glEnableVertexAttribArray(1);
glRotatef(bank, 0.0f, 1.0f, 0.0f);
glScalef(intScale, intScale, intScale);
glTranslatef(offX, offY, 0.0f);
//glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
// bind the VBOs
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, normalsVBO);
glNormalPointer(GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, texVBO);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
Shaders::BindAttribute(1, "Tangent");
glBindBuffer(GL_ARRAY_BUFFER, tangentVBO);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
// draw all the faces
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, nverts);
glDisableVertexAttribArray(1);
glDisableClientState(GL_NORMAL_ARRAY);
glPopMatrix();
glDepthMask(GL_FALSE);
Expand Down
14 changes: 14 additions & 0 deletions Engine/Graphics/Shaders.cpp
Expand Up @@ -115,6 +115,15 @@ class Shader
{
currentShader = programObject;
glUseProgram(programObject);
// bind textures
for (int i = 0; i < 4; ++i)
{
char name[6];
sprintf(name, "tex%d", i);
GLint pos = glGetUniformLocation(programObject, name);
if (pos != -1)
glUniform1i(pos, i);
}
}
}
};
Expand Down Expand Up @@ -162,6 +171,11 @@ GLuint UniformLocation ( const std::string& name )
return location;
}

void BindAttribute ( GLuint index, const std::string& name )
{
glBindAttribLocation(currentShader, index, name.c_str());
}

}

}
1 change: 1 addition & 0 deletions Engine/Graphics/Shaders.h
Expand Up @@ -12,6 +12,7 @@ namespace Shaders

void SetShader ( const std::string& name );
GLuint UniformLocation ( const std::string& name );
void BindAttribute ( GLuint index, const std::string& name );

}

Expand Down
5 changes: 3 additions & 2 deletions Engine/Scripting/LuaBind.cpp
Expand Up @@ -1455,8 +1455,9 @@ int Sound_PlayPositional ( lua_State* L )
{
const char* sound = luaL_checkstring(L, 1);
vec2 pos = luaL_checkvec2(L, 2);
float volume = luaL_optnumber(L, 3, 1.0);
Sound::PlaySoundPositional(sound, pos, volume);
vec2 vel = luaL_checkvec2(L, 3);
float volume = luaL_optnumber(L, 4, 1.0);
Sound::PlaySoundPositional(sound, pos, vel, volume);
return 0;
}

Expand Down
7 changes: 7 additions & 0 deletions Engine/Scripting/Scripting.cpp
@@ -1,6 +1,7 @@
#include "Scripting.h"
#include "ResourceManager.h"
#include "Engine/Logging.h"
#include "Compile.h"

extern "C"
{
Expand Down Expand Up @@ -43,6 +44,12 @@ static void luaLoad ( lua_State* L, const std::string& path )
{
fullpath = "Scripts/" + path + ".lo";
}
else
{
#ifdef NDEBUG
CompileScript(path);
#endif
}
SDL_RWops* rwops = ResourceManager::OpenFile(fullpath);
if (rwops)
{
Expand Down
15 changes: 12 additions & 3 deletions Engine/Sound/Sound.cpp
Expand Up @@ -70,6 +70,9 @@ void Init(int frequency, int resolution, int sources)
//alGenBuffers(2, musicBufs);
soundSourceCount = sources;
atexit(DieUnpleasantly);
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
alSpeedOfSound(1400.0);
alDopplerFactor(0.7);
}

static ALuint GetFreeSource()
Expand All @@ -86,22 +89,28 @@ void Preload(const std::string& name)

void PlaySound(const std::string& name, float gain)
{
const static ALfloat fz[] = { 0.0f, 0.0f, 0.0f };
ALuint buf = GetSound(name);
ALuint source = GetFreeSource();
alSourcei(source, AL_BUFFER, buf);
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef(source, AL_GAIN, gain);
alSourcefv(source, AL_POSITION, fz);
alSourcefv(source, AL_VELOCITY, fz);
alSourcePlay(source);
}

void PlaySoundPositional(const std::string& name, vec2 pos, float gain)
void PlaySoundPositional(const std::string& name, vec2 pos, vec2 vel, float gain)
{
ALfloat fpos[] = {pos.X(), pos.Y(), 0.0f};
ALfloat fvel[] = {vel.X(), vel.Y(), 0.0f};
ALuint buf = GetSound(name);
ALuint source = GetFreeSource();
alSourcei(source, AL_BUFFER, buf);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcefv(source, AL_POSITION, fpos);
alSourcefv(source, AL_VELOCITY, fvel);
alSourcef(source, AL_REFERENCE_DISTANCE, 50.0);
alSourcef(source, AL_GAIN, gain);
alSourcePlay(source);
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Sound/Sound.h
Expand Up @@ -33,7 +33,7 @@ void PlaySound ( const std::string& name, float gain = 1.0f );
* @param pos The origin of the sound
* @param gain The gain of the sound
*/
void PlaySoundPositional ( const std::string& name, vec2 pos, float gain = 1.0f );
void PlaySoundPositional ( const std::string& name, vec2 pos, vec2 vel = vec2(0.0f, 0.0f), float gain = 1.0f );
/**
* Set the position of the listener.
* @param pos The listener's position.
Expand Down
8 changes: 4 additions & 4 deletions Resources/Scripts/Modes/Demo4.lua
Expand Up @@ -298,6 +298,10 @@ function render()
graphics.draw_starfield(0.6)
graphics.draw_starfield(-0.3)
graphics.draw_starfield(-0.9)
-- DEBUG version, keep:
-- graphics.end_warp(scen.playerShip.warp.factor, scen.playerShip.physics.angle, cameraRatio, scen.playerShip.physics.position)
graphics.end_warp()
DrawGrid()
Expand All @@ -308,10 +312,6 @@ function render()
end
graphics.draw_particles()
-- DEBUG version, keep:
-- graphics.end_warp(scen.playerShip.warp.factor, scen.playerShip.physics.angle, cameraRatio, scen.playerShip.physics.position)
graphics.end_warp()
DrawObject(scen.playerShip)
DrawEffects()
Expand Down
1 change: 1 addition & 0 deletions Resources/Shaders/3DBase.fs
Expand Up @@ -3,3 +3,4 @@
#pragma import 3DPeturbationNone
#pragma import 3DDiffuseSphericalHarmonics
#pragma import 3DSpecularBasic
#pragma import 3DPostProcessNone
18 changes: 11 additions & 7 deletions Resources/Shaders/3DFragment.inc
@@ -1,29 +1,33 @@
varying vec2 TX;
varying vec3 N;
varying vec3 V;
varying vec3 T;

const vec3 EyeDir = vec3(0.0, 0.0, 1.0);

uniform sampler2D tex;
uniform sampler2D tex0;

uniform float shininess;
uniform float specularScale;

vec3 peturbNormal(in vec3 normal, in vec2 texCoords);
vec2 peturbTexCoords(in vec3 normal, in vec2 texCoords);
vec3 peturbNormal(in vec3 normal, in vec3 tangent, in vec2 texCoords);
vec2 peturbTexCoords(in vec3 normal, in vec3 tangent, in vec2 texCoords);
vec3 calculateDiffuseLight(in vec3 normal);
vec3 calculateSpecularLight(in vec3 normal, in float exponent);

vec3 lightDirection(in vec3 vertex);
vec3 lightColour(in vec3 vertex);

vec3 postprocessColour(in vec3 colour, in vec3 normal);

void main()
{
vec3 normal = normalize(N);
normal = peturbNormal(normal, TX);
vec2 texCoord = peturbTexCoords(normal, TX);
vec4 sample = texture2D(tex, texCoord);
vec3 tangent = normalize(T);
normal = peturbNormal(normal, T, TX);
vec2 texCoord = peturbTexCoords(normal, T, TX);
vec4 sample = texture2D(tex0, texCoord);
vec3 light = calculateDiffuseLight(normal);
light += sample.a * specularScale * calculateSpecularLight(normal, shininess);
gl_FragColor = vec4(light * sample.rgb, 1.0);
gl_FragColor = vec4(postprocessColour(light * sample.rgb, normal), 1.0);
}
1 change: 1 addition & 0 deletions Resources/Shaders/3DHighlight.fs
Expand Up @@ -3,3 +3,4 @@
#pragma ipmort 3DPeturbationNone
#pragma import 3DDiffuseBasic
#pragma import 3DSpecularBasic
#pragma import 3DPostProcessNone
4 changes: 2 additions & 2 deletions Resources/Shaders/3DPeturbationNone.inc
@@ -1,9 +1,9 @@
vec3 peturbNormal(in vec3 normal, in vec2 texCoords)
vec3 peturbNormal(in vec3 normal, in vec3 tangent, in vec2 texCoords)
{
return normal;
}

vec2 peturbTexCoords(in vec3 normal, in vec2 texCoords)
vec2 peturbTexCoords(in vec3 normal, in vec3 tangent, in vec2 texCoords)
{
return texCoords;
}
11 changes: 11 additions & 0 deletions Resources/Shaders/3DPostProcessCloak.inc
@@ -0,0 +1,11 @@
vec3 greyscale(in vec3 col)
{
return vec3(dot(col, vec3(0.21, 0.66, 0.13)));
}

vec3 postprocessColour(in vec3 colour, in vec3 normal)
{
float c = 1.0 - abs(normal.z);
c = pow(c, 4.0);
return mix(colour * 0.2, greyscale(normalize(colour)), c);
}
4 changes: 4 additions & 0 deletions Resources/Shaders/3DPostProcessNone.inc
@@ -0,0 +1,4 @@
vec3 postprocessColour(in vec3 colour, in vec3 normal)
{
return colour;
}
3 changes: 3 additions & 0 deletions Resources/Shaders/3DVertex.inc
@@ -1,12 +1,15 @@
varying vec2 TX;
varying vec3 N;
varying vec3 V;
varying vec3 T;
attribute vec3 Tangent;

void main()
{
vec4 worldPosition = gl_ModelViewMatrix * gl_Vertex;
V = worldPosition.xyz;
TX = gl_MultiTexCoord0.st;
N = gl_NormalMatrix * gl_Normal;
T = gl_NormalMatrix * Tangent;
gl_Position = gl_ProjectionMatrix * worldPosition;
}

0 comments on commit db81cef

Please sign in to comment.