dxtest2
diff src/mesh.cc @ 0:6ed01ded71d8
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 23 Jun 2013 04:23:13 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.cc Sun Jun 23 04:23:13 2013 +0300 1.3 @@ -0,0 +1,110 @@ 1.4 +#include <d3dut.h> 1.5 +#include "mesh.h" 1.6 + 1.7 + 1.8 +/*static ID3D11InputLayout *vertex_layout; 1.9 + 1.10 +static ID3D11InputLayout *create_vertex_layout() 1.11 +{ 1.12 + static const D3D11_INPUT_ELEMENT_DESC elem_desc[] = { 1.13 + {"position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, pos), D3D11_INPUT_PER_VERTEX_DATA, 0}, 1.14 + {"normal", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, normal), D3D11_INPUT_PER_VERTEX_DATA, 0}, 1.15 + {"texcoord", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, texcoord), D3D11_INPUT_PER_VERTEX_DATA, 0} 1.16 + }; 1.17 + static const int num_elem = sizeof elem_desc / sizeof *elem_desc; 1.18 + 1.19 + if(d3dut_dev->CreateInputLayout(elem_desc, num_elem, 1.20 +}*/ 1.21 + 1.22 + 1.23 +Mesh::Mesh() 1.24 +{ 1.25 + vbuf = 0; 1.26 + cur_norm = Vector3(0, 1, 0); 1.27 +} 1.28 + 1.29 +Mesh::~Mesh() 1.30 +{ 1.31 + if(vbuf) { 1.32 + vbuf->Release(); 1.33 + } 1.34 +} 1.35 + 1.36 +void Mesh::clear() 1.37 +{ 1.38 + invalidate_vbuffer(); 1.39 + verts.clear(); 1.40 +} 1.41 + 1.42 +void Mesh::normal(float x, float y, float z) 1.43 +{ 1.44 + cur_norm.x = x; 1.45 + cur_norm.y = y; 1.46 + cur_norm.z = z; 1.47 +} 1.48 + 1.49 +void Mesh::texcoord(float u, float v) 1.50 +{ 1.51 + cur_texcoord.x = u; 1.52 + cur_texcoord.y = v; 1.53 +} 1.54 + 1.55 +void Mesh::vertex(float x, float y, float z) 1.56 +{ 1.57 + Vertex v; 1.58 + v.pos = Vector3(x, y, z); 1.59 + v.normal = cur_norm; 1.60 + v.texcoord = cur_texcoord; 1.61 + verts.push_back(v); 1.62 + 1.63 + invalidate_vbuffer(); 1.64 +} 1.65 + 1.66 + 1.67 +void Mesh::draw() const 1.68 +{ 1.69 + ((Mesh*)this)->update_vbuffer(); 1.70 + 1.71 + unsigned int stride = sizeof(Vertex); 1.72 + unsigned int offset = 0; 1.73 + d3dut_ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 1.74 + d3dut_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 1.75 + 1.76 + d3dut_ctx->Draw(verts.size(), 0); 1.77 +} 1.78 + 1.79 + 1.80 +bool Mesh::update_vbuffer() 1.81 +{ 1.82 + if(vbuf) { 1.83 + return true; 1.84 + } 1.85 + 1.86 + if(verts.empty()) { 1.87 + return false; 1.88 + } 1.89 + 1.90 + D3D11_BUFFER_DESC bufdesc; 1.91 + memset(&bufdesc, 0, sizeof bufdesc); 1.92 + bufdesc.Usage = D3D11_USAGE_DEFAULT; 1.93 + bufdesc.ByteWidth = verts.size() * sizeof(Vertex); 1.94 + bufdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 1.95 + 1.96 + D3D11_SUBRESOURCE_DATA subres; 1.97 + memset(&subres, 0, sizeof subres); 1.98 + subres.pSysMem = &verts[0]; 1.99 + 1.100 + if(d3dut_dev->CreateBuffer(&bufdesc, &subres, &vbuf) != 0) { 1.101 + fprintf(stderr, "failed to create vertex buffer\n"); 1.102 + return false; 1.103 + } 1.104 + return true; 1.105 +} 1.106 + 1.107 +void Mesh::invalidate_vbuffer() 1.108 +{ 1.109 + if(vbuf) { 1.110 + vbuf->Release(); 1.111 + vbuf = 0; 1.112 + } 1.113 +} 1.114 \ No newline at end of file