Jump to content
  • Advertisement
Sign in to follow this  
ReDevilGames

OpenGL - Can't get FBO to work

This topic is 802 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

Hi! I'm following OpenGL tutorials at www.opengl-tutorial.org

I'm stuck at FBO section. I have set up a FBO class, which I have attached to this message.

 

This is what I have understood:

Every FBO has a color buffer and a depth buffer, which all goes into a texture.

So basically we have to generate these two buffers and an empty texture, passing nullptr to the glTexImage2D function.

Then we have to attach that texture to the entire framebuffer.

So we write colors into GL_COLOR_ATTACHMENT0, which corresponds to the layout 0 in the shader.

Finally, we have to bind the framebuffer, so we can draw everything we want on it.

Once done, switch back to the framebuffer 0, which is the screen buffer.

 

This is what I'm trying to do: draw the entire scene, by calling only the Draw method of all the active instances, just to see if the FBO class work.

BUT NOTHING!!

 

This is the code which is supposed to draw a cube on the FBO:

// Don't judge this code: it's only to see if FBOs actually work XD.
fbo->Bind(); // Render to FBO texture

Model * model2 = ResourceManager::RequestModel("cube");

model2->textureID = ResourceManager::RequestTexture("texture"); // GLuint to the texture ID
model2->BindBuffers(); // Pass data to OpenGL
model2->ActiveTexture(ResourceManager::RequestShader("shader")->uniforms["texSampler"]); // Bind the texture unit 0 and pass it to the shader
model2->Draw(); // Draw procedure for 3D models, including vertices and UV mapping

FBO::BindDefault(); // Switch back to the screen

The above code works normally, but not if I call the Bind method of the fbo object before actually draw anything.

So, if I you had took a look at my FBO class, I should have that cube drawn on the "renderedTexture" variable, shouldn't I?

 

But it doesn't. What am I doing wrong?

Thanks in advance!

 

Excuse me for my bad english xD

Share this post


Link to post
Share on other sites
Advertisement
First, I'm not sure I understand. You are explicitly drawing to an off-screen buffer but are magically expecting it show up on your screen?

Second, add all the requisite error-handling. There are a billion and one GL errors and using the return codes or ARB_debug will help you find them.

Share this post


Link to post
Share on other sites

..Along with what was mentioned above( and I wish I could upvote more than once..because those comments echo my sentiment to the 'T' ), I would suggest refraining from blindly following 'tutorials' without a little background into the topic itself. If not, all one is doing is just copying and pasting code without understanding how it works. Have you read the frame buffer object specification ? Some of the specification documents are a little hard to swallow, but at least they do give an idea as to what needs to be done to get it work and what to expect when it does NOT work.

Share this post


Link to post
Share on other sites

"Every FBO has a color buffer and a depth buffer, which all goes into a texture." - wrong, FBO has access to attachments defined by you. It may be FBO rendering only to the depth if you wish, it may be FBO rendering to more than one color attachment if you wish to have that one going.

 

"I should have that cube drawn on the "renderedTexture" variable, shouldn't I" - it is quite possible that you have it, but how do you know that without drawing any primitive using that texture to the main framebuffer? Well, you may use tools like CodeXL to see content of GL resources ( buffers, textures, etc. ). But it will not give you much as long as you don't understand what FBO is. The texture you render into will not draw itself to the main framebuffer. You need to use this texture rendering another primitive.

 

Maybe going into spec would be an overkill for beginner ( specs are rather written for implementation developers ), but this should help you:

 

https://www.opengl.org/wiki/Framebuffer_Object

Share this post


Link to post
Share on other sites

SOLVED!

Yes, I know that there are some misunderstanding that I committed, such as attachments. Now I have re-read the documentation 'till to FBOs. Then when I was reading about textures:

 

The texture won't work because it is incomplete. The default GL_TEXTURE_MIN_FILTER state is GL_NEAREST_MIPMAP_LINEAR. And because OpenGL defines the default GL_TEXTURE_MAX_LEVEL to be 1000, OpenGL will expect there to be mipmap levels defined. Since you have only defined a single mipmap level, OpenGL will consider the texture incomplete until the GL_TEXTURE_MAX_LEVEL is properly set, or the GL_TEXTURE_MIN_FILTER parameter is set to not use mipmaps.

 

If you looked at my FBO class, the implementation of "CreateEmptyTexture" was missing this part XD.

This was driving me crazy, because framebuffer was clearly bound, but glClearColor didn't even work!

Now it works XD

Edited by FonzTech

Share this post


Link to post
Share on other sites
If you looked at my FBO class, the implementation of "CreateEmptyTexture"

 

 

Well, I don't want to be nit-picking here, but we don't see that, even if we look at it under microscope  :) You didn't give us implementation of CreateEmptyTexture. Attached file contains only a single call of GenerateEmptyTexture() and doesn't contain its implementation.

Edited by j_uk_dev

Share this post


Link to post
Share on other sites

[spoiler]

unsigned int SHADOW_FBO1;
unsigned int SHADOW_RBO1;
unsigned int SHADOW_DEPTH_TEX;


unsigned int SHADOW_FBO2;
unsigned int SHADOW_RBO2;
unsigned int SHADOW_FINAL_TEX;

void Reinitialize(int w, int h)
{
	RENDER_BUFF_W = w;
	RENDER_BUFF_H = h;


glGenTextures(1, &SHADOW_DEPTH_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO1);
glGenRenderbuffers(1, &SHADOW_RBO1);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO1);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO1);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_DEPTH_TEX, 0);
//SHADOW_FBO1 = build2DTexFramebuffer(SHADOW_DEPTH_TEX);




glGenTextures(1, &SHADOW_FINAL_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_FINAL_TEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO2);
glGenRenderbuffers(1, &SHADOW_RBO2);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO2);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO2);
glBindTexture(GL_TEXTURE_2D, SHADOW_FINAL_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO2);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_FINAL_TEX, 0);


LoadShaders();


	full_scr_arr[0].v = t3dpoint<float>(-1.0,-1.0,0.0);
	full_scr_arr[1].v = t3dpoint<float>(1.0,-1.0,0.0);
	full_scr_arr[2].v = t3dpoint<float>(1.0,1.0,0.0);
	full_scr_arr[3].v = t3dpoint<float>(-1.0,1.0,0.0);

	glGenBuffers(1, &FULLSCREEEN_VERTEX_BUFFER);
	glBindBuffer(GL_ARRAY_BUFFER, FULLSCREEEN_VERTEX_BUFFER);
	glBufferData(GL_ARRAY_BUFFER, sizeof(TLightVertex) * 4, full_scr_arr, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
initialized = true;
}
 

[spoiler]

#ifndef lightsH
#define lightsH

#include "glwrapper.h"
#include "DxcMath.h"
#include "shaderclass.h"
#include "modelloading/TachoGLModelSRC.h"
#include "shadermodel_struct.h"
#include "globalvars.h"




struct TGLLight
{
	vec3 pos;
	float radius;
	vec3 color;


	Matrix44<float> CAM_MVP;
/*
 * 	Used for shadowmap calculation (directional light)
 */
	Matrix44<float> SHADOWMAP_MVP;
	Matrix44<float> light_model;
	Matrix44<float> light_proj;
	Matrix44<float> light_view;
int RENDER_BUFF_W;
int RENDER_BUFF_H;


unsigned int SHADOW_FBO1;
unsigned int SHADOW_RBO1;
unsigned int SHADOW_DEPTH_TEX;


unsigned int SHADOW_FBO2;
unsigned int SHADOW_RBO2;
unsigned int SHADOW_FINAL_TEX;


//unsigned int SHADOWMAP_FBO;
//unsigned int SHADOWMAP;

//int DOWNSAMPLE_W;
//int DOWNSAMPLE_H;

//unsigned int DOWNSAMPLE_FBO;
//unsigned int DOWNSAMPLE_TEX;

//unsigned int UPSAMPLE_FBO;
//unsigned int UPSAMPLE_TEX;

TShaderObject * CLEAR_DEPTH_BUFFER_SHADER;
TShaderObject * WRITE_TO_DEPTH_BUFFER_SHADER;
//TShaderObject * DOWNSAMPLE_SHADER;
//TShaderObject * UPSAMPLE_SHADER;
TShaderObject * SHADOWMAP_SHADER;
TShaderObject * DISPLAY_SHADER;
TShaderObject * DEBUG_SHADER;
TShaderObject * POINT_PERPIXEL_SHADER;

bool initialized;
TGLLight()
{
	RENDER_BUFF_W = 512;
	RENDER_BUFF_H = 512;
//	DOWNSAMPLE_W  = 64;
//	DOWNSAMPLE_H  = 64;
	initialized = false;
}

~TGLLight()
{
	if (!initialized) return;
//	delete UPSAMPLE_SHADER;
//	delete DOWNSAMPLE_SHADER;
	delete CLEAR_DEPTH_BUFFER_SHADER;
	delete WRITE_TO_DEPTH_BUFFER_SHADER;
	delete SHADOWMAP_SHADER;
	delete DISPLAY_SHADER;
	delete DEBUG_SHADER;
	delete POINT_PERPIXEL_SHADER;


	glDeleteFramebuffers(1, &SHADOW_FBO1);
	glDeleteRenderbuffers(1, &SHADOW_RBO1);
	glDeleteTextures(1, &SHADOW_DEPTH_TEX);


	glDeleteFramebuffers(1, &SHADOW_FBO2);
	glDeleteRenderbuffers(1, &SHADOW_RBO2);
	glDeleteTextures(1, &SHADOW_FINAL_TEX);
	glDeleteBuffers(1, &FULLSCREEEN_VERTEX_BUFFER);

}

TLightVertex full_scr_arr[4];
unsigned int FULLSCREEEN_VERTEX_BUFFER;

void Reinitialize(int w, int h)
{
	RENDER_BUFF_W = w;
	RENDER_BUFF_H = h;
/*
 * 	make downsample and upsample textures
 */
/*	glGenTextures(1, &DOWNSAMPLE_TEX);
	glBindTexture(GL_TEXTURE_2D, DOWNSAMPLE_TEX);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, DOWNSAMPLE_W, DOWNSAMPLE_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

DOWNSAMPLE_FBO = build2DTexFramebuffer(DOWNSAMPLE_TEX);


	glGenTextures(1, &UPSAMPLE_TEX);
	glBindTexture(GL_TEXTURE_2D, UPSAMPLE_TEX);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

	UPSAMPLE_FBO = build2DTexFramebuffer(UPSAMPLE_TEX);
*/
/*
 * 	OK
 */


	/*
	 * Now heres a tricky part we need to size a texture that matches aspect ratio of SCREEN_WIDTH / SCREEN_HEIGHT
	 * 854x480 this is my scr aspect ratio
	 */

glGenTextures(1, &SHADOW_DEPTH_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO1);
glGenRenderbuffers(1, &SHADOW_RBO1);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO1);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO1);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_DEPTH_TEX, 0);
//SHADOW_FBO1 = build2DTexFramebuffer(SHADOW_DEPTH_TEX);




glGenTextures(1, &SHADOW_FINAL_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_FINAL_TEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO2);
glGenRenderbuffers(1, &SHADOW_RBO2);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO2);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO2);
glBindTexture(GL_TEXTURE_2D, SHADOW_FINAL_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO2);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_FINAL_TEX, 0);


LoadShaders();


	full_scr_arr[0].v = t3dpoint<float>(-1.0,-1.0,0.0);
	full_scr_arr[1].v = t3dpoint<float>(1.0,-1.0,0.0);
	full_scr_arr[2].v = t3dpoint<float>(1.0,1.0,0.0);
	full_scr_arr[3].v = t3dpoint<float>(-1.0,1.0,0.0);

	glGenBuffers(1, &FULLSCREEEN_VERTEX_BUFFER);
	glBindBuffer(GL_ARRAY_BUFFER, FULLSCREEEN_VERTEX_BUFFER);
	glBufferData(GL_ARRAY_BUFFER, sizeof(TLightVertex) * 4, full_scr_arr, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
initialized = true;
}


void DrawFullScreenQuad(TShaderObject * shader)
{

		glBindBuffer(GL_ARRAY_BUFFER, FULLSCREEEN_VERTEX_BUFFER);
		glVertexAttribPointer(shader->vertex_pos, 3, GL_FLOAT, GL_FALSE, sizeof(TLightVertex), (void*)(offsetof(struct TLightVertex, v)));
		glEnableVertexAttribArray(shader->vertex_pos);


	    glDrawArrays(GL_TRIANGLE_FAN, 0 , 4);

	glDisableVertexAttribArray(shader->vertex_pos);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

}

void LoadShaders()
{
	CLEAR_DEPTH_BUFFER_SHADER = new TShaderObject();
	CLEAR_DEPTH_BUFFER_SHADER->LoadShaderProgram(appdir+"shaders/compute/CLEAR_DEPTH_BUFFER_VP.shader", appdir+"shaders/compute/CLEAR_DEPTH_BUFFER_FP.shader");


	WRITE_TO_DEPTH_BUFFER_SHADER = new TShaderObject();
	WRITE_TO_DEPTH_BUFFER_SHADER->LoadShaderProgram(appdir+"shaders/compute/GLSL_SOFTWARE_DEPTHBUFFER_VP.shader", appdir+"shaders/compute/GLSL_SOFTWARE_DEPTHBUFFER_FP.shader");


	SHADOWMAP_SHADER = new TShaderObject();
	SHADOWMAP_SHADER->LoadShaderProgram(appdir+"shaders/compute/shadow/shadowmap_vp.shader", appdir+"shaders/compute/shadow/shadowmap_fp.shader");


	DISPLAY_SHADER = new TShaderObject();
	DISPLAY_SHADER->LoadShaderProgram(appdir+"shaders/compute/shadow/display_shadowmap_vp.shader", appdir+"shaders/compute/shadow/display_shadowmap_fp.shader");


	DEBUG_SHADER = new TShaderObject();
	DEBUG_SHADER->LoadShaderProgram(appdir+"shaders/compute/shadow/debug_depthmap_vp.shader", appdir+"shaders/compute/shadow/debug_depthmap_fp.shader");


	POINT_PERPIXEL_SHADER = new TShaderObject();
	POINT_PERPIXEL_SHADER->LoadShaderProgram(appdir+"shaders/lighting/perpixel_vp.shader", appdir+"shaders/lighting/perpixel_fp.shader");

//
//	DOWNSAMPLE_SHADER = new TShaderObject();
//	DOWNSAMPLE_SHADER->LoadShaderProgram(appdir+"shaders/compute/shadow/downsample_vp.shader", appdir+"shaders/compute/shadow/downsample_fp.shader");
//
//	UPSAMPLE_SHADER = new TShaderObject();
//	UPSAMPLE_SHADER->LoadShaderProgram(appdir+"shaders/compute/shadow/upsample_vp.shader", appdir+"shaders/compute/shadow/upsample_fp.shader");

}


void SetUpPerPixelLightShader(Matrix44<float> MVP, Matrix44<float> ModelMat)
{

	glDepthMask(GL_FALSE);
	glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE);
POINT_PERPIXEL_SHADER->Enable();
POINT_PERPIXEL_SHADER->SetLightPos(pos);
POINT_PERPIXEL_SHADER->SetLightDiffuse(vec3(0.0, 1.0, 0.0));
POINT_PERPIXEL_SHADER->Send1F(glGetUniformLocation(POINT_PERPIXEL_SHADER->gProgram, "LRadius"), 100.0);
POINT_PERPIXEL_SHADER->SendMVPtoShader(MVP);
POINT_PERPIXEL_SHADER->SendWorldMatrixtoShader(ModelMat);


}

void UnbindPerPixelLightShader()
{
	POINT_PERPIXEL_SHADER->Disable();
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
	glEnable(GL_DEPTH_TEST);
}


void AquireDepthMap(TachoGLModel<float> * model, vec3 target, int first, vec3 modelpos, mat4 rotation)
{
	glViewport(0,0, RENDER_BUFF_W, RENDER_BUFF_H);



	Matrix44<float> MVP = (ACTUAL_MODEL * ACTUAL_VIEW) * ACTUAL_PROJECTION;


glLookAt(light_view, pos, target, vec3(0.0, 1.0, 0.0));
gluPerspectiveA(light_proj, 90.0f, float(SCREEN_WIDTH) / float(SCREEN_HEIGHT), 2.0f, 100.0f);
//gluOrthoA(light_proj,-256.0f, 256.0f, -256.0f, 256.0f, 20.0f, 200.0f);

light_model.LoadIdentity();
light_model = rotation;
light_model.TranslateP(modelpos);
	SHADOWMAP_MVP = (light_model * light_view ) * light_proj;

// * Clear depth buffer

	glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO2);
	glClearColor(0.0, 0.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glClearDepthf(1.0f);
	glClear(GL_DEPTH_BUFFER_BIT);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);


	glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO1);
	glClearColor(0.0, 0.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glClearDepthf(1.0f);
	glClear(GL_DEPTH_BUFFER_BIT);

	glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
	CLEAR_DEPTH_BUFFER_SHADER->Enable();
	DrawFullScreenQuad(CLEAR_DEPTH_BUFFER_SHADER);
	CLEAR_DEPTH_BUFFER_SHADER->Disable();
glDepthMask(GL_TRUE);
	glEnable(GL_DEPTH_TEST);




	//draw from light view and store in texture

//		 * Draw into the depth buffer from light view

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		WRITE_TO_DEPTH_BUFFER_SHADER->Enable();
		WRITE_TO_DEPTH_BUFFER_SHADER->Send1I(glGetUniformLocation(WRITE_TO_DEPTH_BUFFER_SHADER->gProgram, "firstrun"), first);
		WRITE_TO_DEPTH_BUFFER_SHADER->Send1F(glGetUniformLocation(WRITE_TO_DEPTH_BUFFER_SHADER->gProgram, "z_far"), 100.0);
		WRITE_TO_DEPTH_BUFFER_SHADER->SetLightPos(pos);
		WRITE_TO_DEPTH_BUFFER_SHADER->SendWorldMatrixtoShader(light_model);//actually its model world matrix...

		WRITE_TO_DEPTH_BUFFER_SHADER->SendMVPtoShader(SHADOWMAP_MVP);

		model->DrawSimpleModel(WRITE_TO_DEPTH_BUFFER_SHADER);

		WRITE_TO_DEPTH_BUFFER_SHADER->Disable();



//		 * Draw model from camera view and compare with depth from shadowmap



	//DrawShadedMapModel(model, true, target, modelpos);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);
/*
 * DOWNSAMPLE THE SHADOWMAP TO GET SMOOTHER LOOK
 */

	//this piece of code writes 512x512 texture to 64x64 one
/*
	glViewport(0,0, DOWNSAMPLE_W, DOWNSAMPLE_H);
	glBindFramebuffer(GL_FRAMEBUFFER, DOWNSAMPLE_FBO);

	glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
	CLEAR_DEPTH_BUFFER_SHADER->Enable();
	DrawFullScreenQuad(CLEAR_DEPTH_BUFFER_SHADER);
	CLEAR_DEPTH_BUFFER_SHADER->Disable();

	DOWNSAMPLE_SHADER->Enable();

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
	DOWNSAMPLE_SHADER->Send1I(glGetUniformLocation(DOWNSAMPLE_SHADER->gProgram,"shadow_map"),0);
	DrawFullScreenQuad(DOWNSAMPLE_SHADER);
	DOWNSAMPLE_SHADER->Disable();
	glDepthMask(GL_TRUE);
		glEnable(GL_DEPTH_TEST);*/
	/*
	 * NOW UPSAMPLE THAT
	 */
/*
	UPSAMPLE_SHADER->Enable();
	glViewport(0,0, RENDER_BUFF_W, RENDER_BUFF_H);
	glBindFramebuffer(GL_FRAMEBUFFER, UPSAMPLE_FBO);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, DOWNSAMPLE_TEX);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
	UPSAMPLE_SHADER->Send1I(glGetUniformLocation(UPSAMPLE_SHADER->gProgram,"shadow_map"),0);
	UPSAMPLE_SHADER->Send1I(glGetUniformLocation(UPSAMPLE_SHADER->gProgram,"downsample_map"),1);
	DrawFullScreenQuad(UPSAMPLE_SHADER);
	UPSAMPLE_SHADER->Disable();
*/

	/*
	 *  END NOW COMPARE THE DEPTH TEX ETC
	 */


	glViewport(0,0, RENDER_BUFF_W, RENDER_BUFF_H);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO2);

	SHADOWMAP_SHADER->Enable();
	SHADOWMAP_SHADER->Send1I(glGetUniformLocation(SHADOWMAP_SHADER->gProgram,"shadow_map"),0);
	SHADOWMAP_SHADER->Send1F(glGetUniformLocation(SHADOWMAP_SHADER->gProgram, "z_far"), 100.0);
	SHADOWMAP_SHADER->SetLightPos(pos);
	SHADOWMAP_SHADER->SendWorldMatrixtoShader(light_model);//actually its model world matrix...
//	glClearColor(0.0, 0.0, 0.0, 0.0);
//	glClear(GL_COLOR_BUFFER_BIT);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);//DOWNSAMPLE_TEX);//SHADOW_DEPTH_TEX);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	MVP = (light_model * ACTUAL_VIEW) * ACTUAL_PROJECTION;
	SHADOWMAP_SHADER->SendMVPtoShader(MVP);
	SHADOWMAP_SHADER->SendShadowMVPtoShader(SHADOWMAP_MVP);


	model->DrawSimpleModel(SHADOWMAP_SHADER);

	SHADOWMAP_SHADER->Disable();
	glBindFramebuffer(GL_FRAMEBUFFER, 0);



/*
 * Display result image
 */

	glViewport(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);

	DISPLAY_SHADER->Enable();
	DISPLAY_SHADER->Send1I(glGetUniformLocation(DISPLAY_SHADER->gProgram,"stexture"),0);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, SHADOW_FINAL_TEX);//

glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
DrawFullScreenQuad(DISPLAY_SHADER);

glDisable(GL_BLEND);

DISPLAY_SHADER->Disable();

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glViewport(0,0, 256, 256);

DEBUG_SHADER->Enable();
DEBUG_SHADER->Send1I(glGetUniformLocation(DEBUG_SHADER->gProgram,"texture"),0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);// SHADOW_DEPTH_TEX);//


DrawFullScreenQuad(DEBUG_SHADER);


DEBUG_SHADER->Disable();


glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glViewport(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
}


};

#endif
 

[/spoiler]

[/spoiler]

Edited by WiredCat

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!