## Recommended Posts

Hi, i'm working on shadow mapping now using OpenGL and CG and I hava a little problem, i guess it's the so called "negative projection". The thing is i don't know how to fix that in the fragment shader. There are some methods that suggest cliping everything behind the light but i'm doing only 1 rendering pass. The fragment program:
void main(float4 texProj : TEXCOORD7,
float2 texDiffuse: TEXCOORD0,
uniform sampler2D diffuseMap:TEXUNIT0,
uniform sampler2D projMap:TEXUNIT3,
out float4 color : COLOR
)
{

float4 projcolor = tex2Dproj(projMap, texProj);
float4 diffusecolor = tex2D(diffuseMap,texDiffuse);
color=pow(attcolor,5)*diffusecolor;
float4 col=pow(projcolor,5);

color=col*diffusecolor;
}


There are some screenshots here: shadow Anyone has any suggestions? Thank you.

##### Share on other sites
Hi,

I don't know if you've tried this, but user clipping planes shold do the trick (I did this when I was playing with projectors...they are dead easy to setup). Only problem is that it's a fairly recent feature and not all video cards support this.

Oh yeah, I seem to remember some trick that utilizes the near/far clipping planes to work as an arbitrary clipping plane. I've never tried this, and I don't have a link handy but it might be worth looking into.

##### Share on other sites
thanks, clipping planes might work if i'd render in 2 passes, one for the geometry that receives shadows and one for the others. But how can i achieve this in only 1 pass?

##### Share on other sites
You just need to test in the shader if the point you are shadowing is behind the light or not. One way is to kill all those points ( with a clip plane ).

Another way is a 2x1 texture with a white and a black texel, mapped to the light's Z axis. You can use this to combine with your shadowing or lighting term to make or or the other cancel out.

##### Share on other sites
Quote:
 Another way is a 2x1 texture with a white and a black texel, mapped to the light's Z axis.

Can you explain please how to map it to the lights Z axis?Thanks

##### Share on other sites
Well, once you put the vertex through the lightviewprojection matrix, the w component of the vertex will contain the view space Z value from the light's point of view.

In opengl, that's along the -Z axis, so you want to use this to create your texture coordinate.

Now, since the left texel has black, and right one has white, we'll need to reverse the coordiante, or reverse the texture, and we want to light to map to the middle of the 2x1 texture
shadow_mask = tex2d( -vertex.w + 0.5f, 0.0f )

Then it returns white if it's in front of the light, or black if behind.

Or, you could do this with code, like
if ( -vertex.w > 0.0f ){  // may cast or receive a shadow}else{   // can't cast or receive a shadow}

##### Share on other sites
thank you for the reply, i tried both methods but still doesn't work, i am wondering is the projection matrix constructed correctly? It does project the shadow as it should but i also get a black region beside the reverse projection.
Here is the code for matrix generation

	CMatrix4 projMatrix;	projMatrix.LoadIdentity();	projMatrix.Scale(CVector3(0.5f*float(800.0/1024.0), 0.5f*float(600.0/1024.0), 1.0f));	projMatrix.Translate(CVector3(1.0f,1.0f,0.0f));	projMatrix.postMultiply(CMatrix4(lightProjectionMatrix));	projMatrix.postMultiply(CMatrix4(lightViewMatrix));	projMatrix.ExportMatrix(mt);

void main(float4 texProj : TEXCOORD7,		  float4 vertex : TEXCOORD5,		  float2 texDiffuse: TEXCOORD0,		  uniform sampler2D diffuseMap:TEXUNIT0,		  uniform sampler2D attenuationMap:TEXUNIT2,		  uniform sampler2D projMap:TEXUNIT3,		  out float4 color : COLOR		  ) {	float4 projcolor = tex2Dproj(projMap, texProj);	float4 shadow_mask= tex2D(attenuationMap, float2(-vertex.w+0.5f,0.0f));	float4 diffusecolor = tex2D(diffuseMap,texDiffuse);	float4 col=pow(projcolor,5);		color=shadow_mask*col*diffusecolor;//shadow_mask*diffusecolor;}

void main(	float4 inPos : POSITION,		  float2 inTex0 : TEXCOORD0,		  uniform float4x4 Mvp: state.matrix.mvp,		  uniform float4x4 ShadowTexMat,		  uniform float4x4 lightViewProj,		  out float4 outPos : POSITION,		  out float2 outTex : TEXCOORD0,		  out float4 outTexProj : TEXCOORD7,		  out float4 oVertex: TEXCOORD5		  ){	// compute vertex position	outPos=mul(Mvp,inPos);	float4 vertex=mul(lightViewProj,inPos);	// compute projected texture coordinates	outTexProj=mul(ShadowTexMat,inPos);	outTex=inTex0;	oVertex=vertex;}

The difference between ShadowTexMat and lightViewProj is that in the later i don't scale and translate...but i tried making them the same and still don't get correct results.
I also tried the other method you suggested but makes no difference. The shadow_mask value is about 0.5 when mapping with vertex.w or i get some stripes if mapped with x,y or z like this.
I can't figure what's wrong :(...thanks

##### Share on other sites
sorry to bump this again but after multiple attempts i haven't manage to make it work correctly. I tried the 2 methods presented here, also tried using an attenuation map that i project...but the projection suffers the same problem.

I apologize again but anyone has any ideea? I'm trying to accomplish this in only 1 pass. Thank you

##### Share on other sites
Hi..

I have GLSL code and a VERY short tutorial on my site.. hopefully it might help.
Ask if you wonder anything (and I will dust of that code once more ;])

Good luck!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627639
• Total Posts
2978344
• ### Similar Content

• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• Hello all,

On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
#include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }
The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
What is the problem and how to solve it please?

• 10
• 12
• 22
• 13
• 33