Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Everything posted by haansn08

  1.   Yes - and it's not just valid it's also good practice: http://www.fatagnus.com/program-to-an-interface-not-an-implementation/   And you can type var (C#) or auto (C++) or whatever if you want the compliler to recognize the type automatically. (But it disguises the type e.g. if the return value of a function is not clear.)
  2. Hello there, I'm trying to learn how to use the effect framework and tried to recreate my "grid scene" (a floor grid with colored axis), but this time with effects instead of own compiled shaders But I'm having a couple of problems with it not least, because the framework seems to remove the shader from the device context: (This can't work...)   My effect file: cbuffer cbPerObject : register(b0) { float4x4 worldViewProj; }; struct VertexIn { float3 pos : POSITION; float4 col : COLOR; }; struct VertexOut { float4 pos : SV_POSITION; float4 col : COLOR; }; VertexOut VS (VertexIn input) { VertexOut vout; vout.pos = float4 (input.pos, 1.0f); vout.pos = mul(vout.pos, worldViewProj); vout.col = input.col; return vout; } float4 PS(VertexOut input) : SV_TARGET { return input.col; } RasterizerState SolidCull { FillMode = Solid; CullMode = Back; }; technique11 ColorTech { pass P0 { SetVertexShader(CompileShader(vs_5_0, VS())); SetPixelShader (CompileShader(ps_5_0, PS())); SetRasterizerState (SolidCull); } } Loading the effect: ID3D10Blob *fx; ID3D10Blob *error; if (FAILED(D3DX11CompileFromFileA("Color.fx", nullptr, nullptr, 0, "fx_5_0", D3D10_SHADER_DEBUG | D3D10_SHADER_SKIP_OPTIMIZATION, 0, 0, &fx, &error, 0))) { throw (char*)error->GetBufferPointer(); } if (FAILED(D3DX11CreateEffectFromMemory(fx->GetBufferPointer(), fx->GetBufferSize(), 0, _dev, &_FXColor))) throw "Can't create effect."; fx->Release(); Rendering Code: XMMATRIX gridMatrix = _camera.GetCameraMatrix() * _proj; _FXColor_WorldViewProj->SetMatrix((float*)(&gridMatrix)); auto colorTech = _FXColor->GetTechniqueByName("ColorTech"); D3DX11_TECHNIQUE_DESC colorTechDesc; colorTech->GetDesc(&colorTechDesc); for (UINT pass = 0; pass < colorTechDesc.Passes; pass++) { colorTech->GetPassByIndex(pass)->Apply(0, _devcon); _grid->Render(); } The Apply function seems to set the shaders to zero.
  3. I just found out I didn't set an input layout! But it still doesn't work: When calling ID3D11SwapChain::Present the screen goes black and I get a strange error message, later when I clear the back buffer...   This seems to work: D3DX11_PASS_DESC passDesc; _FXColor->GetTechniqueByIndex(0)->GetPassByIndex(0)->GetDesc(&passDesc); D3D11_INPUT_ELEMENT_DESC layout[2]; VertexFormats::VertexPositionColor::GetDesc(layout); if (FAILED(_dev->CreateInputLayout(layout, 2, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &_layout))) throw "Failed to create input layout."; But of course I need the input layout...   EDIT: I found  the problem - it seems like my graphics card doesn't support the specified shader version... (But the Effect version needs to be 5.0... ?)
  4. colorTechDesc.Passes is one - and Apply return S_OK. :\
  5. Hello there, I'm currently trying to implement a simple light shader, but I have a couple of problems with it. The diffuse lightning works fine, but the specular highlight seems not very correct. This is my pixel shader: struct VertexOut { float4 posH : SV_POSITION; float3 posW : POSITION; float3 normal : NORMAL; }; cbuffer cbPerObject : register (b0) { float3 eyePos; float3 lightDirection; float4 lightColor; float lightStrength; float4 ambient; float4 diffuse; float4 specular; float specular_power; }; float4 main (VertexOut input) : SV_TARGET { //Calculate diffuse light float3 light0 = normalize(lightDirection); float _diff_fac = max(mul(light0, input.normal), 0); float4 _diff = _diff_fac * (diffuse * lightColor) * lightStrength; //Ambient light float4 _ambient = ambient; //Specular light //Calculate the angle float4 _spec = float4(0.0f,0.0f,0.0f,0.0f); [flatten] if (_diff_fac > 0.0f) { float3 toEye = normalize(input.posW - eyePos); float3 reflection = normalize(reflect(-light0, input.normal)); float _spec_fac = pow(max(dot(reflection, toEye), 0), specular_power); _spec = _spec_fac * specular; } return _diff + _ambient + _spec; } And my vertex shader: cbuffer cbPerObject : register (b0) { float4x4 world; float4x4 worldViewProj; float4x4 worldInvTrans; }; struct VertexIn { float3 pos : POSITION; float3 normal : NORMAL; }; struct VertexOut { float4 posH : SV_POSITION; float3 posW : POSITION; float3 normal : NORMAL; }; VertexOut main (VertexIn input) { VertexOut output; //Transform vertex output.posH = mul(float4(input.pos,1.0f), worldViewProj); output.posW = mul(input.pos, world); //Transform normal output.normal = mul (input.normal, (float3x3)worldInvTrans); return output; } I checked the constant buffer of the pixel shader and everything seems to be right... Here are some screenshots (I increase the specular_power variable linear with the time)   As you can see, the specular highlight dismishes over time and is replaced by the diffuse lightning - but actually it should shrink to a point.   Please tell me if you need more source code.
  6. haansn08

    Indices of an obj file

    I found the problem(s): 1: I only read 2 vertices instead of 3 per triangle //Only triangles are supported! if (triangle_data.size() != 4) //1st element is a empty string! throw invalid_argument("OBJ file contains primitive data other than Triangle Lists. (NGON, Lines, ...)"); //Read the vertices //for (int iVertex = 0; iVertex < 3; iVertex++) //<---- Wrong for (int iVertex = 1; iVertex < 4; iVertex++) //Process each vertex 2: I skip every secound triangle, since I jump to the next line after processing one triangle, but since I use getline the '\n' char is already read. if (data_type == "f") //current line contains a triangle specification { ReadFace(file, positions, normals, vertices, indices); continue; //ReadFace does already read the \n char! } //Simply ignore other commands for now #undef max //Skip the line file.ignore(numeric_limits<streamsize>::max(), '\n'); #define max
  7. Hello there, I'm trying to load an .obj wavefront file (for simplicity reasons - later I want to read other - better - fromats). The models are exported with Blender 2.68a. I assume that the vertices are read correctly - the problem is rather in the index generating part as this picture suggests:   (The left model was recreated from the Input Assembler input)   The main problem with loading the indices is similar to this: http://www.gamedev.net/topic/589494-opengl-es-20-how-to-obtain-indices-from-a-wavefront-obj-file/#entry4741209   My solution to the problem is to look in each face definition if the vertex already exists and then add the corresponding index. Otherwise the new vertex is added to a list and then add a the index (wich is the size of the vertex array...) //Split the line with delim ' ' (Space) to gather the data of each vertex vector<string> triangle_data = Split(line,' '); //Vetor of vertex data //Only triangles are supported! if (triangle_data.size() != 4) //1st element is a empty string! throw invalid_argument("OBJ file contains primitive data other than Triangle Lists. (NGON, L ines, ...)"); //Read the vertices for (int iVertex = 0; iVertex < 3; iVertex++) //Process each vertex { /* one vertex data looks like this: 5/8/9 or 6//4 */ string vertex_data = triangle_data.at(iVertex); if (vertex_data == "") continue; //Split vertex_data with delimeter '/' to gather indices of position and normal vector<string> vertex_index_data = Split(vertex_data); //Get Position data UINT position_index = stoi(vertex_index_data.at(0)) - 1; UINT normal_index = stoi(vertex_index_data.at(2)) - 1; if (position_index >= position_data.size() || normal_index >= normal_data.size()) continue; //We should never get here XMFLOAT3 pos = position_data.at (position_index); XMFLOAT3 nrm = normal_data.at (normal_index); //Assemble the vertex VertexFormats::VertexPositionNormal vertex; vertex.position = pos; vertex.normal = nrm; //Update the vertex and index vectors //Check if the vertex already exists auto vertex_pos = find(vertices.begin(), vertices.end(), vertex); if (vertex_pos == vertices.end()) { //The vertex does not exist - add it to the vertex list vertices.push_back(vertex); //Update the index list indices.push_back(vertices.size() - 1); } else { //the vertex already exists - add the corresponding index indices.push_back(vertex_pos - vertices.begin()); } } //for each vertex (The whole code of the class is attached.)
  8. haansn08

    GDC 2013: Interview with Kate Craig

  9. Hello there, I'm trying to build a class that is responsible for loading and managing a vertex shader. The idea is to have a separate class for managing the constant buffer (the data a vertex shader needs.) This class looks like this:   dxman_cbuffer.h #pragma once #include "dxman_common.h" #include <D3D11.h> template <typename Buffer> class ConstantBuffer { private: ID3D11Buffer *_buffer; const Buffer *_content; public: HRESULT Init(ID3D11Device* dev, Buffer* initialData); void Exit(); ID3D11Buffer* GetBuffer() const; HRESULT SetBufferData (ID3D11DeviceContext* devcon, Buffer *content); const Buffer* GetBufferData() const; }; The problem occours in this class:   ColorShader.h #pragma once #include "common.h" #include "dxman_shader.h" #include "dxman_cbuffer.h" class VSColor : public IVertexShader { private: struct SMatrixBuffer { XMMATRIX worldViewProj; }; ConstantBuffer<SMatrixBuffer> _cbPerObject; public: virtual HRESULT Init (ID3D11Device *dev); //Loads the shader & creates the constant buffer virtual void Exit(); //Releases all resources virtual void Activate (ID3D11DeviceContext* devcon); //Sets the shader and the constant buffer to the Vertex Shader stage virtual HRESULT UpdateMatrixBuffer (ID3D11DeviceContext *devcon, XMMATRIX &worldViewProj); //Updates the buffer data }; Since the template class ConstantBuffer<SMatrixBuffer> is never declared, the linker can't find any symbols. The problem is that I can't just write ConstantBuffer<SMatrixBuffer> in dxman_cbuffer.cpp (where the ConstantBuffer class is) since SMatrixBuffer is declared as private in another class. (And I want to keep both files separated!)
  10. OK, it works. Altough I really have no clue why... 
  11. Hello there, I'm trying to draw some "terrain" (when you can call a displaced grid without color a terrain ^^), and (again) nothing is shown on the screen. With the graphics debugging tools of VS2012 I found out that there is no pixel shader stage similiar to this problem: http://msdn.microsoft.com/en-us/library/vstudio/jj191650.aspx   Altough I do set a pixel shader:   The code of my pixel shader: struct VertexOut { float4 pos : POSITION; float4 col : COLOR; }; float4 main(VertexOut input) : SV_TARGET { return input.col; }
  12. Hm. Probably the bug is somewhere else. I have attached the source code of the whole project now.
  13. Yes. This code creates the viewport: //Set the viewport D3D11_VIEWPORT viewport; ZeroMemory(&viewport, sizeof(viewport)); viewport.Width = float(width); viewport.Height = float(height); viewport.MaxDepth = 1.0f; _devcon->RSSetViewports(1, &viewport); And my vertex shader code: cbuffer perObject { float4x4 worldViewProj; }; struct VertexOut { float4 pos : SV_POSITION; float4 col : COLOR; }; VertexOut main( float4 pos : POSITION) { VertexOut ret; ret.pos = mul(pos, worldViewProj); ret.col = float4(1.0f,1.0f,1.0f,1.0f); return ret; }
  14. Hm changing the VertexOut structure to: struct VertexOut { float4 pos : SV_POSITION; float4 col : COLOR; }; didn't help much. Returning any color in the pixel shader neither did.   But I'm pretty sure that the matrices are correct since the image under "Vertex Shader" looks like this:  
  15. Hello there, I'm having a problem with extreme flickering. In fact it's like I only draw every 50th frame something - the other frames are the background color. I figured out, if I remove the clear call it doesn't flicker at all, but it's drawn with these "letfovers" from the previous frame. It also doesn't flicker when I don't update the constant buffers. I'm using the backbuffer. I'm drawing in a wxWigets window in the idle event. (This shouldn't matter as wxWidgets uses native controls and windows...)   This is my render loop: float color[] = {0.0f,0.25f,0.5f,1.0f}; _devcon->ClearRenderTargetView(_target, color); _devcon->ClearDepthStencilView(_depthStencil, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f,0); //Do rendering stuff here //Set the primitive Topology _devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //Set index & vertex buffers _devcon->IASetIndexBuffer(_indexBuffer, DXGI_FORMAT_R32_UINT, 0); UINT stride = sizeof(Vertex), offset = 0; _devcon->IASetVertexBuffers(0, 1, &_vertexBuffer, &stride, &offset); //update the constant buffer HRESULT hRes; static float rotation = 0; rotation += 0.001f; D3D11_MAPPED_SUBRESOURCE mbuf; hRes = _devcon->Map(_matrixBuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mbuf); if (FAILED(hRes)) return false; MatrixBuffer *matrices; matrices = (MatrixBuffer*)(mbuf.pData); matrices->world = XMMatrixRotationY(rotation) * XMMatrixRotationX(rotation / 2); matrices->world = XMMatrixTranspose(matrices->world); _devcon->Unmap(_matrixBuf, 0); _devcon->VSSetConstantBuffers(0, 1, &_matrixBuf); //Activate the shaders _devcon->IASetInputLayout(_inputLayout); _devcon->VSSetShader(_vertexShader, 0, 0); _devcon->PSSetShader(_pixelShader, 0, 0); _devcon->DrawIndexed(18, 0, 0); _swapchain->Present(0,0); return true;
  16. Good to know. Worked perfectly. But I have 3 questions: Why would the reference dirver work differntly on this flag? Why does it render sometimes - there should be a need of a vaild view and projection matrix, but I didn't set anything. Why does it work without the clear call?
  17. I tested it now with the reference driver - and it didn't flicker! (Great first touch with d3d11! :()  
  18. haansn08

    Nothing is drawn

    C# I did this because XMMatrixLookAtLH expects a XMVector as an argument put I can't construct these so easily! Solved it this way: XMFLOAT3 pos (0.0f,0.0f,-5.0f); XMFLOAT3 at(0.0f,0.0f,0.0f); XMFLOAT3 up (0.0f,1.0f,0.0f); m.view = XMMatrixLookAtLH(XMLoadFloat3(&pos),XMLoadFloat3(&at),XMLoadFloat3(&up)); But I found the problem: I was missing the call (Sorry, Zaoshi Kaba, I didn't read the line where you said I needed to set my render target - should've read your post more exactly,) _devcon->OMSetRenderTargets(1, &_target, _depthStencil); (Clearing the Z-Buffer and transposing the matrices was also a problem - thanks everybody!)
  19. Hello, I'm still trying to get something on my screen except a blue rectangle, (I guess it is the 5th time I make this project :(  ) But nothing is drawn. I can't explain why - I have very little experience with Direct3D. I'm not even sure my shaders get called.   Here's my render function: float color[] = {0.0f,0.25f,0.5f,1.0f}; _devcon->ClearRenderTargetView(_target, color); //Do rendering stuff here //Set the primitive Topology _devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //Set index & vertex buffers _devcon->IASetIndexBuffer(_indexBuffer, DXGI_FORMAT_R32_UINT, 0); UINT stride = sizeof(Vertex), offset = 0; _devcon->IASetVertexBuffers(0, 1, &_vertexBuffer, &stride, &offset); //Activate the shaders _devcon->IASetInputLayout(_inputLayout); _devcon->VSSetShader(_vertexShader, 0, 0); _devcon->PSSetShader(_pixelShader, 0, 0); //Set the constant buffers ID3D11Buffer *matrixBuf; D3D11_BUFFER_DESC cbd; cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbd.ByteWidth = sizeof(MatrixBuffer); cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; cbd.MiscFlags = 0; cbd.Usage = D3D11_USAGE_DYNAMIC; cbd.StructureByteStride = 0; MatrixBuffer m; m.world = XMMatrixIdentity(); m.view = XMMatrixLookAtLH(XMLoadFloat3(new XMFLOAT3(0.0f,0.0f,-5.0f)), XMLoadFloat3(new XMFLOAT3(0.0f,0.0f,0.0f)), XMLoadFloat3(new XMFLOAT3(0.0f,1.0f,0.0f))); m.projection = XMMatrixPerspectiveFovLH(XM_PIDIV4, 1.777f, 0.01f, 10000.0f); D3D11_SUBRESOURCE_DATA matrixData; matrixData.pSysMem = &m; matrixData.SysMemPitch = 0; matrixData.SysMemSlicePitch = 0; HRESULT hRes = _dev->CreateBuffer(&cbd, &matrixData, &matrixBuf); if (FAILED(hRes)) return false; _devcon->VSSetConstantBuffers(0, 1, &matrixBuf); _devcon->DrawIndexed(18, 0, 0); _swapchain->Present(0,0); return true; Btw. The structure MatrixBuffer is something like: struct MatrixBuffer { XMMATRIX world; XMMATRIX view; XMMATRIX projection; }; And here's my vertex shader: cbuffer cbPerObject { float4x4 world; float4x4 view; float4x4 projection; }; struct VertexIn { float3 pos : POSITION; float4 col : COLOR; }; struct VertexOut { float4 pos : SV_POSITION; float4 col : COLOR; }; VertexOut main (VertexIn input) { VertexOut vout; vout.pos = float4 (input.pos, 1.0f); vout.pos = mul(vout.pos, world); vout.pos = mul(vout.pos, view); vout.pos = mul(vout.pos, projection); vout.col = input.col; return vout; } The shaders compile (hopefully) properly. (The result from D3DXCompileFromFile is S_OK!)
  20. haansn08

    Nothing is drawn

    Yeah - I know this site and many commands I've learned from there, but I don't like how little code is explained... Now I'm following the book "Introduction to 3D Game Programming with DirectX 11" (Frank Luna) as good as I can (e.g. I can't use the Effect Library for some reason...)
  21. haansn08

    Nothing is drawn

    Transposing the matrices didn't help (But you were right - I forgot to do so ) My pixel shader is just passing the color: struct VertexOut { float4 pos : SV_POSITION; float4 col : COLOR; }; float4 main(VertexOut input) : SV_TARGET { return input.col; } This is how I create the input layout. D3D11_INPUT_ELEMENT_DESC layout[] = { {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; HR(_dev->CreateInputLayout(layout, 2, vs->GetBufferPointer(), vs->GetBufferSize(), &_inputLayout)); And this guy should create the geometry of a pyramid: HRESULT PyramidApp::InitGeometry() { //Create the vertices Vertex pyramid_vertices[] = { {XMFLOAT3(+0.0f, +2.0f, +0.0f), XMFLOAT4(1.0f, 0.5f, 0.25f, 1.0f)}, //Spitze 0 {XMFLOAT3(-1.0f, -1.0f, +1.0f), XMFLOAT4(0.25f, 0.5f, 0.5f, 1.0f)}, //Links vorne 1 {XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4(0.5f, 1.0f, 0.5f, 1.0f)}, //Links hinten 2 {XMFLOAT3(+1.0f, -1.0f, +1.0f), XMFLOAT4(0.5f, 0.5f, 0.25f, 1.0f)}, //rechts vorne 3 {XMFLOAT3(+1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 1.0f, 0.5f, 1.0f)}, //rechts hinten 4 }; //Create a vertex buffer D3D11_BUFFER_DESC vbd; ZeroMemory(&vbd, sizeof(vbd)); vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.ByteWidth = sizeof(pyramid_vertices); vbd.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA vdata; vdata.pSysMem = pyramid_vertices; HR(_dev->CreateBuffer(&vbd, &vdata, &_vertexBuffer)); //Create the index Buffer UINT pyramid_indices[] = { 0, 3, 1, 0, 4, 3, 0, 2, 4, 0, 1, 2, 1, 3, 2, 2, 3, 4 }; D3D11_BUFFER_DESC ibd; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.ByteWidth = sizeof(pyramid_indices); ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; ibd.StructureByteStride = 0; ibd.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA idata; idata.pSysMem = pyramid_indices; HR(_dev->CreateBuffer(&ibd, &idata, &_indexBuffer)); return S_OK; } The entire source code is in the attachment ;) (Sorry for the incorrect use of design patterns - I'm only 14 and the GoF book is difficult to understand for a non-english...)
  22. Hello, Im a beginner to graphics programming (altough I made some simple scenes with D3D9) and want to learn D3D11. I just tried to draw a single triangle, but I see nothing! #include "stdafx.h" #include <sstream> #include <Windows.h> #include <D3D11.h> #include <D3DX10.h> #include <D3DX11.h> #pragma comment (lib, "d3d11.lib") #pragma comment (lib, "d3dx10.lib") #pragma comment (lib, "d3dx11.lib") #define CHECK() if(FAILED(hRes))return hRes; using namespace std; struct SVertex { float x,y,z; D3DXCOLOR col; }; IDXGISwapChain *swap_chain; //Pointer zur SwapChain ID3D11Device *device; ID3D11DeviceContext *devcon; ID3D11RenderTargetView *target; ID3D11VertexShader *vertex_shader; ID3D11PixelShader *pixel_shader; ID3D11Buffer *vertex_buffer; ID3D11InputLayout *input_layout; HRESULT InitShaders() { HRESULT hRes; //Compile the shaders ID3D10Blob *vs = nullptr, *ps = nullptr; ID3D10Blob *error = nullptr; hRes = D3DX11CompileFromFile(L"shader.hlsl", NULL, NULL, "VShader", "vs_4_0", NULL, NULL, NULL, &vs, &error, NULL); if (error) { stringstream msg; msg << "Vertexshader - Error" << endl << (char*)error->GetBufferPointer(); MessageBoxA(NULL,msg.str().data(), "Error", MB_OK|MB_ICONERROR); return S_OK; //?! } hRes = D3DX11CompileFromFile(L"shader.hlsl", NULL, NULL, "PShader", "ps_4_0", NULL, NULL, NULL, &ps, &error, NULL);//CHECK(); if (error) { stringstream msg; msg << "Pixelshader - Error" << endl << (char*)error->GetBufferPointer(); MessageBoxA(NULL,msg.str().data(), "Error", MB_OK|MB_ICONERROR); return S_OK; //?! } //Create the input layout D3D11_INPUT_ELEMENT_DESC layout[]= { {"POSITION",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"COLOR", 0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0} }; hRes = device->CreateInputLayout(layout, sizeof(layout)/sizeof(D3D11_INPUT_ELEMENT_DESC), vs->GetBufferPointer(), vs->GetBufferSize(), &input_layout); CHECK(); //Set the input layout devcon->IASetInputLayout(input_layout); //Create the shaders hRes = device->CreateVertexShader(vs->GetBufferPointer(), vs->GetBufferSize(), NULL, &vertex_shader);CHECK(); hRes = device->CreatePixelShader(ps->GetBufferPointer(), ps->GetBufferSize(), NULL, &pixel_shader);CHECK(); //Set the shaders as active devcon->VSSetShader(vertex_shader, NULL, NULL); devcon->PSSetShader(pixel_shader, NULL, NULL); //Cleanup vs->Release(); ps->Release(); return S_OK; } HRESULT InitGeometry() { HRESULT hRes; //Create a vertex buffer D3D11_BUFFER_DESC vbuf; ZeroMemory(&vbuf, sizeof(vbuf)); vbuf.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbuf.ByteWidth = sizeof(SVertex)*3; //Size of the vertex Buffer vbuf.Usage = D3D11_USAGE_DYNAMIC; //Both CPU and GPU can write vbuf.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; hRes = device->CreateBuffer(&vbuf, NULL, &vertex_buffer);CHECK(); //Create the buffer //Fill the buffer //Generate the vertices SVertex triangle[] = { {-1.0f,-1.0f ,1.0f,D3DXCOLOR(1.0f,0.0f,0.0f,1.0f)}, //Links unten, rot {0.0f, 1.0f, 1.0f,D3DXCOLOR(0.0f,1.0f,0.0f,1.0f)}, //Mitte oben, grün {1.0f,-1.0f, 1.0f,D3DXCOLOR(0.0f,0.0f,1.0f,1.0f)} //Rechts unten, blau }; //Lock the buffer D3D11_MAPPED_SUBRESOURCE resc; hRes = devcon->Map(vertex_buffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &resc); CHECK(); resc.pData = triangle; //Unlock the buffer devcon->Unmap(vertex_buffer, NULL); return S_OK; } HRESULT InitDirectX (HWND window) { HRESULT hRes; //SwapChain erstellen DXGI_SWAP_CHAIN_DESC scdesc; ZeroMemory(&scdesc, sizeof(scdesc)); scdesc.BufferCount = 1; scdesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; scdesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scdesc.OutputWindow = window; scdesc.Windowed = true; scdesc.SampleDesc.Count = 1; scdesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; hRes = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL,NULL, NULL, D3D11_SDK_VERSION, &scdesc, &swap_chain, &device, NULL, &devcon); //hRes = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &device, NULL, &devcon); CHECK(); //Get the background Buffer ID3D11Texture2D *backBuffer; swap_chain->GetBuffer(NULL, __uuidof(ID3D11Texture2D), (void**)(&backBuffer)); //Create the render target device->CreateRenderTargetView(backBuffer, NULL, &target); backBuffer->Release(); devcon->OMSetRenderTargets(1, &target, NULL); //Set the viewport D3D11_VIEWPORT viewport; ZeroMemory (&viewport, sizeof(viewport)); viewport.Width = 1000; viewport.Height = 900; devcon->RSSetViewports(1, &viewport); hRes = InitShaders(); CHECK(); hRes = InitGeometry(); CHECK(); return S_OK; } void ExitDirectX() { swap_chain->SetFullscreenState(false, NULL); vertex_shader->Release(); pixel_shader->Release(); target->Release(); devcon->Release(); device->Release(); swap_chain->Release(); } bool Render() { devcon->ClearRenderTargetView(target, D3DXCOLOR(0.0f,0.25f,0.5f, 1.0f)); //Render! //Set the vertex buffer UINT stride = sizeof(SVertex); UINT offset = 0; devcon->IASetVertexBuffers(0,1, &vertex_buffer, &stride, &offset); //Set the primive topology devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); devcon->Draw(3, 0); swap_chain->Present(0,0); return true; } The shader "shader.hlsl": struct VOut { float4 position : SV_POSITION; float4 color : COLOR; }; VOut VShader(float4 position : POSITION, float4 color : COLOR) { VOut output; output.position = position; output.color = color; return output; } float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET { return color; } Since I have little experience in D3D and no error message or code I can't really help myself out. I hope you can help.
  23. haansn08

    I can't see anything?

    Yeah, That's the tutorial I was following... I fixed it by replacing resc.pData = triangle; with an memcpy call, but the colors aren't quite as expected...
  • 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!