1 #include "Creature.hpp"
5 #include <glm/gtx/transform.hpp>
15 Creature::~Creature() {
19 glm::dmat4 Creature::LocalTransform() noexcept {
20 // TODO: surface transform
21 constexpr double half_height = 0.25;
22 return glm::translate(glm::dvec3(position.x, position.y, position.z + body->Radius() + half_height))
23 * glm::scale(glm::dvec3(half_height, half_height, half_height));
26 void Creature::BuildVAO() {
29 vao.EnableAttribute(0);
30 vao.EnableAttribute(1);
31 vao.EnableAttribute(2);
32 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
33 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
34 vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
35 vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
37 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
38 const float offset = 1.0f;
39 for (int surface = 0; surface < 6; ++surface) {
40 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
41 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
43 attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
44 attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
45 attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
46 attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
47 attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
48 attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
49 attrib[4 * surface + 0].texture.x = tex_u_begin;
50 attrib[4 * surface + 0].texture.y = 1.0f;
51 attrib[4 * surface + 0].texture.z = surface;
53 attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
54 attrib[4 * surface + 1].position[(surface + 1) % 3] = offset;
55 attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
56 attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
57 attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
58 attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
59 attrib[4 * surface + 1].texture.x = tex_u_end;
60 attrib[4 * surface + 1].texture.y = 1.0f;
61 attrib[4 * surface + 1].texture.z = surface;
63 attrib[4 * surface + 2].position[(surface + 0) % 3] = offset;
64 attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
65 attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
66 attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
67 attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
68 attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
69 attrib[4 * surface + 2].texture.x = tex_u_begin;
70 attrib[4 * surface + 2].texture.y = 0.0f;
71 attrib[4 * surface + 2].texture.z = surface;
73 attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
74 attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
75 attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
76 attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
77 attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
78 attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
79 attrib[4 * surface + 3].texture.x = tex_u_end;
80 attrib[4 * surface + 3].texture.y = 0.0f;
81 attrib[4 * surface + 3].texture.z = surface;
85 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
87 auto element = vao.MapElements(GL_WRITE_ONLY);
88 for (int surface = 0; surface < 3; ++surface) {
89 element[6 * surface + 0] = 4 * surface + 0;
90 element[6 * surface + 1] = 4 * surface + 2;
91 element[6 * surface + 2] = 4 * surface + 1;
92 element[6 * surface + 3] = 4 * surface + 1;
93 element[6 * surface + 4] = 4 * surface + 2;
94 element[6 * surface + 5] = 4 * surface + 3;
96 for (int surface = 3; surface < 6; ++surface) {
97 element[6 * surface + 0] = 4 * surface + 0;
98 element[6 * surface + 1] = 4 * surface + 1;
99 element[6 * surface + 2] = 4 * surface + 2;
100 element[6 * surface + 3] = 4 * surface + 2;
101 element[6 * surface + 4] = 4 * surface + 1;
102 element[6 * surface + 5] = 4 * surface + 3;
108 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
110 vao.DrawTriangles(6 * 6);