• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Joakitex

Local to World space

4 posts in this topic

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:

[source='C++']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;}[/source]

[color=#000000]Here is the RenderObject function:[/color]

[color=#000000][source='C++']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;}[/source]

Here is my shader:[/color]

[source='HLSL']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() ) ); }}[/source]

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)

[spoiler]
[img]http://i.imgur.com/e0l4t.jpg[/img]
[/spoiler]

Thanks in advance.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0