Permalink
Browse files

Initial shadow volume code.

git-svn-id: http://ogldev.googlecode.com/svn/trunk@108 3d42d2e4-7448-fe72-c4ee-b187089d8254
  • Loading branch information...
1 parent b12931f commit 24115538e7dff6584e623d16f2e158ec7c14a011 etay.meiri committed Aug 4, 2012
@@ -115,8 +115,9 @@ void GLUTBackendRun(ICallbacks* pCallbacks)
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
+
+ // glDisable(GL_CULL_FACE);
+ glEnable(GL_CULL_FACE);
s_pCallbacks = pCallbacks;
InitCallbacks();
@@ -0,0 +1,237 @@
+/*
+
+ Copyright 2011 Etay Meiri
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <limits.h>
+#include <string>
+#include <glfx.h>
+
+#include "lighting_technique.h"
+#include "util.h"
+
+using namespace std;
+
+static const char* pEffectFile = "shaders/lighting.glsl";
+
+LightingTechnique::LightingTechnique() : Technique(pEffectFile)
+{
+}
+
+
+bool LightingTechnique::Init()
+{
+ if (!CompileProgram("Lighting")) {
+ return false;
+ }
+
+ m_WVPLocation = GetUniformLocation("gWVP");
+ m_WorldMatrixLocation = GetUniformLocation("gWorld");
+ m_colorTextureLocation = GetUniformLocation("gColorMap");
+ m_eyeWorldPosLocation = GetUniformLocation("gEyeWorldPos");
+ m_dirLightLocation.Color = GetUniformLocation("gDirectionalLight.Base.Color");
+ m_dirLightLocation.AmbientIntensity = GetUniformLocation("gDirectionalLight.Base.AmbientIntensity");
+ m_dirLightLocation.Direction = GetUniformLocation("gDirectionalLight.Direction");
+ m_dirLightLocation.DiffuseIntensity = GetUniformLocation("gDirectionalLight.Base.DiffuseIntensity");
+ m_matSpecularIntensityLocation = GetUniformLocation("gMatSpecularIntensity");
+ m_matSpecularPowerLocation = GetUniformLocation("gSpecularPower");
+ m_numPointLightsLocation = GetUniformLocation("gNumPointLights");
+ m_numSpotLightsLocation = GetUniformLocation("gNumSpotLights");
+ m_colorLocation = GetUniformLocation("gColor");
+
+ if (m_dirLightLocation.AmbientIntensity == INVALID_UNIFORM_LOCATION ||
+ m_WVPLocation == INVALID_UNIFORM_LOCATION ||
+ m_WorldMatrixLocation == INVALID_UNIFORM_LOCATION ||
+ m_colorTextureLocation == INVALID_UNIFORM_LOCATION ||
+ m_eyeWorldPosLocation == INVALID_UNIFORM_LOCATION ||
+ m_dirLightLocation.Color == INVALID_UNIFORM_LOCATION ||
+ m_dirLightLocation.DiffuseIntensity == INVALID_UNIFORM_LOCATION ||
+ m_dirLightLocation.Direction == INVALID_UNIFORM_LOCATION ||
+ m_matSpecularIntensityLocation == INVALID_UNIFORM_LOCATION ||
+ m_matSpecularPowerLocation == INVALID_UNIFORM_LOCATION ||
+ m_numPointLightsLocation == INVALID_UNIFORM_LOCATION ||
+ m_numSpotLightsLocation == INVALID_UNIFORM_LOCATION ||
+ m_colorLocation == INVALID_UNIFORM_LOCATION) {
+ return false;
+ }
+
+ for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_pointLightsLocation) ; i++) {
+ char Name[128];
+ memset(Name, 0, sizeof(Name));
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Base.Color", i);
+ m_pointLightsLocation[i].Color = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Base.AmbientIntensity", i);
+ m_pointLightsLocation[i].AmbientIntensity = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Position", i);
+ m_pointLightsLocation[i].Position = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Base.DiffuseIntensity", i);
+ m_pointLightsLocation[i].DiffuseIntensity = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Constant", i);
+ m_pointLightsLocation[i].Atten.Constant = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Linear", i);
+ m_pointLightsLocation[i].Atten.Linear = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Exp", i);
+ m_pointLightsLocation[i].Atten.Exp = GetUniformLocation(Name);
+
+ if (m_pointLightsLocation[i].Color == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].AmbientIntensity == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].Position == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].DiffuseIntensity == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].Atten.Constant == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].Atten.Linear == INVALID_UNIFORM_LOCATION ||
+ m_pointLightsLocation[i].Atten.Exp == INVALID_UNIFORM_LOCATION) {
+ return false;
+ }
+ }
+
+ for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_spotLightsLocation) ; i++) {
+ char Name[128];
+ memset(Name, 0, sizeof(Name));
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.Color", i);
+ m_spotLightsLocation[i].Color = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.AmbientIntensity", i);
+ m_spotLightsLocation[i].AmbientIntensity = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Position", i);
+ m_spotLightsLocation[i].Position = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Direction", i);
+ m_spotLightsLocation[i].Direction = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Cutoff", i);
+ m_spotLightsLocation[i].Cutoff = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.DiffuseIntensity", i);
+ m_spotLightsLocation[i].DiffuseIntensity = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Constant", i);
+ m_spotLightsLocation[i].Atten.Constant = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Linear", i);
+ m_spotLightsLocation[i].Atten.Linear = GetUniformLocation(Name);
+
+ snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Exp", i);
+ m_spotLightsLocation[i].Atten.Exp = GetUniformLocation(Name);
+
+ if (m_spotLightsLocation[i].Color == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].AmbientIntensity == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Position == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Direction == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Cutoff == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].DiffuseIntensity == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Atten.Constant == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Atten.Linear == INVALID_UNIFORM_LOCATION ||
+ m_spotLightsLocation[i].Atten.Exp == INVALID_UNIFORM_LOCATION) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void LightingTechnique::SetWVP(const Matrix4f& WVP)
+{
+ glUniformMatrix4fv(m_WVPLocation, 1, GL_TRUE, (const GLfloat*)WVP.m);
+}
+
+
+void LightingTechnique::SetWorldMatrix(const Matrix4f& WorldInverse)
+{
+ glUniformMatrix4fv(m_WorldMatrixLocation, 1, GL_TRUE, (const GLfloat*)WorldInverse.m);
+}
+
+
+void LightingTechnique::SetColorTextureUnit(unsigned int TextureUnit)
+{
+ glUniform1i(m_colorTextureLocation, TextureUnit);
+}
+
+
+void LightingTechnique::SetDirectionalLight(const DirectionalLight& Light)
+{
+ glUniform3f(m_dirLightLocation.Color, Light.Color.x, Light.Color.y, Light.Color.z);
+ glUniform1f(m_dirLightLocation.AmbientIntensity, Light.AmbientIntensity);
+ Vector3f Direction = Light.Direction;
+ Direction.Normalize();
+ glUniform3f(m_dirLightLocation.Direction, Direction.x, Direction.y, Direction.z);
+ glUniform1f(m_dirLightLocation.DiffuseIntensity, Light.DiffuseIntensity);
+}
+
+
+void LightingTechnique::SetEyeWorldPos(const Vector3f& EyeWorldPos)
+{
+ glUniform3f(m_eyeWorldPosLocation, EyeWorldPos.x, EyeWorldPos.y, EyeWorldPos.z);
+}
+
+
+void LightingTechnique::SetMatSpecularIntensity(float Intensity)
+{
+ glUniform1f(m_matSpecularIntensityLocation, Intensity);
+}
+
+
+void LightingTechnique::SetMatSpecularPower(float Power)
+{
+ glUniform1f(m_matSpecularPowerLocation, Power);
+}
+
+
+void LightingTechnique::SetPointLights(unsigned int NumLights, const PointLight* pLights)
+{
+ glUniform1i(m_numPointLightsLocation, NumLights);
+
+ for (unsigned int i = 0 ; i < NumLights ; i++) {
+ glUniform3f(m_pointLightsLocation[i].Color, pLights[i].Color.x, pLights[i].Color.y, pLights[i].Color.z);
+ glUniform1f(m_pointLightsLocation[i].AmbientIntensity, pLights[i].AmbientIntensity);
+ glUniform1f(m_pointLightsLocation[i].DiffuseIntensity, pLights[i].DiffuseIntensity);
+ glUniform3f(m_pointLightsLocation[i].Position, pLights[i].Position.x, pLights[i].Position.y, pLights[i].Position.z);
+ glUniform1f(m_pointLightsLocation[i].Atten.Constant, pLights[i].Attenuation.Constant);
+ glUniform1f(m_pointLightsLocation[i].Atten.Linear, pLights[i].Attenuation.Linear);
+ glUniform1f(m_pointLightsLocation[i].Atten.Exp, pLights[i].Attenuation.Exp);
+ }
+}
+
+void LightingTechnique::SetSpotLights(unsigned int NumLights, const SpotLight* pLights)
+{
+ glUniform1i(m_numSpotLightsLocation, NumLights);
+
+ for (unsigned int i = 0 ; i < NumLights ; i++) {
+ glUniform3f(m_spotLightsLocation[i].Color, pLights[i].Color.x, pLights[i].Color.y, pLights[i].Color.z);
+ glUniform1f(m_spotLightsLocation[i].AmbientIntensity, pLights[i].AmbientIntensity);
+ glUniform1f(m_spotLightsLocation[i].DiffuseIntensity, pLights[i].DiffuseIntensity);
+ glUniform3f(m_spotLightsLocation[i].Position, pLights[i].Position.x, pLights[i].Position.y, pLights[i].Position.z);
+ Vector3f Direction = pLights[i].Direction;
+ Direction.Normalize();
+ glUniform3f(m_spotLightsLocation[i].Direction, Direction.x, Direction.y, Direction.z);
+ glUniform1f(m_spotLightsLocation[i].Cutoff, cosf(ToRadian(pLights[i].Cutoff)));
+ glUniform1f(m_spotLightsLocation[i].Atten.Constant, pLights[i].Attenuation.Constant);
+ glUniform1f(m_spotLightsLocation[i].Atten.Linear, pLights[i].Attenuation.Linear);
+ glUniform1f(m_spotLightsLocation[i].Atten.Exp, pLights[i].Attenuation.Exp);
+ }
+}
+
+
+void LightingTechnique::SetColor(const Vector4f& Color)
+{
+ glUniform4f(m_colorLocation, Color.x, Color.y, Color.z, Color.w);
+}
@@ -0,0 +1,148 @@
+/*
+ Copyright 2011 Etay Meiri
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIGHTING_TECHNIQUE_H
+#define LIGHTING_TECHNIQUE_H
+
+#include "technique.h"
+#include "math_3d.h"
+
+struct BaseLight
+{
+ Vector3f Color;
+ float AmbientIntensity;
+ float DiffuseIntensity;
+
+ BaseLight()
+ {
+ Color = Vector3f(0.0f, 0.0f, 0.0f);
+ AmbientIntensity = 0.0f;
+ DiffuseIntensity = 0.0f;
+ }
+};
+
+struct DirectionalLight : public BaseLight
+{
+ Vector3f Direction;
+
+ DirectionalLight()
+ {
+ Direction = Vector3f(0.0f, 0.0f, 0.0f);
+ }
+};
+
+struct PointLight : public BaseLight
+{
+ Vector3f Position;
+
+ struct
+ {
+ float Constant;
+ float Linear;
+ float Exp;
+ } Attenuation;
+
+ PointLight()
+ {
+ Position = Vector3f(0.0f, 0.0f, 0.0f);
+ Attenuation.Constant = 1.0f;
+ Attenuation.Linear = 0.0f;
+ Attenuation.Exp = 0.0f;
+ }
+};
+
+struct SpotLight : public PointLight
+{
+ Vector3f Direction;
+ float Cutoff;
+
+ SpotLight()
+ {
+ Direction = Vector3f(0.0f, 0.0f, 0.0f);
+ Cutoff = 0.0f;
+ }
+};
+
+class LightingTechnique : public Technique {
+public:
+
+ static const unsigned int MAX_POINT_LIGHTS = 2;
+ static const unsigned int MAX_SPOT_LIGHTS = 2;
+
+ LightingTechnique();
+
+ virtual bool Init();
+
+ void SetWVP(const Matrix4f& WVP);
+ void SetWorldMatrix(const Matrix4f& WVP);
+ void SetColorTextureUnit(unsigned int TextureUnit);
+ void SetDirectionalLight(const DirectionalLight& Light);
+ void SetPointLights(unsigned int NumLights, const PointLight* pLights);
+ void SetSpotLights(unsigned int NumLights, const SpotLight* pLights);
+ void SetEyeWorldPos(const Vector3f& EyeWorldPos);
+ void SetMatSpecularIntensity(float Intensity);
+ void SetMatSpecularPower(float Power);
+ void SetColor(const Vector4f& Color);
+
+private:
+
+ GLuint m_WVPLocation;
+ GLuint m_WorldMatrixLocation;
+ GLuint m_colorTextureLocation;
+ GLuint m_eyeWorldPosLocation;
+ GLuint m_matSpecularIntensityLocation;
+ GLuint m_matSpecularPowerLocation;
+ GLuint m_numPointLightsLocation;
+ GLuint m_numSpotLightsLocation;
+ GLuint m_colorLocation;
+
+ struct {
+ GLuint Color;
+ GLuint AmbientIntensity;
+ GLuint DiffuseIntensity;
+ GLuint Direction;
+ } m_dirLightLocation;
+
+ struct {
+ GLuint Color;
+ GLuint AmbientIntensity;
+ GLuint DiffuseIntensity;
+ GLuint Position;
+ struct {
+ GLuint Constant;
+ GLuint Linear;
+ GLuint Exp;
+ } Atten;
+ } m_pointLightsLocation[MAX_POINT_LIGHTS];
+
+ struct {
+ GLuint Color;
+ GLuint AmbientIntensity;
+ GLuint DiffuseIntensity;
+ GLuint Position;
+ GLuint Direction;
+ GLuint Cutoff;
+ struct {
+ GLuint Constant;
+ GLuint Linear;
+ GLuint Exp;
+ } Atten;
+ } m_spotLightsLocation[MAX_SPOT_LIGHTS];
+};
+
+
+#endif /* LIGHTING_TECHNIQUE_H */
Oops, something went wrong.

0 comments on commit 2411553

Please sign in to comment.