-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.h
More file actions
377 lines (322 loc) · 7.76 KB
/
main.h
File metadata and controls
377 lines (322 loc) · 7.76 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <unordered_map>
//GLM
#include "glm/glm.hpp"
#include <GL/glew.h>
//structs
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN,
ROTATE_X_UP,
ROTATE_X_DOWN,
ROTATE_Y_UP,
ROTATE_Y_DOWN,
ROTATE_Z_UP,
ROTATE_Z_DOWN,
};
struct CenteredView {
// Position
glm::vec3 size;
// Normal
glm::vec3 center;
// TexCoords
glm::mat4 transform;
//camera position
glm::vec3 cameracenter;
};
struct angelV{
glm::vec3 position;
float angel;
};
struct Vertex {
// Position
glm::vec3 Position;
// Normal
glm::vec3 Normal;
// TexCoords
glm::vec2 TexCoords;
glm::vec3 Tangent;
glm::vec3 Bitangent;
};
struct obj_index {
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> tex_coods;
};
// ============= constant =================================
// glm::vec3(0.0, 0.0, 0.0),
// glm::vec3(0.0, 0.0, 1.0),
// glm::vec3(0.0, 1.0, 0.0),
// glm::vec3(0.0, 1.0, 1.0),
// glm::vec3(1.0, 0.0, 0.0),
// glm::vec3(1.0, 0.0, 1.0),
// glm::vec3(1.0, 1.0, 0.0),
// glm::vec3(1.0, 1.0, 1.0)
// ============================================================
//utils
GLubyte * load_3d_raw_data(std::string texture_path, glm::vec3 dimension);
CenteredView getMinMax(std::vector<Vertex> &vertices);
CenteredView getCubeMinMax();
void load1DTexturecolorBar(unsigned int textureID);
//============================================
//view
// Default camera values
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.5f;
const float ZOOM = 45.0f;
class Camera
{
public:
// Camera view parameters
glm::vec3 ori_position;
glm::vec3 ori_front;
glm::vec3 ori_up;
glm::vec3 ori_right;
glm::vec3 position;
glm::vec3 front;
glm::vec3 up;
glm::vec3 right;
// Camera projection parameters
float ori_zoom;
float zoom;
float near;
float far;
unsigned int width;
unsigned int height;
// Camera projection matrix: used for projection
glm::mat4 proj_mat;
// Camera view matrix: used for changing camera rotation and position
glm::mat4 view_mat;
// Camera parameter initialization
Camera(glm::vec3 position_ ,
glm::vec3 front_,
glm::vec3 up_ ,
glm::vec3 right_,
float zoom_,
float near_ ,
float far_ ,
unsigned int width_,
unsigned int height_);
void init();
void reset();
void HandelKeyboardInput(Camera_Movement direction, GLfloat delta_time);
// Rotate specific angle along local camera system(LCS)
void rotate_x(GLfloat angle);
void rotate_y(GLfloat angle);
void rotate_z(GLfloat angle);
glm::vec3 GetViewDir();
// Get camera view matrix
glm::mat4 get_view_mat();
// Get camera projection matrix
glm::mat4 get_projection_mat();
};
//====================================================================
//Shader
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};
class Shader
{
private:
unsigned int m_RendererID;
std::string m_FilePath;
std::unordered_map<std::string, int> m_UniformLocationCache;
public:
Shader(const std::string& filepath);
~Shader();
void Bind() const;
void Unbind() const;
void SetUniform3f(const std::string& name, float f0, float f1, float f2);
void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3);
void SetUniformMat4f(const std::string& name, const glm::mat4& matrix);
void SetUniformInt(const std::string& name, int i);
void SetUniform1f(const std::string& name, float f);
private:
int GetUniformLocation(const std::string& name);
struct ShaderProgramSource ParseShader(const std::string& filepath);
unsigned int CompileShader(unsigned int type, const std::string& source);
unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader);
};
//=============================================
//mesh
class Mesh {
public:
/* Mesh Data */
//cubes coordinates for binding 3d texture
GLfloat cube_vertices[24] = {
0.0, 0.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
0.0, 1.0, 1.0,
1.0, 0.0, 0.0,
1.0, 0.0, 1.0,
1.0, 1.0, 0.0,
1.0, 1.0, 1.0
};
GLuint cube_indices[36] = {
1,5,7,
7,3,1,
0,2,6,
6,4,0,
0,1,3,
3,2,0,
7,5,4,
4,6,7,
2,3,7,
7,6,2,
1,0,4,
4,5,1
};
GLuint cube_edges[24] = {
1,5,
5,7,
7,3,
3,1,
0,4,
4,6,
6,2,
2,0,
0,1,
2,3,
4,5,
6,7
};
//vector<Texture> textures;
/* Functions */
Mesh(int ID);
void Draw();
void Bind();
private:
/* Render data */
unsigned int VAO, VBO, EBO;
GLuint GBO[2];
int len;
/* Functions */
void CreateMeshVBO();
};
//=================================================
//textures
class Texture
{
private:
unsigned int m_RendererID;
std::string m_FilePath;
GLubyte * m_LocalBuffer;
// Glint g_volTexObj;
public:
Texture(const std::string& path,glm::vec3 dimension);
~Texture();
void Bind(unsigned int slot = 0) const;
void Unbind() const;
// inline int GetWidth() const { return m_Width; }
// inline int GetHeight() const { return m_Height; }
};
class RawObject{
public:
RawObject(std::string file_path,glm::vec3 dimension);
std::string file_Path;
glm::vec3 dimension;
};
class TextureSlice{
public:
glm::vec3 center;
TextureSlice(int ID);
void getSortedVertex(std::vector<glm::vec3> &vertices);
void add(glm::vec3 vertex);
static bool PseudoangleComparator(angelV V1,angelV V2){
return V1.angel < V2.angel;
};
private:
static float Pseudoangle(glm::vec3 p1, glm::vec3 p2);
void ComputeCenter();
std::vector<glm::vec3> sliceVertex;
std::vector<angelV> angelVertex;
};
class cubeSlice{
public:
cubeSlice(glm::vec3 viewDirection);
void setNumSlice(int numSlices);
// void updateSlice(glm::vec3 viewDirection,int numSlices,std::vector<glm::vec3> &vTextureSlices);
void getNewSlices(std::vector<std::vector<glm::vec3> > &slices,std::vector<int> lengthArr);
void getZMinMax();
void linePlaneIntersection();
void calculateIntersect();
int getNumSlice();
glm::vec3 cube_vertices[8] = {
glm::vec3(-0.5,-0.5,-0.5),
glm::vec3(0.5,-0.5,-0.5),
glm::vec3(0.5, 0.5,-0.5),
glm::vec3(-0.5, 0.5,-0.5),
glm::vec3(-0.5,-0.5, 0.5),
glm::vec3(0.5,-0.5, 0.5),
glm::vec3(0.5, 0.5, 0.5),
glm::vec3(-0.5, 0.5, 0.5)
};
GLuint cube_indices[36] = {
1,5,7,
7,3,1,
0,2,6,
6,4,0,
0,1,3,
3,2,0,
7,5,4,
4,6,7,
2,3,7,
7,6,2,
1,0,4,
4,5,1
};
GLuint cube_edges[24] = {
1,5,
5,7,
7,3,
3,1,
0,4,
4,6,
6,2,
2,0,
0,1,
2,3,
4,5,
6,7
};
private:
glm::vec3 edgeStart[12];
glm::vec3 edgeDir[12];
float lambda[12];
float lambda_inc[12];
float denom;
glm::vec3 viewDir;
int numberOfSlice;
float max_x;
float min_x;
float max_y;
float min_y;
float max_z;
float min_z;
float daltaZ;
int minZidx;
int maxZidx;
int count;
const float EPSILON = 0.0001f;
const int MAX_SLICES = 2000;
// glm::vec3 vTextureSlices[numberOfSlice * 12];
std::vector<TextureSlice> SlicePositionArray;
};