-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyUtils.h
195 lines (153 loc) · 5.6 KB
/
MyUtils.h
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
#ifndef APP_H
#define APP_H
#define GLEW_STATIC
#define CY_GL_DONT_CHECK_CONTEXT
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "IMGUI/imgui.h"
#include "IMGUI/imgui_impl_glfw.h"
#include "IMGUI/imgui_impl_opengl3.h"
#include "TF/trasnferfunc.h"
#include "cyCodeBase/cyTriMesh.h"
#include "cyCodeBase/cyMatrix.h"
#include "cyCodeBase/cyGL.h"
#include <iostream>
#include <fstream>
#include "camera.h"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
cy::Vec3f cameraPos(0.0f, 0.0f, 10.0f);
cy::Vec3f cameraTarget(0.0f, 0.0f, 0.0f);
cy::Vec3f cameraUp(0.0f, 1.0f, 0.0f);
Camera camera(cameraPos,cameraTarget,cameraUp);
double pre_xpos = 0;
double pre_ypos = 0;
cy::Vec3f lightPos(5.0f, 2.5f, 1.0f);
float lightyaw = 11.3f;
float lightpitch = 26.5f;
bool onImgui = false;
bool blendmode = false;
float isovalue = 0.46;
float stepsize = 100.0;
float background[3] = {0.0f, 0.0f, 0.0f};
float radian(float degree)
{
return degree * M_PI / 180.0f;
}
/**
* @brief
* Create a mesh with a vao and a vbo. This method is based on convention
* position is 3d, normal is 3d, and uv is 2d.
*
* @return mesh vao ID
*/
unsigned int GL_CreateVAO(float *vertices, size_t size, size_t stride, bool enable_nor, bool enable_uv)
{
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void *)0);
if (enable_nor)
{
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void *)(3 * sizeof(float)));
}
if (enable_uv)
{
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void *)(6 * sizeof(float)));
}
return VAO;
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
// glfw: mouse button
//---------------------------------------------------------------------------
void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos)
{
int left_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
int right_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
if (left_state == GLFW_PRESS && !onImgui)
{
if (xpos > pre_xpos)
camera.RotateHorizontal(1.0f);
if (xpos < pre_xpos)
camera.RotateHorizontal(-1.0f);
if (ypos > pre_ypos)
camera.RotateVertical(1.0f);
if (ypos < pre_ypos)
camera.RotateVertical(-1.0f);
pre_xpos = xpos;
pre_ypos = ypos;
}
if (right_state == GLFW_PRESS && !onImgui)
{
if (xpos > pre_xpos)
camera.Zoom(3.0f);
if (xpos < pre_xpos)
camera.Zoom(-3.0f);
pre_xpos = xpos;
pre_ypos = ypos;
}
}
int GL_CreateTexture3D(char* filename, int x, int y, int z)
{
char* data;
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (file.is_open())
{
data = (char*) malloc(x * y * z);
file.read(data, x * y * z);
file.close();
}
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, x, y, z, 0, GL_RED, GL_UNSIGNED_BYTE, data);
free(data);
return texture;
}
int GL_CreateTexture1D(char* data, int x)
{
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_1D, texture);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, x, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
return texture;
}
int GL_DrawCube( int size_vertices, int size_indices, float* vertices, int* indices)
{
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, size_vertices, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size_indices, indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
return VAO;
}
#endif