-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
210 lines (182 loc) · 6.34 KB
/
main.cpp
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
#include "MyUtils.h"
class App
{
public:
char *AppName;
char *Path;
int x, y, z;
App(char *name, char *path, int x, int y, int z)
{
AppName = name;
Path = path;
this->x = x;
this->y = y;
this->z = z;
}
void run()
{
init();
createShader();
mainloop();
clean();
}
private:
GLFWwindow *window;
cy::GLSLProgram prog;
void init()
{
if (!glfwInit())
std::cout << "glfw error" << std::endl;
const char *glsl_version = "#version 330";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, this->AppName, NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "glfw error" << std::endl;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
std::cout << "glew error" << std::endl;
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// Setup Dear ImGui style
ImGui::StyleColorsDark();
glEnable(GL_TEXTURE_3D);
glEnable(GL_DEPTH_TEST);
}
void createShader()
{
prog.BuildFiles("./shaders/quad.vs",
"./shaders/quad.fs");
}
void mainloop()
{
float Cube[] = {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f};
int Order[] = {
// front
0, 1, 2,
1, 2, 3,
// back
4, 5, 6,
4, 6, 7,
// bottom
0, 2, 5,
2, 4, 5,
// top
1, 3, 6,
3, 6, 7,
// left
0, 1, 5,
1, 5, 6,
// right
2, 3, 4,
3, 4, 7};
GL_DrawCube(sizeof(Cube), sizeof(Order), Cube, Order);
// Assuming data size is a byte = sizeof(char)
int texture = GL_CreateTexture3D(this->Path, this->x, this->y, this->z);
cy::Matrix4f projection = projection.Perspective(radian(45.0f),
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
cy::Matrix4f view, model, s, t;
cy::Vec4f tmp;
cy::Vec3f cameraPosInSTR, m;
m = cy::Vec3f(-0.5f, -0.5f, 0.5f);
t.SetTranslation(m);
s.SetScale(2.0f, 2.0f, 2.0f);
model = s * t;
ImGuiIO &io = ImGui::GetIO();
tfnw::TransferFunctionWidget tfn_widget;
auto colormap = tfn_widget.get_colormap();
int tfID = GL_CreateTexture1D((char*) colormap.data(), colormap.size()/4);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
if (tfn_widget.changed())
{
auto colormap = tfn_widget.get_colormap();
glTexImage1D(GL_TEXTURE_1D,
0,
GL_RGBA8,
colormap.size() / 4,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
colormap.data());
}
glfwPollEvents();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
view.SetView(camera.pos, camera.lookat, camera.up);
prog.Bind();
prog["volumeTex"] = 0;
prog["tfTex"] = 1;
prog["backtofront"] = (uint8_t)blendmode;
prog["isovalue"] = isovalue;
prog["stepsize"] = stepsize;
prog["background"] = cy::Vec3f(background[0], background[1], background[2]);
prog["MVP"] = projection * view * model;
// Just translate in opposite direction
// In general, just multiply by M inverse and negate z component.
tmp = model.GetInverse() * camera.pos;
cameraPosInSTR = cy::Vec3f(tmp.x, tmp.y, -tmp.z);
prog["cameraPosInSTR"] = cameraPosInSTR;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D, tfID);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
onImgui = io.WantCaptureMouse;
ImGui::Begin("Demo window");
if(ImGui::Button("blending"))
blendmode = !blendmode;
ImGui::SliderFloat("Isovalue", &isovalue, 0.0f, 1.0f);
ImGui::SliderFloat("stepsize", &stepsize, 0.0f, 300.0f);
ImGui::ColorEdit4("Cube color", background);
tfn_widget.draw_ui();
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
}
void clean()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
}
};
int main(int argc, char *argv[])
{
if (argc != 5)
{
std::cout << "missing commandline arg" << std::endl;
return -1;
}
App app("volume rendering", argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
app.run();
return 0;
}