Jump to content
  • Advertisement
Sign in to follow this  
Ey-Lord

OpenGL ShadowMapping : Shaders

This topic is 3667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone. I'm trying *again* to code some ShadowMapping and i'm stuck near the end. I've understood the process but i may be missing something :/ here is my shadow mapping init()
void ShadowMappingManager::Init()
{
	m_WindowWidth = Application::Instance().m_Settings.m_ScreenWidth ;
	m_WindowHeight = Application::Instance().m_Settings.m_ScreenHeight ;
 
	//Check for necessary extensions
	if ( !glewIsSupported("GL_ARB_depth_texture"))
		std::cout << "ARB texture *not* supported" << std::endl ;
 
	if ( !glewIsSupported("GL_ARB_shadow"))
		std::cout << "Shadow *not* supported" << std::endl ;
 
	if ( !glewIsSupported("GL_EXT_framebuffer_object"))
		std::cout << "FBO *not* supported" << std::endl ;
 
	// Creation  of a depth-texture
	glActiveTexture(GL_TEXTURE1);
	glEnable(GL_TEXTURE_2D);
	//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	//glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
	//glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);
 
	glGenTextures(1, &m_ShadowMapTexture);
	glBindTexture(GL_TEXTURE_2D, m_ShadowMapTexture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_ShadowMapSize, m_ShadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
 
	// Creation of a fbo to get the depth
	glGenFramebuffersEXT(1, &m_ShadowMapFBO);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_ShadowMapFBO);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_ShadowMapTexture, 0);
	// No color buffer to draw to or read from
	glDrawBuffer(GL_NONE);
	glReadBuffer(GL_NONE);
 
	// Check framebuffer completeness at the end of initialization.
	CHECK_FBO_STATUS() ;
 
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
	glActiveTexture(GL_TEXTURE0);
 
	GLfloat white[]= { 1.0f, 1.0f, 1.0f, 1.0f };
	Vector3D<float> LightPos = Vector3D<float>(1.0f, 1.0f,1.0f);
	float LP[3];
	LP[0] = LightPos.x ;
	LP[1] = LightPos.y ;
	LP[2] = LightPos.z ;
	glLightfv(GL_LIGHT0, GL_POSITION, LP);
	glLightfv(GL_LIGHT0, GL_SPECULAR,white);
	glLightfv(GL_LIGHT0, GL_AMBIENT,white);
	glLightfv(GL_LIGHT0, GL_DIFFUSE,white); 
	
	// Load the shader
	ShaderManager::Instance().LoadShaders("ShadowMapping","sm.vert","sm.frag");
	Loc_Dif = ShaderManager::Instance().AddUniformVariable("ShadowMapping","diffuse");
	Loc_Shad = ShaderManager::Instance().AddUniformVariable("ShadowMapping","shadow0");
	ShaderManager::Instance().SetUniformVariableValue1i("ShadowMapping",Loc_Dif,0);
	ShaderManager::Instance().SetUniformVariableValue1i("ShadowMapping",Loc_Shad,1);
}
Here is all my code in one function (for a test purpose) to render with shadowmapping
void ShadowMappingManager::All()
{
	// 1 - Set up opengl to render only a depth-map
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_ShadowMapFBO);
	glClear(GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluPerspective(45.0f, 1.0f, 1.0f, 10.0f);
	glMatrixMode(GL_MODELVIEW);
	gluLookAt(m_LightPos.x, m_LightPos.y,m_LightPos.z,   0.0, 0.0, 0.0,         0.0, 1.0, 0.0 );
	glGetFloatv(GL_MODELVIEW_MATRIX, Light_M);
	glGetFloatv(GL_PROJECTION_MATRIX, Light_P);
	glPushAttrib(GL_VIEWPORT_BIT);
	glViewport(0,0,m_ShadowMapSize,m_ShadowMapSize);
	glEnable(GL_POLYGON_OFFSET_FILL);
	Draw();
	glDisable(GL_POLYGON_OFFSET_FILL);
	glPopAttrib();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	// 2 - Apply the depth map
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	gluPerspective(45.0,(float) m_WindowWidth/m_WindowHeight,0.1,100.);
	glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	Application::Instance().m_RenderTask.Cam.Focalize();
	glGetFloatv(GL_MODELVIEW_MATRIX, Camera_M);

	glMatrixMode(GL_TEXTURE);
	glActiveTexture(GL_TEXTURE1);
	glLoadIdentity();
	float M[16] = 
	{
		0.5f, 0.0f, 0.0f, 0.0f,
		0.0f, 0.5f, 0.0f, 0.0f,
		0.0f, 0.0f, 0.5f, 0.0f,
		0.5f, 0.5f, 0.5f, 1.0f,
	};
	glLoadMatrixf(M);
	glMultMatrixf(Light_P);
	glMultMatrixf(Light_M);

	// Will use inv matrix in vertex shader ?
	//glActiveTexture(GL_TEXTURE0);
	//glLoadMatrixf(Camera_M);

	glActiveTexture(GL_TEXTURE1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, m_ShadowMapTexture);
	ShaderManager::Instance().UseShader("ShadowMapping");
	Draw();
	ShaderManager::Instance().UseShader(0);
	glBindTexture(GL_TEXTURE_2D, 0);
	glDisable(GL_TEXTURE_2D);
}
(it's included in a small standard render loop) And finally a small vertex prog and fragment just to test if it works :
varying vec4 projCoord;

void main(void)
{
	projCoord = gl_TextureMatrix[1] * gl_ModelViewMatrix * gl_Vertex;

	gl_Position = ftransform();
}
uniform sampler2DShadow shadow0;
varying vec4 projCoord;

void main(void)
{
	vec4 s = shadow2DProj(shadow0, projCoord);

	gl_FragColor = vec4(1.0,0.0,0.0,1.0)*s;
}
My whole scene is red, i would expect shadowy zone to be non-red or am i missing somehting there?

Share this post


Link to post
Share on other sites
Advertisement
I cant see anywhere in the code posted where you use glUniform1i( location, texture unit id) to specify that uniform sampler2DShadow shadow0 is texture unit 1.

although this could be hidden away in ShaderManager?

other than that general process seems fine at a quick glance.

Share this post


Link to post
Share on other sites
At the end of my init i got :


ShaderManager::Instance().LoadShaders("ShadowMapping","sm.vert","sm.frag");
Loc_Dif = ShaderManager::Instance().AddUniformVariable("ShadowMapping","diffuse");
Loc_Shad = ShaderManager::Instance().AddUniformVariable("ShadowMapping","shadow0");
ShaderManager::Instance().SetUniformVariableValue1i("ShadowMapping",Loc_Dif,0);
ShaderManager::Instance().SetUniformVariableValue1i("ShadowMapping",Loc_Shad,1);


The "diiffuse" var isnt used anymore but shadow0 is 1 ( GL_TEXTURE1).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!