I've been working on the shadow system but had no clue even I've digged the internet for the past 3 years. Anyone have it and willing to share? I've give up for it.
Posted on 2012-01-10 08:15:17 by Farabi
Could you be more specific?
There are many ways to render shadows.
Posted on 2012-01-10 14:32:33 by Scali

Could you be more specific?
There are many ways to render shadows.


Well, I cant be explain about what I want more since I had no clue, I tried to making a shadowing system for OpenGL but never make it. Maybe anyone already have it and willing to share it. My ground is not a flat plane but a heightfield ground and I had no Idea how to make it.
Posted on 2012-01-10 19:06:13 by Farabi
I have experience in this area ;) Shadow Mapping is accomplished by rendering the scene into a rendertarget texture or just into the depth buffer, from the point of view of your light source (let's start with one light eh?) Now you have made an Occlusion Map, which is bright where the light hits surfaces, and your shadows are where the light cannot see. Now render the scene from the camera's point of view, and use the occlusion map to weight the brightness at each vertex (or each pixel if you want per pixel lighting).
Posted on 2012-01-11 01:36:20 by Homer
Usually this shadow rendering pass is done at a lower resolution than the main pass, using a very simple shader that does not even do texturing, just black and white shades. But I wouldn't even begin to think about shadow mapping until you've created a lighting system that takes distance into account ie lighting fades as a power of distance.
Posted on 2012-01-11 01:40:03 by Homer
The key is to project your screenspace stuff back toward the light, find the intersection with the map u made earlier, which is the near plane of the light's view frustum when we rendered it. Now you can determine brightness from YOUR point of view.
Most simple way is called depth buffer shadows, its worst quality but teaches the math and the way of thinking to tackle more expensive lighting and shadowing techniques.
Know that the cost of this process scales linearly to the number of lights as a clean multiple - its twice as expensive with two lights, three times as expensive with three, at any given quality. You have to stop thinking about light and shadow as being mutually exclusive, one is a result of the lack of the other.
Posted on 2012-01-11 01:42:39 by Homer
Hi, here is my code for the shadow



fCreateShadowTexture proc w:dword,h:dword
LOCAL textID:dword

invoke Check_Extension,CADD("GL_ARB_shadow")
.if eax==FALSE
invoke MessageBox,0,CADD("Not Support GL_ARB_shadow"),0,0
xor eax,eax
dec eax
.endif

invoke Check_Extension,CADD("GL_ARB_depth_texture")
.if eax==FALSE
invoke MessageBox,0,CADD("Not Support GL_ARB_depth_texture"),0,0
xor eax,eax
dec eax
.endif

invoke glGenTextures,1,addr textID
invoke glBindTexture,GL_TEXTURE_2D,textID
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
invoke glTexParameteri,GL_TEXTURE_2D, 0884Ch, 0884Eh;
invoke glTexParameteri,GL_TEXTURE_2D, 0884Dh, GL_LEQUAL;
invoke glTexParameteri,GL_TEXTURE_2D, 0884Bh, GL_INTENSITY;
invoke glTexImage2D,GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,w,h,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,0




mov eax,textID

ret
fCreateShadowTexture endp


fShadowInit proc uses esi edi lpfCamCrnt:dword,lpfCamLght:dword,nTexID:dword

invoke glMatrixMode,GL_MODELVIEW
invoke glLoadIdentity

invoke glShadeModel,GL_SMOOTH
invoke glClearColor,FP4(0.0),FP4(0.0),FP4(0.0),FP4(0.0)
invoke glColor4f,FP4(1.0),FP4(1.0),FP4(1.0),FP4(1.0)
invoke glHint,GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST

invoke glClearDepth,FP8(1.0)
invoke glDepthFunc,GL_LEQUAL
invoke glEnable,GL_DEPTH_TEST

invoke glEnable,GL_CULL_FACE
invoke glEnable,GL_NORMALIZE


invoke glBindTexture,GL_TEXTURE_2D,nTexID
invoke glColorMaterial,GL_FRONT, GL_AMBIENT_AND_DIFFUSE;
invoke glEnable,GL_COLOR_MATERIAL;
invoke glMaterialfv,GL_FRONT, GL_SPECULAR, addr white;
invoke glMaterialf,GL_FRONT, GL_SHININESS, FP4(16.0);

invoke glPushMatrix
invoke glLoadIdentity

invoke gluPerspective,FP8(45.0),FP8(1.33333333333333),FP8(1.0),FP8(1000000000.0)
invoke glGetFloatv,GL_MODELVIEW_MATRIX,addr cpm
invoke glLoadIdentity
invoke fCameraLook,lpfCamCrnt
invoke glGetFloatv,GL_MODELVIEW_MATRIX,addr cvm


invoke gluPerspective,FP8(45.0),FP8(1.0),FP8(2.0),FP8(8.0)
invoke glGetFloatv,GL_MODELVIEW_MATRIX,addr _lpm
invoke glLoadIdentity
invoke fCameraLook,lpfCamLght
invoke glGetFloatv,GL_MODELVIEW_MATRIX,addr _lvm

invoke glPopMatrix


ret
fShadowInit endp

fShadowBegin proc uses esi edi nTexID:dword
LOCAL ngrnd,nlmp:dword
LOCAL ray:dword
LOCAL fc:fCam
LOCAL mat[16]:dword

invoke glMatrixMode,GL_PROJECTION
invoke glLoadMatrixf,addr _lpm
invoke glMatrixMode,GL_MODELVIEW
invoke glLoadMatrixf,addr _lvm

invoke glViewport,0,0,640,480
invoke glCullFace,GL_FRONT
invoke glShadeModel,GL_FLAT
invoke glColorMask,0,0,0,0
invoke fCameraLook,addr lamp
invoke fSceneDrawAll
invoke glBindTexture,GL_TEXTURE_2D,nTexID
invoke glCopyTexSubImage2D,GL_TEXTURE_2D,0,0,0,0,0,640,480
invoke glCullFace,GL_BACK
invoke glShadeModel,GL_SMOOTH
invoke glColorMask,1,1,1,1

invoke glClear,GL_DEPTH_BUFFER_BIT
invoke glMatrixMode,GL_PROJECTION
invoke glLoadMatrixf,addr cpm
invoke glMatrixMode,GL_MODELVIEW
invoke glLoadMatrixf,addr cvm
invoke glViewport,0,0,640,480

invoke glLightfv,GL_LIGHT1,GL_POSITION,addr lamp
invoke glLightfv,GL_LIGHT1,GL_AMBIENT,addr hwhite
invoke glLightfv,GL_LIGHT1,GL_DIFFUSE,addr hwhite
invoke glLightfv,GL_LIGHT1,GL_SPECULAR,addr black
invoke glEnable,GL_LIGHT1
invoke glEnable,GL_LIGHTING
invoke fCameraLook,addr lamp
invoke fSceneDrawAll


invoke glLightfv,GL_LIGHT1, GL_DIFFUSE, addr white
invoke glLightfv,GL_LIGHT1, GL_SPECULAR, addr white

invoke fMat4fLoadIdentity,addr mat
invoke fFillMatrix4f,addr mat,0,FP4(0.5)
invoke fFillMatrix4f,addr mat,5,FP4(0.5)
invoke fFillMatrix4f,addr mat,10,FP4(0.5)
invoke fFillMatrix4f,addr mat,12,FP4(0.5)
invoke fFillMatrix4f,addr mat,13,FP4(0.5)
invoke fFillMatrix4f,addr mat,14,FP4(0.5)

invoke glPushMatrix
invoke glMatrixMode,GL_MODELVIEW
invoke glLoadMatrixf,addr mat
invoke glMultMatrixf,addr _lpm
invoke glMultMatrixf,addr _lvm
invoke glGetFloatv,GL_MODELVIEW_MATRIX,addr mat
invoke glPopMatrix

; 3Rd Pass
;--------------------------------------------------------------------------------
invoke glTexGeni,GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR;
invoke glTexGenfv,GL_S, GL_EYE_PLANE, addr mat
invoke glEnable,GL_TEXTURE_GEN_S;

invoke glTexGeni,GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR;
invoke glTexGenfv,GL_T, GL_EYE_PLANE, addr mat+4
invoke glEnable,GL_TEXTURE_GEN_T;

invoke glTexGeni,GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR;
invoke glTexGenfv,GL_R, GL_EYE_PLANE, addr mat+8
invoke glEnable,GL_TEXTURE_GEN_R;

invoke glTexGeni,GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR;
invoke glTexGenfv,GL_Q, GL_EYE_PLANE, addr mat+12
invoke glEnable,GL_TEXTURE_GEN_Q;

;;Bind & enable shadow map texture
invoke glBindTexture,GL_TEXTURE_2D, nTexID;
invoke glEnable,GL_TEXTURE_2D;

;;Enable shadow comparison
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE;

;;Shadow comparison should be true ,ie not in shadow, if r<=texture
invoke glTexParameteri,GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL;

;;Shadow comparison should generate an INTENSITY result
invoke glTexParameteri,GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY;

;;Set alpha test to discard false comparisons
invoke glAlphaFunc,GL_GEQUAL, 0.99;
invoke glEnable,GL_ALPHA_TEST;

invoke glDisable,GL_CULL_FACE

invoke fCameraLook,addr lamp
invoke fSceneDrawAll

;;Disable textures and texgen
invoke glDisable,GL_TEXTURE_2D;

invoke glDisable,GL_TEXTURE_GEN_S;
invoke glDisable,GL_TEXTURE_GEN_T;
invoke glDisable,GL_TEXTURE_GEN_R;
invoke glDisable,GL_TEXTURE_GEN_Q;

;;Restore other states
invoke glDisable,GL_LIGHTING;
invoke glDisable,GL_ALPHA_TEST;



ret
fShadowBegin endp



I never get it to work, can anyone tell me where is my mistake? Thanks.  :dazzled:
Posted on 2012-01-14 17:48:11 by Farabi
All this is based on fixed function pipeline, which is Dead in modern opengl contexts, perhaps that is all thats wrong, from gl3+ glBegin and glEnd do not exist, neither does anything inside them basically. Try a 2.0 context and if that fails i will review your sourcecode again. Also, if you are using a modern context, dont use ARB extensions when they already got promoted to core. Use the core api with the same name, less the _ARB extension.
Posted on 2012-01-16 01:35:20 by Homer
More so, moving to your own shaders gives you total control of every pixel, and you would be a fool to live in the past when there is so much possible by using a modern context! OK so we have fallbacks, but we suffer when we use them.
Posted on 2012-01-16 01:37:04 by Homer
Its very good that you attempt to write your first depth buffer shadows though, I commend you for the effort to try. But where are your triangles? I have a hint to help debug shaders, when you are not sure, pass a fixed color to the pixel (fragment) and see if that shows up, and if not, look closely at your vertex normals!
Posted on 2012-01-16 01:40:44 by Homer
one or two more hints - go get ur hands on GeDebugger, its shown me errors in my code i didnt know about.
Other hint is to add the following line after every single opengl call:
assert (!glGetError());

In other words, call glGetError and if its not zero, complain loudly!
Posted on 2012-01-16 01:44:53 by Homer
Hi HOmer,
Thanks for your nice answers. Unfortunately I used SiS Mirage3 card whic is only support OpenGL 1.5. So I cant do the shaders, only a few slow functions.
Posted on 2012-01-21 05:17:58 by Farabi
Its still able to do in older gl contexts, but we have to be stuck with the old fixed functions pipeline, and i think for 1.5, we cannot render to a texture, so we are very limited in what we can achieve... we can do per vertex shadows at least. Some of my friends are stuck in the old gl stuff, i think you should get away from it to newer stuff as soon as you can. Program for hardware of the day, and optimize the crap out of your slow code, and you can hold your head up.
Posted on 2012-02-02 05:10:50 by Homer

Its still able to do in older gl contexts, but we have to be stuck with the old fixed functions pipeline, and i think for 1.5, we cannot render to a texture, so we are very limited in what we can achieve... we can do per vertex shadows at least. Some of my friends are stuck in the old gl stuff, i think you should get away from it to newer stuff as soon as you can. Program for hardware of the day, and optimize the crap out of your slow code, and you can hold your head up.


1.5 is able to render to a texture using the PBuffer. The demo worked, but when I tried it it did not worked except I copy it from the backbuffer.
Posted on 2012-02-05 01:53:21 by Farabi
One of the things that really pisses me off about opengl tutorials on the web is that they rarely state exactly which version they are targetting, and don't bother showing you the startup and context creations stuff.
We must blame the authors and demand better or we will have a huge searchable database of no value to anyone.
If you post examples, make sure you quote exactly what target, and better yet, show the initializing! Opengl is very version sensitive, but the apis will pretend to do something rather than break your app, i would rather the app didnt build so i can see exactly where and why! Khronos! Listen!
Posted on 2012-02-08 23:23:59 by Homer
Yea, OpenGL was always a mess, and it's only gotten worse when they started deprecating things in newer versions.
Now you HAVE to use a newer context to access some functionality, which means some legacy code no longer works.
OpenGL is broken in many ways, when it comes to that.
I wrote a blog on that a while ago, addressing many of the differences between Direct3D and OpenGL, and how Khronos could change those: http://scalibq.wordpress.com/2010/05/10/thoughts-of-a-direct3d-developer-after-a-few-months-of-opengl-development/

Some issues have popped up here already. For example: Farabi's SiS videocard only has an OpenGL 1.5 driver. Perhaps the hardware can do more, but without a driver, you're locked out.
For example, my laptop has an Intel X3100 IGP. It is DirectX 10-class. But the OpenGL support is stuck at 2.1. So only basic DX9-level functionality, not SM4.0.
My FreeBSD server is even worse. It has an Intel G31 chip, which is DX9 SM2.0. In theory it should at least support OpenGL 2.0, possibly 2.1. But in practice the driver only supports 1.5, so I cannot use GLSL at all. The only shaders it supports are the old ARB programs in assembly.
With Direct3D, you'd always have vertex shaders at least, since they can be processed on CPU. Even DX7-class hardware can run shaders in DX9. But in OpenGL, the entire GLSL extensions have to be in the driver, and vendors only include them when there is hardware support.
Posted on 2012-02-09 04:54:18 by Scali
I never know OpenGL is really messed up. If I knew it, I should tried DirectX instead of OpenGL, OpenGL is lot simpler, and on some demo it is faster. But then, I saw a DirectX demo that even faster than OpenGL. Should we switch to OpenGL? DX COM style is really a mess for me, where I need to obtain the pointer to each functions. Im just hoping that DX parameter still use push for each parameters.
Posted on 2012-02-15 22:50:28 by Farabi

One of the things that really pisses me off about opengl tutorials on the web is that they rarely state exactly which version they are targetting, and don't bother showing you the startup and context creations stuff.
We must blame the authors and demand better or we will have a huge searchable database of no value to anyone.
If you post examples, make sure you quote exactly what target, and better yet, show the initializing! Opengl is very version sensitive, but the apis will pretend to do something rather than break your app, i would rather the app didnt build so i can see exactly where and why! Khronos! Listen!



Never mind it Mr Leith. I've given up creating shadow on OpenGL, it is too slow. I wonder why texturing on OpenGL is 100 times slower than GDI. I think allocating and sending texture to GPU shouldnt be that slow, GDI BitBlt is far faster than OpengGL.
Posted on 2012-02-15 23:05:21 by Farabi

I never know OpenGL is really messed up. If I knew it, I should tried DirectX instead of OpenGL, OpenGL is lot simpler, and on some demo it is faster. But then, I saw a DirectX demo that even faster than OpenGL. Should we switch to OpenGL? DX COM style is really a mess for me, where I need to obtain the pointer to each functions. Im just hoping that DX parameter still use push for each parameters.


OpenGL is far more complex on the driver side. Which is what you are experiencing right now: only nVidia has a decent OpenGL implementation. AMD is passable in recent years... Any other brand? Forget it.
Direct3D has much better support.
Also, OpenGL being faster is a myth. As long as the software is written properly, the speed is dictated by the CPU and GPU, not by the API used.
Not all software is written properly though, giving extra overhead to either API, depending upon bias.
The problem with OpenGL however is that you can't write it properly for all implementations. You don't always know what features are hardware-accelerated, and what features are emulated in software.
With D3D it's simple: there is only one feature that can be processed in software: T&L/vertex shaders. But it is easy to check the D3D caps to see if the driver supports the features in hardware or not.
Everything else simply does not work at all, if there is no hardware support.

This probably explains why your OpenGL texturing performs worse than GDI.

As for COM in asm? I wouldn't recommend it. Last time I looked, there aren't even any include files for DX10 or DX11.
It's not worth trying to make these yourself. After all, there is nothing to gain by calling API functions from assembly. You might as well do it in C++. You can still use inline assembly, or link to external assembly functions for performance-critical parts.
Posted on 2012-02-16 06:23:59 by Scali
It's certainly possible to create asm headers from the dx10/11 ones, but there is to this day, no tool that will do that job all by itself. You will need to do it by hand.
There is a tool (h2inca) which gets you some of the way there.
Good luck!
Posted on 2012-02-19 06:58:52 by Homer