dxtest2

view 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 source
1 #include <d3dut.h>
2 #include "mesh.h"
5 /*static ID3D11InputLayout *vertex_layout;
7 static ID3D11InputLayout *create_vertex_layout()
8 {
9 static const D3D11_INPUT_ELEMENT_DESC elem_desc[] = {
10 {"position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, pos), D3D11_INPUT_PER_VERTEX_DATA, 0},
11 {"normal", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, normal), D3D11_INPUT_PER_VERTEX_DATA, 0},
12 {"texcoord", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, texcoord), D3D11_INPUT_PER_VERTEX_DATA, 0}
13 };
14 static const int num_elem = sizeof elem_desc / sizeof *elem_desc;
16 if(d3dut_dev->CreateInputLayout(elem_desc, num_elem,
17 }*/
20 Mesh::Mesh()
21 {
22 vbuf = 0;
23 cur_norm = Vector3(0, 1, 0);
24 }
26 Mesh::~Mesh()
27 {
28 if(vbuf) {
29 vbuf->Release();
30 }
31 }
33 void Mesh::clear()
34 {
35 invalidate_vbuffer();
36 verts.clear();
37 }
39 void Mesh::normal(float x, float y, float z)
40 {
41 cur_norm.x = x;
42 cur_norm.y = y;
43 cur_norm.z = z;
44 }
46 void Mesh::texcoord(float u, float v)
47 {
48 cur_texcoord.x = u;
49 cur_texcoord.y = v;
50 }
52 void Mesh::vertex(float x, float y, float z)
53 {
54 Vertex v;
55 v.pos = Vector3(x, y, z);
56 v.normal = cur_norm;
57 v.texcoord = cur_texcoord;
58 verts.push_back(v);
60 invalidate_vbuffer();
61 }
64 void Mesh::draw() const
65 {
66 ((Mesh*)this)->update_vbuffer();
68 unsigned int stride = sizeof(Vertex);
69 unsigned int offset = 0;
70 d3dut_ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset);
71 d3dut_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
73 d3dut_ctx->Draw(verts.size(), 0);
74 }
77 bool Mesh::update_vbuffer()
78 {
79 if(vbuf) {
80 return true;
81 }
83 if(verts.empty()) {
84 return false;
85 }
87 D3D11_BUFFER_DESC bufdesc;
88 memset(&bufdesc, 0, sizeof bufdesc);
89 bufdesc.Usage = D3D11_USAGE_DEFAULT;
90 bufdesc.ByteWidth = verts.size() * sizeof(Vertex);
91 bufdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
93 D3D11_SUBRESOURCE_DATA subres;
94 memset(&subres, 0, sizeof subres);
95 subres.pSysMem = &verts[0];
97 if(d3dut_dev->CreateBuffer(&bufdesc, &subres, &vbuf) != 0) {
98 fprintf(stderr, "failed to create vertex buffer\n");
99 return false;
100 }
101 return true;
102 }
104 void Mesh::invalidate_vbuffer()
105 {
106 if(vbuf) {
107 vbuf->Release();
108 vbuf = 0;
109 }