-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
150 lines (116 loc) · 3.17 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
#include <cstddef> // std::size_t
#include "common.h"
#include GLUT_INCLUDE
#include "Gyroid.h"
#include "Sphere.h"
#include "Decimate.h"
GLuint preDecimate(const Isosurface& surface,
float xMin, float xMax,
float yMin, float yMax,
float zMin, float zMax,
float isolevel,
std::size_t resolution)
{
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
decimate(surface, xMin, xMax, yMin, yMax, zMin, zMax, isolevel, resolution);
glEndList();
return list;
}
static float rotX;
static float rotY;
static bool mouseInitialized = false;
static struct { int x, y; } mouse;
static GLuint list;
void render()
{
glClearColor(0, 0.3, 0.6, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, -1.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glPushMatrix();
glRotatef(rotY, 1, 0, 0);
glRotatef(rotX, 0, 1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(rotY, 1, 0, 0);
glRotatef(rotX, 0, 1, 0);
glScalef(0.1, 0.1, 0.1);
glCallList(list);
glPopMatrix();
glutSwapBuffers();
}
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45, ratio, 1, 1000);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set up the camera
gluLookAt(0, 0, -3, 0, 0, 0, 0, 1, 0);
}
void mouseDown(int button, int state, int x, int y)
{
mouse.x = x;
mouse.y = y;
mouseInitialized = true;
}
void mouseDragged(int x, int y)
{
if (mouseInitialized) {
float dx = x - mouse.x;
float dy = y - mouse.y;
rotX += dx / 10;
rotY -= dy / 10;
}
mouse.x = x;
mouse.y = y;
mouseInitialized = true;
glutPostRedisplay();
}
void init()
{
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//Sphere surface;
Gyroid surface;
list = preDecimate(surface, -5, 5, -5, 5, -5, 5, -1, 30);
}
int main (int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("Volume Rendering");
init();
glutDisplayFunc(render);
glutReshapeFunc(changeSize);
glutMouseFunc(mouseDown);
glutMotionFunc(mouseDragged);
glutMainLoop();
return 0;
}