Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 10 Feb 2012
Offline Last Active Mar 17 2013 01:48 PM

Topics I've Started

Problem passing matrices to shader

27 February 2012 - 01:03 AM

Hello there. I recently decided to port my engine from D3D10 to D3D11 and I am having serious problems when trying to pass the world, view and projection matrices to the GPU. I had been using the ID3D10Effect until now, which allowed me to easily set variables in the shader. Now I am using constant buffers and I am not able to pass the matrices to the video card. I've read a lot of tutorials but I don't seem to find what's wrong with my code.

This is the function where I set the buffer:

This two functions do the rendering (the last one is the problem):

This is my matrix struct:

And this is my shader:

Here's some PIX info

Thanks in advance.

Is it possible to dynamically allocate memory in HLSL?

19 February 2012 - 12:13 AM

Hello there, I am working on this graphics engine and I am now learning how to implement lights into the engine using HLSL. I've come across several tutorials that teach you how to create directional lights, multiple point lights, etc, everything works fine but, I want to create a shader that is as generic as possible, and I've encountered a problem with that.

Lets say that I have a structure in HLSL that looks like this:

struct PointLight
	 float4 LightColor;
	 float3 LightPosition;

And lets also say that I created a level for my game that uses X amount of point lights.

Is there any way to create an X amount of PointLight instances in HLSL during runtime? Is it possible to do something like: PointLight arrayOfLights = new PointLight[X]; ?

Another question, what is the best way in D3D10 to pass variables from the program to a structure member in a shader?


I want to pass this

D3DXVECTOR3 LPosition(0.0f, 0.0f, 0.0f);

Into this


How should I do this?

Thanks in advance

Local to World space

10 February 2012 - 11:51 AM

Hello, I am new here so I hope I am posting in the correct section.

I will try to explain how the engine I am developing works so it'll be easier for you to see what am I doing wrong.

I have a class named CModel, which stores the local matrix of the model, a vector with the position it was in the 3D design program, the vertex buffer, the index buffer and an instance of the material the object uses. I initialize a different CModel instance for each model in the scene.

I initialize each object and translate them to the position (0, 0, 0) using their local matrix, which will position them where they were in the 3D modeling program.

I then call the shader for each object in the scene (is this correct?) and pass in 3 matrices, the local matrix from the model, the view matrix from the camera and the projection matrix, also from the camera.

Here is the Render function:

bool CGraphics::Render(){unsigned int stride = sizeof(SimpleVertex);unsigned int offset = 0;// Clear the render targetD3Ddeviceptr->ClearRenderTargetView(RenderTargetptr, D3DXCOLOR(0.0f, 0.0f, 0.2f, 0.0f));D3Ddeviceptr->ClearDepthStencilView(DepthStencilViewptr, D3D10_CLEAR_DEPTH, 1.0, 0);// Render the first object of the arrayfor (unsigned int count = 0; count < models.size(); count++){  shader->RenderObject(models[count], camera, stride, offset);  //shader->RenderBitmap(bitmap, camera);}// Swap the buffersswapChainptr->Present(0, 0);return true;}

Here is the RenderObject function:

void CShader::RenderObject(CModel *model, CCamera *camera, unsigned int stride, unsigned int offset){pEffectDevice->IASetVertexBuffers(0, 1, model->ppGetVertexBuffer(), &stride, &offset);pEffectDevice->IASetIndexBuffer(model->pGetIndexBuffer(), DXGI_FORMAT_R32_UINT, 0); [/color][color=#000000]D3D10_TECHNIQUE_DESC technique;[/color][color=#000000]pWorldMatrixVariable->SetMatrix( (float*) &model->worldMatrix);pViewMatrixVariable->SetMatrix( (float*) &camera->viewMatrix );pProjectionMatrixVariable->SetMatrix( (float*) &camera->projectionMatrix );[/color][color=#000000]for (unsigned int t = 0; t < model->GetpMaterial()->pGetTextures()->size(); t++){  pTextureVariable->SetResource( model->GetpMaterial()->pGetTextureByIndex(t) );}[/color][color=#000000]pTechnique->GetDesc(&technique);[/color][color=#000000]for (UINT p = 0; p < technique.Passes; ++p){  pTechnique->GetPassByIndex(p)->Apply(0);  pEffectDevice->DrawIndexed(model->GetNumIndices(), 0, 0);}[/color][color=#000000]return;}

Here is my shader:

Texture2D tex2D;SamplerState linearSampler{Filter   = MIN_MAG_MIP_LINEAR;AddressU = Wrap;AddressV = Wrap;};// --------------------------------------------------------------// GLOBALS// --------------------------------------------------------------matrix World;matrix View;matrix Projection;float4 AmbientColor = float4(0.35f, 0.35f, 0.35f, 1.0f);float4 DiffuseColor = float4(0.0f, 0.0f, 1.0f, 1.0f);float3 LightDirection = float3(0.0f, 0.0f, 1.0f);//--------------------------------------------------------------------------------------struct VS_INPUT{	float4 Pos : POSITION;float2 TexCoord : TEXCOORD;float3 Normal : NORMAL;};struct PS_INPUT{	float4 Pos : SV_POSITION;float2 TexCoord : TEXCOORD0;float3 Normal : NORMAL;float4 Color : COLOR0;};// Lighting function// -----------------------------------------------------float4 CalcPhongLighting( float4 LColor, float3 Normal, float3 LRay, float3 Eye, float3 Phi ){float4 Ia = AmbientColor;float4 Id = saturate( dot( Normal, normalize( LRay ) ) );return Ia * Id;}//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------PS_INPUT VS( VS_INPUT input ){float4 WorldPos;float3 eye = (0.0f, 4.0f, -50.0f);input.Pos.w = 1.0f;	PS_INPUT output = (PS_INPUT)0;	output.Pos = mul( input.Pos, World );	output.Pos = mul( output.Pos, View );	output.Pos = mul( output.Pos, Projection );output.TexCoord = input.TexCoord;float3 Normal = normalize( mul( input.Normal, (float3x3) World ) );float3 View = normalize( eye - (float3) input.Pos );float3 Phi = reflect( normalize( LightDirection ), Normal );output.Color = CalcPhongLighting( DiffuseColor, Normal, LightDirection, View, Phi );	return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( PS_INPUT input ) : SV_TARGET{return tex2D.Sample( linearSampler, input.TexCoord ) * input.Color;}//--------------------------------------------------------------------------------------technique10 Render{   pass P0	{		SetVertexShader( CompileShader( vs_4_0, VS() ) );		SetGeometryShader( NULL );		SetPixelShader( CompileShader( ps_4_0, PS() ) );	}}

I simply don't know how to transform all the local matrices from each mesh to a single world matrix (which is the one I should pass to the shader, I suppose)

This is how the scene looks like when all the meshes have been set to their (0, 0, 0)


Thanks in advance.