Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


TheOrestes

Member Since 10 Feb 2009
Offline Last Active Jun 23 2015 08:10 AM

Topics I've Started

Lighting Shader Confusion

18 January 2015 - 12:11 PM

Hi,

 

I am encountering an issue where am sure I am the culprit but am not able to quite figure out what is it that I am doing wrong. Here is the vertex shader :

#version 400

layout(location=0) in vec3 in_Position;
layout(location=1) in vec3 in_Normal;
layout(location=2) in vec2 in_Tex;

out vec2 vs_outTex;
out vec3 vs_outNormal;
out vec3 vs_outLightDir;
out vec4 vs_outObjColor;

// uniforms...
uniform mat4 matWorld;
uniform mat4 matView;
uniform mat4 matProj;

vec3 LightDirection = vec3(1,1,1);
vec4 objColor = vec4(1,0,0,1);

void main()
{	
	mat4 WVP = matProj * matView * matWorld;
	gl_Position = WVP * vec4(in_Position, 1.0);

	vs_outNormal = in_Normal;
	vs_outLightDir = normalize(LightDirection);
	vs_outTex = in_Tex;
	vs_outObjColor = objColor;
}

and here is the fragment shader :

#version 400

in vec2 vs_outTex;
in vec3 vs_outNormal;
in vec3 vs_outLightDir;
in vec4 vs_outObjColor;

out vec4 outColor;

uniform sampler2D texture_diffuse1;

void main()
{
	vec4 baseColor = vec4(texture(texture_diffuse1, vs_outTex));
		
	float NdotL = max(dot(vs_outNormal, vs_outLightDir), 0);

	vec4 Emissive = baseColor;
	vec4 Ambient = vec4(0.2, 0.2, 0.2, 1);
	vec4 DiffuseDirect = vec4(NdotL);

	outColor = vs_outObjColor * DiffuseDirect; //Emissive; // * (Ambient + DiffuseDirect);
}

Here is the output produced by the shader during rendering :

 

Model_Space.png

 

Obviously, you guys must have noticed that I have not transformed object normals from object space to world space. Due to this, when the object rotates the lighting rotates with it giving a feeling that light is attached to the object and rotates with it. However, logically what I am trying to do is implement simple directional light shader where direction is suppose to be in world space, so no matter what orientation object is in, lighting direction remains constant.

 

For this to work, I took my object space normals to world space by doing following changes to the code in vertex shader :

vs_outNormal = (matWorld * vec4(in_Normal, 1.0)).xyz;

After doing this, here is the output I get :

 

World_Space.png

 

This time around, deer at the center receives proper lighting & gives impression of light ( which is global directional light ) being static if object rotates etc. However, other deer object's lighting has gone for the toss! Interestingly the closer i get left & right deers to the center deer object, lighting seems to be working fine. Here is the outptut :

 

World_Space_Close.png

 

Can anyone suggest where am I screwing up things? Why lighting seems to be working fine only when Object is at (0,0,0) but the time we move to left or right, lighting seem to be changing? ( Obviously this will happen in case the light source is point light with attenutation, but its directional light for me! )

 

Any thoughts or way forward in debugging will be really helpful.

 

Thanks.


D3DXMatrixLookAtLH vs Custom View Matrix

24 April 2014 - 01:08 AM

Hi,

 

I am quite sure am doing something wrong here but not able to figure it out. Basically,

D3DXMatrixLookAtLH(&m_matView, &m_vecPosition, &m_vecLookAtPoint, &m_WorldUp);

results in : d3dview.png

 

Whereas. custom view matrix calculation :

m_vecLookAtPoint = m_vecLookAtPoint - m_vecPosition;
    D3DXVec3Normalize(&m_vecLookAtPoint, &m_vecLookAtPoint);
    
    D3DXVec3Cross(&m_vecRight, &m_vecLookAtPoint, &m_WorldUp);

    D3DXVec3Normalize(&m_vecRight, &m_vecRight);

    D3DXVec3Cross(&m_WorldUp, &m_vecLookAtPoint, &m_vecRight);
    D3DXVec3Normalize(&m_WorldUp, &m_WorldUp);

    m_matView._11 = m_vecRight.x; m_matView._12 = m_WorldUp.x; m_matView._13 = m_vecLookAtPoint.x; m_matView._14 = 0;
    m_matView._21 = m_vecRight.y; m_matView._22 = m_WorldUp.y; m_matView._23 = m_vecLookAtPoint.y; m_matView._24 = 0;
    m_matView._31 = m_vecRight.z; m_matView._32 = m_WorldUp.z; m_matView._33 = m_vecLookAtPoint.z; m_matView._34 = 0;
    
    m_matView._41 = -D3DXVec3Dot(&m_vecPosition, &m_vecRight);
    m_matView._42 = -D3DXVec3Dot(&m_vecPosition, &m_WorldUp);
    m_matView._43 = -D3DXVec3Dot(&m_vecPosition, &m_vecLookAtPoint);
    m_matView._44 = 1.0f;

results in :

 

customview.png

 

If i manually change the WorldUp vector to (0,-1,0) instead of (0,1,0) i get the default output as in first image.

 

These are my vectors used for the matrix calculations :

    m_vecPosition    = D3DXVECTOR3(3.0f, 3.0f, -10.0f);
    m_WorldUp        = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    m_vecRight        = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_vecLookAtPoint= D3DXVECTOR3(0.0f, 0.0f, 0.0f);  

The question is, why I have to invert my World up vector during custom view matrix calculation? what is it that am doing wrong?

 

Cheers.

 

 


PC to browser

08 November 2013 - 07:57 PM

Hi Guys,

 

Not sure if this is the right place to post this topic, please let me know if otherwise, i tried putting this in Game Programming section, but no reply, so thought like posting it here.

 

I need some advice on porting front. We have pipeline set in such a way that content is being done on 3d studio max with DirectX 9 shaders. The file format we are working on is VRML format which supports direct DirectX shaders inside the mesh definition. Everything is smooth and fine till this point. However, we are trying to figure out cross platform strategy, where the 3d content will be rendered on the browser as well. Obviously, had it been started with Unity Engine, there was little to worry about ( correct me here ) Or if entire codebase was done keeping in mind this cross platform work in future, we would have faced less issues.

 

obviously, directx shaders won't run on browsers. I came across http://threejs.org/ & http://www.web3d.org/x3d/   as the closest thing which might do the trick for me. VRML mesh format is absolute requirement & these 2 things seems like supporting it. Also, for porting shaders from HLSL to GLSL, I am thinking of using https://github.com/aras-p/hlsl2glslfork .

 

To be honest, I have never worked on browser based technologies before, but am quite comfortable with C++ & DirectX. Hence, I wanted to ask folks around here if this is the sane approach for porting? Please let me know if there is anything else I should keep in mind. Please also point me to any other tools or codebase which handles rendering of VRML mesh formats on Web.

 

Looking forward to hear from you.

 

Cheers.


PC to Browser

05 November 2013 - 10:27 PM

Hi Guys,

 

Not sure if this is the right place to post this topic, please let me know if otherwise. I need some advice on porting front. We have pipeline set in such a way that content is being done on 3d studio max with DirectX 9 shaders. The file format we are working on is VRML format which supports direct DirectX shaders inside the mesh definition. Everything is smooth and fine till this point. However, we are trying to figure out cross platform strategy, where the 3d content will be rendered on the browser as well. Obviously, had it been started with Unity Engine, there was little to worry about ( correct me here ) Or if entire codebase was done keeping in mind this cross platform work in future, we would have faced less issues.

 

obviously, directx shaders won't run on browsers. I came across http://threejs.org/ & http://www.web3d.org/x3d/   as the closest thing which might do the trick for me. VRML mesh format is absolute requirement & these 2 things seems like supporting it. Also, for porting shaders from HLSL to GLSL, I am thinking of using https://github.com/aras-p/hlsl2glslfork .

 

To be honest, I have never worked on browser based technologies before, but am quite comfortable with C++ & DirectX. Hence, I wanted to ask folks around here if this is the sane approach for porting? Please let me know if there is anything else I should keep in mind. Please also point me to any other tools or codebase which handles rendering of VRML mesh formats on Web.

 

Looking forward to hear from you.

 

Cheers.


DirectX + Obj + Boost::Serialization

06 January 2013 - 05:53 AM

Hi,

 

Recently I wrote a very basic Obj format loader in DIrectX, thanks to this wonderful article by Adel Amro : http://my.opera.com/adelamro/blog/direct3d-obj-viewer-sample

 

Obj file was taking time to load. I decided to give Boost's binary serialization a try just to check if after converting data into custom mesh format in binary if I get any speed benifits or not. Just to keep things very simple to begin with, I simply chose 4 members to be a part of my custom mesh format : vertex buffer, index buffer, attribute buffer & custom material structure just as blog above uses.

 

To start off with, i chose boost::text_archive to get hang of boost, am using Boost for the very first time. I was able to successfully read data from obj into my own data format & later use my data format to render it on the screen. This assured that all the required setup to render the mesh was correct & properly set.

 

Then I switched to boost::binary_archive. I was able to write the data in Binary format, but when I tried loading it, I got error which hangs up at following code inside boost library. Am absolutely beginner in boost & not able to dig down the root cause for the error. The error occurs at following line inside "boost_binary_iprimitive.hpp" file :

 

 

 std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
    std::streamsize scount = m_sb.sgetn(
        static_cast<Elem *>(address),
        s
    );
    if(scount != s)
        boost::serialization::throw_exception(
            archive_exception(archive_exception::input_stream_error)
        );

 

where the value of s is 4 & scount is 2, which throws the exception. Can anyone let me know what am I doing wrong & what causes for this type of error?

 

Thanks,

TheOrestes.


PARTNERS