Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More configuration options. Turning sound, bump mapping, decals, the …

…skybox, and clipping on and off
  • Loading branch information...
commit 96febe34de4dfc597be2259707268a272ec2fc4b 1 parent 5aadb42
@malexw authored
View
9 res/shaders/bump.frag
@@ -14,11 +14,6 @@ void main(void)
float diffuseIntensity = max(0.0, dot(normalize(normalAdjusted), -normalize(gl_LightSource[0].position.xyz)));
vec3 color = diffuseIntensity * (gl_FrontMaterial.diffuse).rgb + (gl_FrontMaterial.ambient).rgb;
- vec4 dec = texture2D(decal, vBumpCoords.st);
- if (dec.a < 0.1) {
- vec4 texel = texture2D(tex, vTexCoords.st);
- gl_FragColor = vec4(texel.rgb * color, texel.a * gl_FrontMaterial.diffuse.a);
- } else {
- gl_FragColor = vec4(dec.rgb * color, 1.0);
- }
+ vec4 texel = texture2D(tex, vTexCoords.st);
+ gl_FragColor = vec4(texel.rgb * color, texel.a * gl_FrontMaterial.diffuse.a);
}
View
24 res/shaders/bumpdec.frag
@@ -0,0 +1,24 @@
+uniform sampler2D tex;
+uniform sampler2D bump;
+uniform sampler2D decal;
+
+//varying vec4 diffuse, ambient;
+varying vec3 vVaryingNormal;
+//varying vec3 vVaryingLightDir;
+varying vec2 vTexCoords;
+varying vec2 vBumpCoords;
+
+void main(void)
+{
+ vec3 normalAdjusted = vVaryingNormal + normalize((texture2D(bump, vBumpCoords.st).rgb - 0.5)*2.0);
+ float diffuseIntensity = max(0.0, dot(normalize(normalAdjusted), -normalize(gl_LightSource[0].position.xyz)));
+
+ vec3 color = diffuseIntensity * (gl_FrontMaterial.diffuse).rgb + (gl_FrontMaterial.ambient).rgb;
+ vec4 dec = texture2D(decal, vBumpCoords.st);
+ if (dec.a < 0.1) {
+ vec4 texel = texture2D(tex, vTexCoords.st);
+ gl_FragColor = vec4(texel.rgb * color, texel.a * gl_FrontMaterial.diffuse.a);
+ } else {
+ gl_FragColor = vec4(dec.rgb * color, 1.0);
+ }
+}
View
19 res/shaders/bumpdec.vert
@@ -0,0 +1,19 @@
+//varying vec4 diffuse, ambient;
+varying vec3 vVaryingNormal;
+//varying vec3 vVaryingLightDir;
+varying vec2 vTexCoords;
+varying vec2 vBumpCoords;
+
+void main(void)
+{
+
+ vVaryingNormal = gl_NormalMatrix * gl_Normal;
+ vTexCoords = gl_MultiTexCoord0.st;
+ vBumpCoords = gl_MultiTexCoord0.st;
+
+ //diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
+ //ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
+ //ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
+
+ gl_Position = ftransform();
+}
View
16 src/CosmosConfig.hpp
@@ -23,6 +23,9 @@ class CosmosConfig {
void set_hdr(bool value) { hdr_ = value; invalidate(); }
bool is_hdr() const { return hdr_; }
+ void set_skybox(bool value) { skybox_ = value; }
+ bool is_skybox() const { return skybox_; }
+
void set_textures(bool value) { textures_ = value; invalidate(); }
bool is_textures() const { return textures_; }
@@ -37,6 +40,15 @@ class CosmosConfig {
void set_collidables(bool value) { collidables_ = value; }
bool is_collidables() const { return collidables_; }
+
+ void set_collisions(bool value) { collisions_ = value; }
+ bool is_collisions() const { return collisions_; }
+
+ void set_shadows(bool value) { shadows_ = value; invalidate(); }
+ bool is_shadows() const { return shadows_; }
+
+ void set_sounds(bool value) { sounds_ = value; invalidate(); }
+ bool is_sounds() const { return sounds_; }
private:
bool valid_;
@@ -46,6 +58,10 @@ class CosmosConfig {
bool decals_;
bool particles_;
bool collidables_;
+ bool collisions_;
+ bool skybox_;
+ bool shadows_;
+ bool sounds_;
DISALLOW_COPY_AND_ASSIGN(CosmosConfig);
};
View
6 src/PlayerInputHandler.cpp
@@ -26,12 +26,16 @@ void PlayerInputHandler::handleInput(SDL_Event e) {
case SDLK_s: velo_.z() = 0; collidable_->set_velocity(velo_); break;
case SDLK_d: velo_.x() = 0; collidable_->set_velocity(velo_); break;
// Config stuff
- case SDLK_c: config_.set_collidables(!config_.is_collidables()); break;
+ case SDLK_v: config_.set_collidables(!config_.is_collidables()); break;
+ case SDLK_c: config_.set_collisions(!config_.is_collisions()); break;
case SDLK_b: config_.set_bump_mapping(!config_.is_bump_mapping()); break;
case SDLK_t: config_.set_textures(!config_.is_textures()); break;
+ case SDLK_k: config_.set_skybox(!config_.is_skybox()); break;
case SDLK_h: config_.set_hdr(!config_.is_hdr()); break;
case SDLK_y: config_.set_decals(!config_.is_decals()); break;
case SDLK_p: config_.set_particles(!config_.is_particles()); break;
+ case SDLK_n: config_.set_shadows(!config_.is_shadows()); break;
+ case SDLK_m: config_.set_sounds(!config_.is_sounds()); break;
// HDR exposure
case SDLK_1: hdr_program_->setf(std::string("exposure"), 1.0f); break;
case SDLK_2: hdr_program_->setf(std::string("exposure"), 2.0f); break;
View
12 src/Renderable.cpp
@@ -19,7 +19,7 @@ void Renderable::render() const {
glBindTexture(GL_TEXTURE_2D, material_->get_texture()->get_index());
}
mesh_->draw();
- if (material_->is_bump_mapped()) {
+ if (material_->is_bump_mapped() && CosmosConfig::get().is_bump_mapping()) {
glActiveTexture(GL_TEXTURE1);
glClientActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
@@ -28,17 +28,17 @@ void Renderable::render() const {
// assume true for now
//if (material_->has_decals()) {
+ if (CosmosConfig::get().is_decals()) {
glActiveTexture(GL_TEXTURE2);
//glEnable(GL_TEXTURE_2D); // probably never necessary
//glEnableClientState(GL_TEXTURE_COORD_ARRAY); // no need for this for now
glBindTexture(GL_TEXTURE_2D, TextureManager::get().get_texture("res/textures/decal-test.png")->get_index());
- //}
-
+ ShaderManager::get().get_shader_program("bumpdec")->run();
+ } else {
+ ShaderManager::get().get_shader_program("bump")->run();
+ }
//std::cout << "Tex " << material_->get_texture()->get_index() << " Bump " << material_->get_bump_tex()->get_index() << std::endl;
- ShaderProgram::ShPtr prog = ShaderManager::get().get_shader_program("bump");
- prog->run();
-
mesh_->set_tex_pointer();
glDrawArrays(GL_TRIANGLES, 0, mesh_->triangle_count() * 3);
View
29 src/ResourceManager/ShaderManager.cpp
@@ -14,12 +14,14 @@ ShaderManager::ShaderManager()
* hand
*/
void ShaderManager::init() {
- shader_names_.push_back(std::string("res/shaders/bump.vert"));
- shader_names_.push_back(std::string("res/shaders/bump.frag"));
+ shader_names_.push_back(std::string("res/shaders/bumpdec.vert"));
+ shader_names_.push_back(std::string("res/shaders/bumpdec.frag"));
shader_names_.push_back(std::string("res/shaders/shadow.vert"));
shader_names_.push_back(std::string("res/shaders/shadow.frag"));
shader_names_.push_back(std::string("res/shaders/hdr.vert"));
shader_names_.push_back(std::string("res/shaders/hdr.frag"));
+ shader_names_.push_back(std::string("res/shaders/bump.vert"));
+ shader_names_.push_back(std::string("res/shaders/bump.frag"));
load_shaders();
}
@@ -65,7 +67,7 @@ void ShaderManager::load_shaders() {
std::cout << "ShaderManager error: shader type not recognized" << std::endl;
}
}
- // The bump program
+ // The bumpdec program
int p = glCreateProgram();
int v = vshaders_[0]->get_id();
int f = fshaders_[0]->get_id();
@@ -75,7 +77,7 @@ void ShaderManager::load_shaders() {
glLinkProgram(p);
print_program_log(p);
- ShaderProgram::ShPtr program(new ShaderProgram("bump", p));
+ ShaderProgram::ShPtr program(new ShaderProgram("bumpdec", p));
programs_.push_back(program);
glUseProgram(p);
@@ -119,6 +121,25 @@ void ShaderManager::load_shaders() {
//GLint shadowSampler = glGetUniformLocation(p, "shadowMap");
glUniform1f(exp, 1.0);
//glUniform1i(shadowSampler, 3);
+
+ // The bump program
+ p = glCreateProgram();
+ v = vshaders_[3]->get_id();
+ f = fshaders_[3]->get_id();
+
+ glAttachShader(p,v);
+ glAttachShader(p,f);
+
+ glLinkProgram(p);
+ print_program_log(p);
+ ShaderProgram::ShPtr bump(new ShaderProgram("bump", p));
+ programs_.push_back(bump);
+ glUseProgram(p);
+
+ texSampler = glGetUniformLocation(p, "tex");
+ bumpSampler = glGetUniformLocation(p, "bump");
+ glUniform1i(texSampler, 0);
+ glUniform1i(bumpSampler, 1);
glUseProgram(0);
}
View
4 src/Sound.cpp
@@ -24,6 +24,10 @@ void Sound::play() {
alSourcePlay(sound_index_);
}
+void Sound::pause() {
+ alSourcePause(sound_index_);
+}
+
void Sound::set_gain(float gain) {
alSourcef(sound_index_, AL_GAIN, gain);
}
View
3  src/Sound.hpp
@@ -26,7 +26,8 @@ class Sound {
// Return the source index of the sound. This value is meaningless unless the Sound is loaded.
const int get_index() const;
- void play();
+ void play();
+ void pause();
void set_gain(float gain);
void set_looping(bool loop);
void set_position(const Vector3f& pos);
View
132 src/main.cpp
@@ -181,7 +181,6 @@ int main(int argc, char* argv[]) {
part_sound->set_gain(1.0f);
part_sound->set_looping(true);
part_sound->set_rolloff(0.5f);
- part_sound->play();
ParticleEmitter::ShPtr emitter(new ParticleEmitter(particle_renderable, Vector3f(7, 1, -20), Vector3f::UNIT_Y, Vector3f::UNIT_X, 3.0f, 2.0f, 20, 30));
//Particle::ShPtr part(new Particle(ren2));
@@ -215,14 +214,22 @@ int main(int argc, char* argv[]) {
} else {
skybox_renderable->set_mesh(MeshManager::get().get_mesh("res/meshes/skybox.obj")).set_material(MaterialManager::get().get_material("res/materials/skybox.mtl"));
}
-
if (!config.is_textures()) {
glBindTexture(GL_TEXTURE_2D, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
-
+ if (!config.is_sounds()) {
+ part_sound->pause();
+ } else {
+ part_sound->play();
+ }
+ if (!config.is_shadows()) {
+ glActiveTexture(GL_TEXTURE3);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ }
+
config.set_valid();
}
@@ -236,7 +243,9 @@ int main(int argc, char* argv[]) {
r += 1.0f;
// Collisions
- camera_collidable->check(cube_collidable);
+ if (config.is_collisions()) {
+ camera_collidable->check(cube_collidable);
+ }
//camera_collidable->gjk(cube_collidable);
// Reupdate
@@ -245,61 +254,63 @@ int main(int argc, char* argv[]) {
AudioManager::get().set_listener_transform(camera_transform);
//-------------- First pass for shadows
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadowBuffer); //Rendering offscreen
- glUseProgram(0);
- glViewport(0,0,1024,1024);
- glClear(GL_DEPTH_BUFFER_BIT);
- glCullFace(GL_FRONT);
- glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45,1,10,40000);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMultMatrixf(Camera::matrixFromPositionDirection(Vector3f(5, 15, 5), Vector3f(5, 0, -30)-Vector3f(5, 15, 5)).to_array());
-
- // Draw all the things that should cast shadows
- world->draw_geometry();
+ if (config.is_shadows()) {
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadowBuffer); //Rendering offscreen
+ glUseProgram(0);
+ glViewport(0,0,1024,1024);
+ glClear(GL_DEPTH_BUFFER_BIT);
+ glCullFace(GL_FRONT);
+ glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(45,1,10,40000);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glMultMatrixf(Camera::matrixFromPositionDirection(Vector3f(5, 15, 5), Vector3f(5, 0, -30)-Vector3f(5, 15, 5)).to_array());
+
+ // Draw all the things that should cast shadows
+ world->draw_geometry();
- glPushMatrix();
- glTranslatef(2.0f,1.0f,-12.0f);
- glRotatef(r, 0.0f, 1.0f, 0.0f);
- glRotatef(r/2, 1.0f, 0.0f, 0.0f);
- glMatrixMode(GL_TEXTURE);
- glPushMatrix();
- glTranslatef(2.0f,1.0f,-12.0f);
- glRotatef(r, 0.0f, 1.0f, 0.0f);
- glRotatef(r/2, 1.0f, 0.0f, 0.0f);
- //
- cube_renderable->draw_geometry();
- //
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
-
- // Configure the shadow texture
- double modelView[16];
- double projection[16];
+ glPushMatrix();
+ glTranslatef(2.0f,1.0f,-12.0f);
+ glRotatef(r, 0.0f, 1.0f, 0.0f);
+ glRotatef(r/2, 1.0f, 0.0f, 0.0f);
+ glMatrixMode(GL_TEXTURE);
+ glPushMatrix();
+ glTranslatef(2.0f,1.0f,-12.0f);
+ glRotatef(r, 0.0f, 1.0f, 0.0f);
+ glRotatef(r/2, 1.0f, 0.0f, 0.0f);
+ //
+ cube_renderable->draw_geometry();
+ //
+ glPopMatrix();
+ glMatrixMode(GL_MODELVIEW);
+ glPopMatrix();
+
+ // Configure the shadow texture
+ double modelView[16];
+ double projection[16];
- const GLdouble bias[16] = {
- 0.5, 0.0, 0.0, 0.0,
- 0.0, 0.5, 0.0, 0.0,
- 0.0, 0.0, 0.5, 0.0,
- 0.5, 0.5, 0.5, 1.0};
+ const GLdouble bias[16] = {
+ 0.5, 0.0, 0.0, 0.0,
+ 0.0, 0.5, 0.0, 0.0,
+ 0.0, 0.0, 0.5, 0.0,
+ 0.5, 0.5, 0.5, 1.0};
- glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
- glGetDoublev(GL_PROJECTION_MATRIX, projection);
+ glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
+ glGetDoublev(GL_PROJECTION_MATRIX, projection);
- glMatrixMode(GL_TEXTURE);
- glActiveTexture(GL_TEXTURE3);
+ glMatrixMode(GL_TEXTURE);
+ glActiveTexture(GL_TEXTURE3);
- glLoadIdentity();
- glLoadMatrixd(bias);
- glMultMatrixd(projection);
- glMultMatrixd(modelView);
+ glLoadIdentity();
+ glLoadMatrixd(bias);
+ glMultMatrixd(projection);
+ glMultMatrixd(modelView);
- glMatrixMode(GL_MODELVIEW);
+ glMatrixMode(GL_MODELVIEW);
+ }
//-------------- Second pass for skybox
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, hdrFrameBuffer);
@@ -324,7 +335,10 @@ int main(int argc, char* argv[]) {
if (config.is_hdr()) {
ShaderManager::get().get_shader_program("hdr")->run();
}
+ glPushMatrix();
+ glRotatef(180, 0.0, 1.0f, 0.0);
skybox_renderable->render();
+ glPopMatrix();
glUseProgram(0);
glFrontFace(GL_CCW);
glEnable(GL_LIGHTING);
@@ -341,7 +355,7 @@ int main(int argc, char* argv[]) {
// "skybox"
// Skip drawing the background when textures are off since it obscures everything
- if (config.is_textures()) {
+ if (config.is_textures() && config.is_skybox()) {
glPushMatrix();
glDisable(GL_LIGHTING);
glTranslatef(0.0f, 0.0f, -2.0f);
@@ -357,10 +371,12 @@ int main(int argc, char* argv[]) {
glClear(GL_DEPTH_BUFFER_BIT);
- ShaderManager::get().get_shader_program("shadow")->run();
- glActiveTexture(GL_TEXTURE3);
- glBindTexture(GL_TEXTURE_2D,TextureManager::get().get_texture("shadow_map")->get_index());
- glActiveTexture(GL_TEXTURE0);
+ if (config.is_shadows()) {
+ ShaderManager::get().get_shader_program("shadow")->run();
+ glActiveTexture(GL_TEXTURE3);
+ glBindTexture(GL_TEXTURE_2D,TextureManager::get().get_texture("shadow_map")->get_index());
+ glActiveTexture(GL_TEXTURE0);
+ }
camera_transform->apply_inverse();
//glPushMatrix();
Please sign in to comment.
Something went wrong with that request. Please try again.