Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Local to World space

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Joakitex   Members   


Posted 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.

#2 BornToCode   Members   


Posted 10 February 2012 - 08:14 PM

You usually past the vertices into an format where you multiply World*View*Projection*vertex will put it on the screen. There is no such thing really as local space. Local space is whatever space you define your geometry in which can be in any form. As long it makes sense when you multiply your World*View*Projection by that incomming vertex and it gives you a proper pixel on screen you should be fine.

#3 Joakitex   Members   


Posted 10 February 2012 - 10:46 PM

Well, I am quite sure that I am making the multiplications correctly, because the objects get properly drawn on screen, but I have ran into some issues and I simply do not know how to solve them.

- I cannot rotate an object over it's own axis, because I can never translate the object to the center of the scene.
- I am not getting proper lightning, probably because of the matrix thing too. I mean, the objects get proper lightning if I do not rotate them (as you can see in the picture I posted), but when I do the lightning just gets really weird.

#4 iedoc   GDNet+   


Posted 11 February 2012 - 12:00 AM

The world matrix is what your put the objects transformations in, so the translation, rotation and scale is put into the world matrix for that object. Remember again the order that you multiply matters. Each transformation you do is relative to the axis (0, 0, 0), So if your objects are not centered around 0, 0, 0, and you try to scale the object, it will appear to be stretched, so you should try to center your objects around the point (0,0,0) in the modeling program. The same goes for rotation. the rotation is done around the point (0,0,0), so if your object is not centered, it will be rotating around the point 0,0,0, and not the objects own center. You can fix this by first translating the object to be centered around the point (0,0,0). First you will have to find your objects true center. So if you want to have the object rotate around it's own center, it would look something like this:

D3DXVECTOR3 objectsTrueCenter;

D3DXMATRIX TranslationTrueCenter;

D3DXMatrixTranslation(&TranslationTrueCenter, -objectsTrueCenter.x, -objectsTrueCenter.y, -objectsTrueCenter.z); // Subtract the objects true center from it's position, so it is centered around 0,0,0

D3DXMATRIX Rotation; // Now create your rotation matrix

D3DXMATRIX Scale; // If you have one, you need to make sure the object is centered around 0,0,0 before you use it, or it will become stretched. since objects are usually centered around 0,0,0 by default, you will want to do a scale before you translate it to another position

D3DXMATRIX worldTranslation; // Now translate the object to the place you want in the world. If your object was already in the place you wanted it (before the translation to 0,0,0), then you can just use the objectsTrueCenter again to put it back in it's place

D3DXMATRIX worldMatrix = TranslationTrueCenter * Scale * Rotation * worldTranslation;

You need to create a world matrix for every object like i already said. About the lighting, it's maybe because you are not rotating your normals correctly
Try multiplying the normal the same way you multiply the vertex position, with all three world view and projection matrices
Braynzar Soft - DirectX Lessons & Game Programming Resources!

#5 Joakitex   Members   


Posted 12 February 2012 - 12:47 PM

Thanks, that helped me with the rotation problem, I haven't solved it completely yet but now I know where this is going. I will have to keep looking in the lightning thing to see what is wrong.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.