Commit 6bf618a1b8c48f54c5c36e5e14cefe397c2b2880
0 parents
initial commit
Showing
3 changed files
with
230 additions
and
0 deletions
Show diff stats
1 | +++ a/CMakeLists.txt | |
1 | +#Specify the version being used aswell as the language | |
2 | +cmake_minimum_required(VERSION 2.8.11) | |
3 | + | |
4 | +#Name your project here | |
5 | +project(shview) | |
6 | + | |
7 | +#set the module directory | |
8 | +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}") | |
9 | + | |
10 | +#find the STIM library | |
11 | +find_package(STIM REQUIRED) | |
12 | + | |
13 | +find_package(OpenGL REQUIRED) | |
14 | +find_package(GLUT REQUIRED) | |
15 | + | |
16 | +#find the pthreads package | |
17 | +find_package(Threads) | |
18 | + | |
19 | +include_directories( | |
20 | + ${STIM_INCLUDE_DIRS} | |
21 | + ${GLUT_INCLUDE_DIR} | |
22 | + ${OpenGL_INCLUDE_DIRS} | |
23 | + ) | |
24 | + | |
25 | +#Assign source files to the appropriate variables | |
26 | +file(GLOB SRC_CPP "*.cpp") | |
27 | +file(GLOB SRC_H "*.h") | |
28 | + | |
29 | +#create an executable file | |
30 | +add_executable(shview | |
31 | + ${SRC_H} | |
32 | + ${SRC_CPP} | |
33 | + ) | |
34 | + | |
35 | +#set the link libraries | |
36 | +target_link_libraries(shview | |
37 | + ${CMAKE_THREAD_LIBS_INIT} | |
38 | + ${OPENGL_LIBRARIES} | |
39 | + ${GLUT_LIBRARY} | |
40 | + ) | |
41 | + | |
42 | + | ... | ... |
1 | +++ a/FindSTIM.cmake | |
1 | +include(FindPackageHandleStandardArgs) | |
2 | + | |
3 | +set(STIM_INCLUDE_DIR $ENV{STIMLIB_PATH}) | |
4 | + | |
5 | +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIR) | |
6 | + | |
7 | +if(STIM_FOUND) | |
8 | + set(STIM_INCLUDE_DIRS ${STIM_INCLUDE_DIR}) | |
9 | +endif() | |
0 | 10 | \ No newline at end of file | ... | ... |
1 | +++ a/main.cpp | |
1 | +#include <iostream> | |
2 | + | |
3 | +#include <GL/glut.h> | |
4 | + | |
5 | +#include <stim/visualization/camera.h> | |
6 | +#include <stim/parser/arguments.h> | |
7 | +#include <stim/visualization/sph_harmonics.h> | |
8 | + | |
9 | +#define theta_scale 0.01 | |
10 | +#define phi_scale 0.01 | |
11 | + | |
12 | +//create a global camera that will specify the viewport | |
13 | +stim::camera cam; | |
14 | +int mx, my; //mouse coordinates in the window space | |
15 | + | |
16 | +stim::sph_harmonics S; | |
17 | + | |
18 | +float d = 1.5; //initial distance between the camera and the sphere | |
19 | + | |
20 | +bool rotate_zoom = true; //sets the current camera mode (rotation = true, zoom = false) | |
21 | + | |
22 | +stim::arglist args; //class for processing command line arguments | |
23 | + | |
24 | +#ifdef _WIN32 | |
25 | + args.set_ansi(false); | |
26 | +#endif | |
27 | + | |
28 | +bool init(){ | |
29 | + | |
30 | + //set the clear color to white | |
31 | + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | |
32 | + | |
33 | + //initialize the camera | |
34 | + cam.setPosition(d, d, d); | |
35 | + cam.LookAt(0, 0, 0, 0, 1, 1); | |
36 | + cam.setFOV(40); | |
37 | + | |
38 | + //initialize the texture map stuff | |
39 | + | |
40 | + srand(time(NULL)); | |
41 | + unsigned int T = 40; | |
42 | + double c; | |
43 | + for(unsigned int t = 0; t < T; t++){ | |
44 | + | |
45 | + c = (double)rand() / RAND_MAX - 0.5; | |
46 | + S.push(c); | |
47 | + } | |
48 | + | |
49 | + S.glInit(256); | |
50 | + | |
51 | + return true; | |
52 | +} | |
53 | + | |
54 | +//code that is run every time the user changes something | |
55 | +void display(){ | |
56 | + //clear the screen | |
57 | + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | |
58 | + | |
59 | + //set the projection matrix | |
60 | + glMatrixMode(GL_PROJECTION); //put the projection matrix on the stack | |
61 | + glLoadIdentity(); //set it to the identity matrix | |
62 | + gluPerspective(cam.getFOV(), 1, 0.001, 1000000); //set up a perspective projection | |
63 | + | |
64 | + | |
65 | + //set the model view matrix | |
66 | + glMatrixMode(GL_MODELVIEW); //load the model view matrix to the stack | |
67 | + glLoadIdentity(); //set it to the identity matrix | |
68 | + | |
69 | + //get the camera parameters | |
70 | + stim::vec<float, 3> p = cam.getPosition(); | |
71 | + stim::vec<float, 3> u = cam.getUp(); | |
72 | + stim::vec<float, 3> d = cam.getDirection(); | |
73 | + | |
74 | + //specify the camera parameters to OpenGL | |
75 | + gluLookAt(p[0], p[1], p[2], d[0], d[1], d[2], u[0], u[1], u[2]); | |
76 | + | |
77 | + //draw the sphere | |
78 | + S.glRender(); | |
79 | + | |
80 | + //flush commands on the GPU | |
81 | + glutSwapBuffers(); | |
82 | +} | |
83 | + | |
84 | +void mouse_press(int button, int state, int x, int y){ | |
85 | + | |
86 | + //set the camera motion mode based on the mouse button pressed | |
87 | + if(button == GLUT_LEFT_BUTTON) | |
88 | + rotate_zoom = true; | |
89 | + else if(button == GLUT_MIDDLE_BUTTON) | |
90 | + rotate_zoom = false; | |
91 | + | |
92 | + //if the mouse is pressed | |
93 | + if(state == GLUT_DOWN){ | |
94 | + //set the current mouse position | |
95 | + mx = x; my = y; | |
96 | + } | |
97 | +} | |
98 | + | |
99 | +void mouse_drag(int x, int y){ | |
100 | + | |
101 | + //if the camera is in rotation mode, rotate | |
102 | + if(rotate_zoom == true){ | |
103 | + float theta = theta_scale * (mx - x); | |
104 | + float phi = -phi_scale * (my - y); | |
105 | + | |
106 | + //if the mouse is dragged | |
107 | + cam.OrbitFocus(theta, phi); | |
108 | + } | |
109 | + //otherwize zoom | |
110 | + else{ | |
111 | + cam.Push(my - y); | |
112 | + } | |
113 | + | |
114 | + //update the mouse position | |
115 | + mx = x; my = y; | |
116 | + | |
117 | + glutPostRedisplay(); | |
118 | +} | |
119 | + | |
120 | +void process_arguments(int argc, char* argv[]){ | |
121 | + | |
122 | + args.add("help", "prints this help"); | |
123 | + | |
124 | + //process the command line arguments | |
125 | + args.parse(argc, argv); | |
126 | + | |
127 | + | |
128 | + //push all of the arguments to the spherical harmonics class as coefficients | |
129 | + for(unsigned int a = 0; a < args.nargs(); a++) | |
130 | + S.push(atof(args.arg(a).c_str())); | |
131 | + | |
132 | + //if the user asks for help, give it and exit | |
133 | + if(args["help"].is_set()){ | |
134 | + std::cout<<"usage: shview c0 c1 c2 c3 ... --option [A B C]"<<std::endl; | |
135 | + std::cout<<"examples:"<<std::endl; | |
136 | + std::cout<<" shview 1 2 3 4"<<std::endl; | |
137 | + std::cout<<args.str(); | |
138 | + exit(0); | |
139 | + } | |
140 | +} | |
141 | + | |
142 | +int main(int argc, char *argv[]){ | |
143 | + | |
144 | + //initialize GLUT | |
145 | + glutInit(&argc, argv); | |
146 | + | |
147 | + //process arguments | |
148 | + process_arguments(argc, argv); | |
149 | + | |
150 | + //set the size of the GLUT window | |
151 | + glutInitWindowSize(500, 500); | |
152 | + | |
153 | + glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE); | |
154 | + | |
155 | + //create the GLUT window (and an OpenGL context) | |
156 | + glutCreateWindow("Spherical Harmonic Viewport"); | |
157 | + | |
158 | + //set the display function (which will be called repeatedly by glutMainLoop) | |
159 | + glutDisplayFunc(display); | |
160 | + | |
161 | + //set the mouse press function (called when a mouse button is pressed) | |
162 | + glutMouseFunc(mouse_press); | |
163 | + //set the mouse motion function (which will be called any time the mouse is dragged) | |
164 | + glutMotionFunc(mouse_drag); | |
165 | + | |
166 | + //run the initialization function | |
167 | + if(!init()) | |
168 | + return 1; //return an error if it fails | |
169 | + | |
170 | + | |
171 | + //enter the main loop | |
172 | + glutMainLoop(); | |
173 | + | |
174 | + //return 0 if everything is awesome | |
175 | + return 0; | |
176 | + | |
177 | + | |
178 | + | |
179 | +} | ... | ... |