Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

138 Neutral

About Joakitex

  • Rank
  1. Joakitex

    Problem passing matrices to shader

    Yay! thanks everyone. Transposing the matrices before passing them to the video card did the trick!
  2. Joakitex

    Problem passing matrices to shader

    Thanks, I'll try that out. About the PIX thing, I am concerned about the pre and post Vertex shader thing, shouldn't the post Vertex shader output be equal or at least similar to the pre Vertex shader output?
  3. 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: [spoiler] bool CShader::CreateConstantBuffers(ID3D11Device *pDev) { D3D11_BUFFER_DESC matrixDesc; matrixDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; matrixDesc.ByteWidth = sizeof(MatrixType); matrixDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; matrixDesc.MiscFlags = 0; matrixDesc.Usage = D3D11_USAGE_DYNAMIC; matrixDesc.StructureByteStride = 0; if ( FAILED(pDev->CreateBuffer(&matrixDesc, 0, &pMatrixBuffer)) ) return ::Error(L"Unable to create constant buffer"); return true; }[/spoiler] This two functions do the rendering (the last one is the problem): [spoiler] bool CShader::RenderObject(ID3D11DeviceContext *pDevCon, ID3D11Device *pDev, CModel *model, CCamera *camera, unsigned int stride, unsigned int offset) { // Send information to the video card pDevCon->IASetVertexBuffers(0, 1, model->ppGetVertexBuffer(), &stride, &offset); pDevCon->IASetIndexBuffer(model->pGetIndexBuffer(), DXGI_FORMAT_R32_UINT, 0); pDevCon->IASetInputLayout(pInputLayout); if (! SetShaderVariables(pDevCon, model, 0, camera, RENDER_MODE_MODEL) ) return false; pDevCon->DrawIndexed(model->GetNumIndices(), 0, 0); return true; } bool CShader::SetShaderVariables(ID3D11DeviceContext *pDevCon, CModel *model, CSprite *sprite, CCamera *camera, unsigned int renderMode) { D3D11_MAPPED_SUBRESOURCE mappedSubResource; if ( FAILED(pDevCon->Map(pMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubResource)) ) return ::Error(L"Could not lock the buffer"); MatrixType *pData = (MatrixType*)mappedSubResource.pData; pData->World = model->worldMatrix; pData->View = camera->viewMatrix; pData->Projection = camera->projectionMatrix; pDevCon->Unmap(pMatrixBuffer, 0); pDevCon->VSSetConstantBuffers(0, 1, &pMatrixBuffer); pDevCon->VSSetShader(pVertexShader, 0, 0); pDevCon->PSSetShader(pPixelShader, 0, 0); return true; }[/spoiler] This is my matrix struct: [spoiler] struct MatrixType { D3DXMATRIX World; D3DXMATRIX View; D3DXMATRIX Projection; }; [/spoiler] And this is my shader: [spoiler] cbuffer cbPerObj : register ( b0 ) { matrix World : packoffset ( c0 ); matrix View : packoffset ( c4 ); matrix Projection : packoffset ( c8 ); } struct VS_INPUT { float4 Pos : POSITION; float3 Normal : NORMAL; float2 TexCoord : TEXCOORD; }; struct PS_INPUT { float4 Pos : SV_POSITION; float2 TexCoord : TEXCOORD0; float3 Normal : NORMAL; float4 Color : COLOR0; }; PS_INPUT VS( VS_INPUT input ) { 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; return output; } float4 PS( PS_INPUT input ) : SV_TARGET { return float4(1.0f, 0.0f, 0.0f, 1.0f); } [/spoiler] Here's some PIX info [spoiler] [/spoiler] Thanks in advance.
  4. Thanks again, I'll take a look at that.
  5. Thanks both of you, this helped me a lot. Also, a noob question. Do the constant buffers always get padded out to a multiple of the size of the biggest member of the buffer? If I had this in the shader struct PointLight { float4 LightColor; float2 SomeData; }; The code in C++ would be like this, am I right? struct PointLight { D3DXVECTOR4 LColor; D3DXVECTOR2 SomeData; D3DXVECTOR2 Padding; };
  6. 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? Example: I want to pass this D3DXVECTOR3 LPosition(0.0f, 0.0f, 0.0f); Into this PointLightInstance.LightPosition; How should I do this? Thanks in advance
  7. Joakitex

    Local to World space

    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.
  8. Joakitex

    Local to World space

    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.
  9. 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=#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=#000000]D3D10_TECHNIQUE_DESC technique;[color=#000000]pWorldMatrixVariable->SetMatrix( (float*) &model->worldMatrix);pViewMatrixVariable->SetMatrix( (float*) &camera->viewMatrix );pProjectionMatrixVariable->SetMatrix( (float*) &camera->projectionMatrix );[color=#000000]for (unsigned int t = 0; t < model->GetpMaterial()->pGetTextures()->size(); t++){ pTextureVariable->SetResource( model->GetpMaterial()->pGetTextureByIndex(t) );}[color=#000000]pTechnique->GetDesc(&technique);[color=#000000]for (UINT p = 0; p < technique.Passes; ++p){ pTechnique->GetPassByIndex(p)->Apply(0); pEffectDevice->DrawIndexed(model->GetNumIndices(), 0, 0);}[color=#000000]return;}[/source] Here is my shader: [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] [/spoiler] Thanks in advance.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!