-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestshapes.cpp
335 lines (287 loc) · 11.5 KB
/
testshapes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "testshapes.hpp"
#include "camera.hpp"
#ifdef WIN32
#include <DirectXMath.h>
#endif
// Shapes for testing purposes
unsigned Triangle::program = 0;
GLuint Triangle::vao = 0;
GLuint Triangle::vbo = 0;
#ifdef WIN32
ID3D11Buffer* Triangle::d3d11buffer = nullptr;
ID3D11InputLayout* Triangle::d3d11_input_layout = nullptr;
extern ID3D11Device* g_device11;
extern ID3D11DeviceContext *g_context11;
extern ID3D11VertexShader* g_vs_default_palette, *g_vs_simpletexture;
extern ID3D11PixelShader* g_ps_default_palette, *g_ps_simpletexture;
extern ID3DBlob *g_vs_default_palette_blob;
extern ID3DBlob *g_ps_default_palette_blob;
extern DirectX::XMMATRIX g_projection_d3d11;
extern ID3D11Buffer* g_perobject_cb_default_palette;
extern void UpdateGlobalPerObjectCB(const DirectX::XMMATRIX* M, const DirectX::XMMATRIX* V, const DirectX::XMMATRIX* P);
extern ID3DBlob* g_vs_textrender_blob, * g_ps_textrender_blob;
extern ID3D11BlendState* g_blendstate11;
extern ID3D11Buffer* g_simpletexture_cb;
#endif
extern glm::mat4 g_projection;
extern Camera* GetCurrentSceneCamera();
float Triangle::base_vertices_and_attrib_ccw[] = {
0.0f, 0.0f, 0.0f, 0.0f, 128.0f, 0.0f,
9.0f, 0.0f, 0.0f, 0.0f, 240.0f, 0.0f,
0.0f, 9.0f, 0.0f, 0.0f, 87.0f, 0.0f,
};
float Triangle::base_vertices_and_attrib_cw[] = {
0.0f, 0.0f, 0.0f, 128.0f,
0.0f, 9.0f, 0.0f, 87.0f,
9.0f, 0.0f, 0.0f, 240.0f,
};
#ifdef WIN32
struct DefaultPalettePerObjectCB {
DirectX::XMMATRIX M, V, P;
};
#endif
void Triangle::Init(unsigned prog) {
if (vao != 0) return;
program = prog;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(base_vertices_and_attrib_ccw), base_vertices_and_attrib_ccw, GL_STATIC_DRAW);
// XYZ Pos
const size_t stride = 6 * sizeof(GLfloat);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)0);
glEnableVertexAttribArray(0);
// Normal Idx
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// Data
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(4*sizeof(GLfloat)));
glEnableVertexAttribArray(2);
// AO Index
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(5*sizeof(GLfloat)));
glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
}
#ifdef WIN32
void Triangle::Init_D3D11() {
D3D11_BUFFER_DESC desc = { };
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.ByteWidth = sizeof(base_vertices_and_attrib_cw);
desc.StructureByteStride = sizeof(float) * 4;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA srd = { };
srd.pSysMem = base_vertices_and_attrib_cw;
srd.SysMemPitch = sizeof(base_vertices_and_attrib_cw);
assert(SUCCEEDED(g_device11->CreateBuffer(&desc, &srd, &d3d11buffer)));
D3D11_INPUT_ELEMENT_DESC inputdesc1[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 0, DXGI_FORMAT_R32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 1, DXGI_FORMAT_R32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // COLOR1
{ "COLOR" , 2, DXGI_FORMAT_R32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // COLOR2
};
assert(SUCCEEDED(g_device11->CreateInputLayout(inputdesc1, 4, g_vs_default_palette_blob->GetBufferPointer(),
g_vs_default_palette_blob->GetBufferSize(), &d3d11_input_layout)));
}
#endif
Triangle::Triangle() {
}
void Triangle::Render() {
glUseProgram(program);
GLuint mLoc = glGetUniformLocation(program, "M");
GLuint vLoc = glGetUniformLocation(program, "V");
GLuint pLoc = glGetUniformLocation(program, "P");
glm::mat4 M(1), V = GetCurrentSceneCamera()->GetViewMatrix(), P = g_projection;
M = glm::translate(M, pos);
glUniformMatrix4fv(mLoc, 1, GL_FALSE, &(M[0][0]));
glUniformMatrix4fv(vLoc, 1, GL_FALSE, &(V[0][0]));
glUniformMatrix4fv(pLoc, 1, GL_FALSE, &(P[0][0]));
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
}
#ifdef WIN32
void Triangle::Render_D3D11() {
g_context11->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
const UINT stride = sizeof(float)*4, zero = 0; // Why do I need to specify the stride here again
g_context11->IASetVertexBuffers(0, 1, &d3d11buffer, &stride, &zero);
g_context11->IASetInputLayout(d3d11_input_layout);
DirectX::XMVECTOR pos_d3d;
pos_d3d.m128_f32[0] = pos.x;
pos_d3d.m128_f32[1] = pos.y;
pos_d3d.m128_f32[2] = -pos.z;
DirectX::XMMATRIX M = DirectX::XMMatrixTranslationFromVector(pos_d3d);
UpdateGlobalPerObjectCB(&M, nullptr, nullptr);
g_context11->Draw(3, 0);
}
#endif
//
unsigned ColorCube::program = 0;
GLuint ColorCube::vao = 0, ColorCube::vbo = 0;
#ifdef WIN32
ID3D11InputLayout* ColorCube::d3d11_input_layout = nullptr;
ID3D11Buffer* ColorCube::d3d11_vertex_buffer = nullptr;
#endif
float ColorCube::base_vertices_and_attrib[] = {
-0.5f, -0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 8.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 19.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 19.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 19.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 19.0f, 0.0f, 0.0f,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, -0.5f, 0.0f, 127.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 189.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 189.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 189.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 189.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 189.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 189.0f, 0.0f,
};
ColorCube::ColorCube() {
}
void ColorCube::Init(unsigned prog) {
if (vao != 0) return;
program = prog;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(base_vertices_and_attrib), base_vertices_and_attrib, GL_STATIC_DRAW);
// XYZ Pos
const size_t stride = 6 * sizeof(GLfloat);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)0);
glEnableVertexAttribArray(0);
// Normal Idx
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// Data
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(4 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
// AO Index
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(5 * sizeof(GLfloat)));
glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
}
#ifdef WIN32
void ColorCube::Init_D3D11() {
// Vertex Buffer
D3D11_BUFFER_DESC desc = { };
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.ByteWidth = sizeof(base_vertices_and_attrib_cw);
desc.StructureByteStride = sizeof(float) * 6;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA srd = { };
srd.pSysMem = base_vertices_and_attrib_cw;
srd.SysMemPitch = sizeof(base_vertices_and_attrib_cw);
assert(SUCCEEDED(g_device11->CreateBuffer(&desc, &srd, &d3d11_vertex_buffer)));
D3D11_INPUT_ELEMENT_DESC inputdesc1[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 0, DXGI_FORMAT_R32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 1, DXGI_FORMAT_R32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 2, DXGI_FORMAT_R32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
assert(SUCCEEDED(g_device11->CreateInputLayout(inputdesc1, 4, g_vs_default_palette_blob->GetBufferPointer(),
g_vs_default_palette_blob->GetBufferSize(), &d3d11_input_layout)));
}
#endif
void ColorCube::Render() {
glUseProgram(program);
GLuint mLoc = glGetUniformLocation(program, "M");
GLuint vLoc = glGetUniformLocation(program, "V");
GLuint pLoc = glGetUniformLocation(program, "P");
glm::mat4 M(1), V = GetCurrentSceneCamera()->GetViewMatrix(), P = g_projection;
M = glm::translate(M, pos);
glUniformMatrix4fv(mLoc, 1, GL_FALSE, &(M[0][0]));
glUniformMatrix4fv(vLoc, 1, GL_FALSE, &(V[0][0]));
glUniformMatrix4fv(pLoc, 1, GL_FALSE, &(P[0][0]));
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glUseProgram(0);
}
#ifdef WIN32
void ColorCube::Render_D3D11() {
g_context11->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
const UINT stride = sizeof(float) * 6, zero = 0; // I need to specify the stride here again
g_context11->IASetVertexBuffers(0, 1, &d3d11_vertex_buffer, &stride, &zero);
g_context11->IASetInputLayout(d3d11_input_layout);
//g_context11->VSSetShader(g_vs_default_palette, nullptr, 0);
//g_context11->PSSetShader(g_ps_default_palette, nullptr, 0);
DirectX::XMVECTOR pos_d3d;
pos_d3d.m128_f32[0] = pos.x;
pos_d3d.m128_f32[1] = pos.y;
pos_d3d.m128_f32[2] = -pos.z;
DirectX::XMMATRIX M = DirectX::XMMatrixTranslationFromVector(pos_d3d);
UpdateGlobalPerObjectCB(&M, nullptr, nullptr);
g_context11->VSSetConstantBuffers(0, 1, &g_perobject_cb_default_palette);
g_context11->Draw(36, 0);
}
#endif