PeriDyno 1.0.0
Loading...
Searching...
No Matches
Mesh.cpp
Go to the documentation of this file.
1#include "Mesh.h"
2
3#include <glad/glad.h>
4#include <vector>
5
6#include <math.h>
7
8namespace dyno
9{
10
12 {
14 ibo.create(GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW);
15 vbo.create(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
17 bindVertexBuffer(&vbo, 0, 3, GL_FLOAT, 0, 0, 0);
18 }
19
21 {
22 unbind();
23 // release buffers
24 vbo.release();
25 ibo.release();
26
27 // release VAO
29 }
30
31
32 void Mesh::draw(int instance)
33 {
34 this->bind();
35
36 if (instance > 0)
37 glDrawElementsInstanced(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0, instance);
38 else
39 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
40
41 this->unbind();
42 }
43
44 // helper functions
45 Mesh* Mesh::Sphere(float radius, int sectorCount, int stackCount)
46 { // create sphere
47 // http://www.songho.ca/opengl/gl_sphere.html
48
49 std::vector<float> vertices;
50 std::vector<float> normals;
51 std::vector<float> texCoords;
52 std::vector<int> indices;
53
54 float x, y, z, xy; // vertex position
55 float nx, ny, nz, lengthInv = 1.0f / radius; // vertex normal
56 float s, t; // vertex texCoord
57
58 const float PI = 3.14159265f;
59 float sectorStep = 2 * PI / sectorCount;
60 float stackStep = PI / stackCount;
61 float sectorAngle, stackAngle;
62
63 for (int i = 0; i <= stackCount; ++i)
64 {
65 stackAngle = PI / 2 - i * stackStep; // starting from pi/2 to -pi/2
66 xy = radius * cosf(stackAngle); // r * cos(u)
67 z = radius * sinf(stackAngle); // r * sin(u)
68
69 // add (sectorCount+1) vertices per stack
70 // the first and last vertices have same position and normal, but different tex coords
71 for (int j = 0; j <= sectorCount; ++j)
72 {
73 sectorAngle = j * sectorStep; // starting from 0 to 2pi
74
75 // vertex position (x, y, z)
76 x = xy * cosf(sectorAngle); // r * cos(u) * cos(v)
77 y = xy * sinf(sectorAngle); // r * cos(u) * sin(v)
78 vertices.push_back(x);
79 vertices.push_back(y);
80 vertices.push_back(z);
81
82 // normalized vertex normal (nx, ny, nz)
83 nx = x * lengthInv;
84 ny = y * lengthInv;
85 nz = z * lengthInv;
86 normals.push_back(nx);
87 normals.push_back(ny);
88 normals.push_back(nz);
89
90 // vertex tex coord (s, t) range between [0, 1]
91 s = (float)j / sectorCount;
92 t = (float)i / stackCount;
93 texCoords.push_back(s);
94 texCoords.push_back(t);
95 }
96 }
97
98 // generate CCW index list of sphere triangles
99 int k1, k2;
100 for (int i = 0; i < stackCount; ++i)
101 {
102 k1 = i * (sectorCount + 1); // beginning of current stack
103 k2 = k1 + sectorCount + 1; // beginning of next stack
104
105 for (int j = 0; j < sectorCount; ++j, ++k1, ++k2)
106 {
107 // 2 triangles per sector excluding first and last stacks
108 // k1 => k2 => k1+1
109 if (i != 0)
110 {
111 indices.push_back(k1);
112 indices.push_back(k2);
113 indices.push_back(k1 + 1);
114 }
115
116 // k1+1 => k2 => k2+1
117 if (i != (stackCount - 1))
118 {
119 indices.push_back(k1 + 1);
120 indices.push_back(k2);
121 indices.push_back(k2 + 1);
122 }
123 }
124 }
125
126 Mesh* mesh = new Mesh;
127 mesh->create();
128 mesh->vbo.load(vertices.data(), vertices.size() * sizeof(float));
129 mesh->ibo.load(indices.data(), indices.size() * sizeof(unsigned int));
130 mesh->count = indices.size();
131 return mesh;
132 }
133
135 {
136 std::vector<float> vertices = {
137 -1, -1, 0,
138 -1, 1, 0,
139 1, -1, 0,
140 1, 1, 0
141 };
142
143 std::vector<unsigned int> indices = {
144 0, 2, 1,
145 2, 3, 1
146 };
147
148 Mesh* mesh = new Mesh;
149 mesh->create();
150 mesh->vbo.load(vertices.data(), vertices.size() * sizeof(float));
151 mesh->ibo.load(indices.data(), indices.size() * sizeof(unsigned int));
152 mesh->count = indices.size();
153 return mesh;
154 }
155
156 Mesh* Mesh::Plane(float scale)
157 {
158 std::vector<float> vertices = {
159 -scale, -0.0001f, -scale,
160 -scale, -0.0001f, scale,
161 scale, -0.0001f, -scale,
162 scale, -0.0001f, scale,
163 };
164
165 std::vector<unsigned int> indices = {
166 0, 1, 2,
167 2, 1, 3
168 };
169
170 Mesh* mesh = new Mesh;
171 mesh->create();
172 mesh->vbo.load(vertices.data(), vertices.size() * sizeof(float));
173 mesh->ibo.load(indices.data(), indices.size() * sizeof(unsigned int));
174 mesh->count = indices.size();
175 return mesh;
176 }
178 {
179 float p0[3] = {-1, -1, -1};
180 float p1[3] = { 1, 1, 1 };
181 float vertices[]{
182 p0[0], p0[1], p0[2],
183 p0[0], p0[1], p1[2],
184 p1[0], p0[1], p1[2],
185 p1[0], p0[1], p0[2],
186
187 p0[0], p1[1], p0[2],
188 p0[0], p1[1], p1[2],
189 p1[0], p1[1], p1[2],
190 p1[0], p1[1], p0[2],
191 };
192
193 unsigned int indices[]
194 {
195 0, 1, 2, 2, 3, 0, // bottom
196 7, 6, 5, 5, 4, 7, // top
197 4, 5, 1, 1, 0, 4, // left
198 3, 2, 6, 6, 7, 3, // right
199 0, 3, 7, 7, 4, 0, // front
200 5, 6, 2, 2, 1, 5, // back
201 };
202
203 Mesh* mesh = new Mesh;
204 mesh->create();
205 mesh->vbo.load(vertices, 8 * 3 * sizeof(float));
206 mesh->ibo.load(indices, 36 * sizeof(unsigned int));
207 mesh->count = 36;
208 return mesh;
209 }
210}
virtual void load(void *data, int size, int offset=0)
Definition Buffer.cpp:55
static Mesh * Sphere(float radius=1.f, int sectors=16, int stacks=8)
Definition Mesh.cpp:45
virtual void release() override
Definition Mesh.cpp:20
virtual void create() override
Definition Mesh.cpp:11
static Mesh * Plane(float scale)
Definition Mesh.cpp:156
virtual void draw(int instance=0)
Definition Mesh.cpp:32
static Mesh * Cube()
Definition Mesh.cpp:177
Buffer vbo
Definition Mesh.h:40
static Mesh * ScreenQuad()
Definition Mesh.cpp:134
Buffer ibo
Definition Mesh.h:41
int count
Definition Mesh.h:42
virtual void unbind()
virtual void bindIndexBuffer(Buffer *buffer)
virtual void release() override
virtual void bind()
virtual void bindVertexBuffer(Buffer *buffer, int index, int size, int type, int stride, int offset, int divisor)
virtual void create() override
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25