-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.hpp
138 lines (114 loc) · 3.56 KB
/
shape.hpp
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
#pragma once
#include <array>
#include <cassert>
#include <vector>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.hpp"
#include "texture.hpp"
#include "camera.hpp"
#include "material.hpp"
#include "utils.hpp"
class ShapeObject {
public:
glm::mat4 model = glm::mat4(1.0f);
ShapeObject() {
}
virtual void draw(const Camera &cam, std::shared_ptr<Material> material) {
}
};
class Triangle : public ShapeObject {
public:
std::array<float, 9> vertices;
GLuint vbo, vao;
Triangle(const std::array<Point3f, 3>& points) {
for (int i = 0; i < 3; ++i) {
vertices[i * 3] = points[i].x;
vertices[i * 3 + 1] = points[i].y;
vertices[i * 3 + 2] = points[i].z;
}
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
}
void draw(const Camera& cam, std::shared_ptr<Material> material) override {
material->apply(model, cam);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
};
class Sphere : public ShapeObject {
public:
std::vector<float> vertices;
std::vector<GLuint> indices;
GLuint vbo, vao, ebo;
Sphere(float r = 1.0f) {
float pi = glm::pi<float>();
int m_step = 50;
float step = 2 * pi / m_step;
for (int i = 0; i <= m_step / 2; ++i) {
float y = r * glm::cos(step * i);
if (i == 0) {
y = r;
}
else if (i == m_step / 2) {
y = -r;
}
float rho = r * glm::sin(step * i);
for (int j = 0; j <= m_step; ++j) {
float theta = step * j;
float x = rho * glm::cos(theta);
float z = -rho * glm::sin(theta);
if (i == 0 || i == m_step / 2) {
x = .0f;
z = .0f;
}
vertices.push_back(x);
vertices.push_back(y);
vertices.push_back(z);
vertices.push_back((float)j / m_step);
vertices.push_back(1.0f - (step * i) / pi);
vertices.push_back(x/r);
vertices.push_back(y/r);
vertices.push_back(z/r);
if (j == m_step) {
continue;
}
if (1 <= i && i < m_step/2) {
indices.push_back((i) * (m_step + 1) + j);
indices.push_back((i - 1) * (m_step + 1) + j);
indices.push_back((i) * (m_step + 1) + (j + 1));
indices.push_back((i - 1) * (m_step + 1) + j);
indices.push_back((i - 1) * (m_step + 1) + (j + 1));
indices.push_back((i) * (m_step + 1) + (j + 1));
}
}
}
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(5 * sizeof(float)));
glEnableVertexAttribArray(2);
// glDeleteBuffers(1, &vbo);
}
void draw(const Camera &cam, std::shared_ptr<Material> material) override {
material->apply(model, cam);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}
};