Skip to content

Commit

Permalink
#12 flash lights
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed Nov 16, 2016
1 parent 08dc561 commit 0921c61
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 101 deletions.
Binary file modified bin/OpenLara.exe
Binary file not shown.
1 change: 1 addition & 0 deletions src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ struct Controller {

Core::active.shader->setParam(uModel, m);
Core::active.shader->setParam(uColor, vec4(0.0f, 0.0f, 0.0f, 0.5f));
Core::active.shader->setParam(uAmbient, vec3(0.0f));
mesh->renderShadowSpot();
}

Expand Down
9 changes: 7 additions & 2 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
#endif
#endif

#define MAX_LIGHTS 3

struct Shader;
struct Texture;

Expand Down Expand Up @@ -113,8 +115,8 @@ namespace Core {
float deltaTime;
mat4 mView, mProj, mViewProj, mViewInv, mModel;
vec3 viewPos;
vec3 lightPos;
vec4 lightColor;
vec3 lightPos[MAX_LIGHTS];
vec4 lightColor[MAX_LIGHTS];
vec3 ambient;
vec4 color;

Expand Down Expand Up @@ -185,6 +187,9 @@ namespace Core {
support.VAO = (void*)glBindVertexArray != NULL;

Sound::init();

for (int i = 0; i < MAX_LIGHTS; i++)
lightColor[i] = vec4(0, 0, 0, 1);
}

void free() {
Expand Down
10 changes: 10 additions & 0 deletions src/lara.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#define DESCENT_SPEED 2048.0f
#define MUZZLE_FLASH_TIME 0.1f
#define FLASH_LIGHT_COLOR vec4(0.8f, 0.7f, 0.3f, 2048 * 2048)
#define TARGET_MAX_DIST (8.0f * 1024.0f)

struct Lara : Controller {
Expand Down Expand Up @@ -490,12 +491,15 @@ struct Lara : Controller {

for (int i = 0; i < count; i++) {
Arm *arm;
int armIndex;
if (wpnCurrent == Weapon::SHOTGUN) {
if (!rightHand) continue;
arm = &arms[0];
armIndex = 0;
} else {
if (!(i ? leftHand : rightHand)) continue;
arm = &arms[i];
armIndex = i;
}

arm->shotTimer = 0.0f;
Expand Down Expand Up @@ -523,6 +527,9 @@ struct Lara : Controller {
nearDist = dist;
}
}

Core::lightPos[1 + armIndex] = getJoint(armIndex == 0 ? 10 : 13, false).getPos();
Core::lightColor[1 + armIndex] = FLASH_LIGHT_COLOR;
}

if (hasShot) {
Expand Down Expand Up @@ -571,6 +578,9 @@ struct Lara : Controller {
for (int i = 0; i < 2; i++){
arms[i].animTime += Core::deltaTime * arms[i].animDir;
arms[i].shotTimer += Core::deltaTime;

float intensity = clamp((0.1f - arms[i].shotTimer) * 20.0f, 0.0f, 1.0f);
Core::lightColor[1 + i] = FLASH_LIGHT_COLOR * vec4(intensity, intensity, intensity, sqrtf(intensity));
}

if (isRifle)
Expand Down
57 changes: 32 additions & 25 deletions src/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ struct Level {

void initShaders() {
char def[255], ext[255];
sprintf(def, "#define MAX_RANGES %d\n#define MAX_OFFSETS %d\n", mesh->animTexRangesCount, mesh->animTexOffsetsCount);
sprintf(def, "#define MAX_LIGHTS %d\n#define MAX_RANGES %d\n#define MAX_OFFSETS %d\n", MAX_LIGHTS, mesh->animTexRangesCount, mesh->animTexOffsetsCount);
shaders[shStatic] = new Shader(SHADER, def);
sprintf(ext, "%s#define CAUSTICS\n", def);
sprintf(ext, "#define MAX_LIGHTS %d\n%s#define CAUSTICS\n", MAX_LIGHTS, def);
shaders[shCaustics] = new Shader(SHADER, ext);
sprintf(ext, "%s#define SPRITE\n", def);
sprintf(ext, "#define MAX_LIGHTS %d\n%s#define SPRITE\n", MAX_LIGHTS, def);
shaders[shSprite] = new Shader(SHADER, ext);
}

Expand Down Expand Up @@ -238,11 +238,12 @@ struct Level {

sh->bind();
sh->setParam(uColor, Core::color);
sh->setParam(uLightColor, Core::lightColor[0], MAX_LIGHTS);
sh->setParam(uLightPos, Core::lightPos[0], MAX_LIGHTS);
sh->setParam(uAmbient, vec3(0.0f));//Core::ambient);

// room static meshes
{
PROFILE_MARKER("R_MESH");

for (int i = 0; i < room.meshesCount; i++) {
TR::Room::Mesh &rMesh = room.meshes[i];
if (rMesh.flags.rendered) continue; // skip if already rendered
Expand All @@ -261,11 +262,17 @@ struct Level {
// set light parameters
getLight(offset, roomIndex);

if (rMesh.intensity >= 0) {
Core::ambient = vec3(intensity(rMesh.intensity) / 255.0f);
Core::ambient = vec3(0.0);
sh->setParam(uAmbient, Core::ambient);
}

// render static mesh
mat4 mTemp = Core::mModel;
Core::mModel.translate(offset);
Core::mModel.rotateY(rMesh.rotation);
Core::active.shader->setParam(uModel, Core::mModel);
sh->setParam(uModel, Core::mModel);
mesh->renderMesh(mesh->meshMap[sMesh->mesh]);
Core::mModel = mTemp;
}
Expand All @@ -275,30 +282,30 @@ struct Level {
if (!room.flags.rendered) { // skip if already rendered

mat4 mTemp = Core::mModel;
{
PROFILE_MARKER("R_GEOM");
room.flags.rendered = true;

room.flags.rendered = true;
Core::lightColor[0] = vec4(0, 0, 0, 1);
Core::ambient = vec3(0.0);

Core::lightColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
Core::ambient = vec3(1.0f);
sh->setParam(uLightColor, Core::lightColor);
sh->setParam(uAmbient, Core::ambient);
sh->setParam(uLightColor, Core::lightColor[0], MAX_LIGHTS);
sh->setParam(uLightPos, Core::lightPos[0], MAX_LIGHTS);
sh->setParam(uAmbient, Core::ambient);

Core::mModel.translate(offset);
Core::mModel.translate(offset);

// render room geometry
sh->setParam(uModel, Core::mModel);
mesh->renderRoomGeometry(roomIndex);
}
// render room geometry
sh->setParam(uModel, Core::mModel);
mesh->renderRoomGeometry(roomIndex);

// render room sprites
if (mesh->hasRoomSprites(roomIndex)) {
PROFILE_MARKER("R_SPR");
sh = shaders[shSprite];
sh->bind();
sh->setParam(uModel, Core::mModel);
sh->setParam(uColor, Core::color);
sh->setParam(uLightColor, Core::lightColor[0], MAX_LIGHTS);
sh->setParam(uLightPos, Core::lightPos[0], MAX_LIGHTS);
sh->setParam(uAmbient, vec3(0.0f));//Core::ambient);
mesh->renderRoomSprites(roomIndex);
}

Expand Down Expand Up @@ -353,17 +360,17 @@ struct Level {
if (idx > -1) {
TR::Room::Light &light = level.rooms[room].lights[idx];
float c = level.rooms[room].lights[idx].intensity / 8191.0f;
Core::lightPos = vec3(light.x, light.y, light.z);
Core::lightColor = vec4(c, c, c, (float)light.attenuation * (float)light.attenuation);
Core::lightPos[0] = vec3(light.x, light.y, light.z);
Core::lightColor[0] = vec4(c, c, c, (float)light.attenuation * (float)light.attenuation);
} else {
Core::lightPos = vec3(0);
Core::lightColor = vec4(0, 0, 0, 1);
Core::lightPos[0] = vec3(0);
Core::lightColor[0] = vec4(0, 0, 0, 1);
}

Core::ambient = vec3(1.0f - level.rooms[roomIndex].ambient / 8191.0f);
Core::active.shader->setParam(uAmbient, Core::ambient);
Core::active.shader->setParam(uLightPos, Core::lightPos);
Core::active.shader->setParam(uLightColor, Core::lightColor);
Core::active.shader->setParam(uLightPos, Core::lightPos[0], MAX_LIGHTS);
Core::active.shader->setParam(uLightColor, Core::lightColor[0], MAX_LIGHTS);
}

void renderEntity(const TR::Entity &entity) {
Expand Down
Loading

0 comments on commit 0921c61

Please sign in to comment.