Commit 4d80c1dd5184bd4468684903ae0c66ce77472f67
1 parent
59071526
added a GLUT template header file that can be added to GLUT projects to create a…
… standard visualization environment
Showing
2 changed files
with
103 additions
and
1 deletions
Show diff stats
stim/image/image.h
1 | #ifndef STIM_IMAGE_H | 1 | #ifndef STIM_IMAGE_H |
2 | #define STIM_IMAGE_H | 2 | #define STIM_IMAGE_H |
3 | 3 | ||
4 | +#ifdef _WIN32 | ||
5 | +#undef max | ||
6 | +#endif | ||
7 | + | ||
4 | #ifdef USING_OPENCV | 8 | #ifdef USING_OPENCV |
5 | //#include <opencv2/core/core.hpp> | 9 | //#include <opencv2/core/core.hpp> |
6 | //#include <opencv2/highgui/highgui.hpp> | 10 | //#include <opencv2/highgui/highgui.hpp> |
@@ -11,7 +15,6 @@ | @@ -11,7 +15,6 @@ | ||
11 | #include <vector> | 15 | #include <vector> |
12 | #include <iostream> | 16 | #include <iostream> |
13 | #include <climits> //use limits and remove the MIN and MAX macros | 17 | #include <climits> //use limits and remove the MIN and MAX macros |
14 | -#define NOMINMAX | ||
15 | #include <typeinfo> | 18 | #include <typeinfo> |
16 | #include <fstream> | 19 | #include <fstream> |
17 | #include <cstring> | 20 | #include <cstring> |
1 | +/* Don't change this file. Make a copy in your project and change parameters as necessary. */ | ||
2 | + | ||
3 | +#ifndef GLUT_TEMPLATE_H | ||
4 | +#define GLUT_TEMPLATE_H | ||
5 | + | ||
6 | +#include <stim/visualization/camera.h> | ||
7 | +#include <GL/glut.h> | ||
8 | + | ||
9 | +struct glut_template_struct { | ||
10 | + float theta_scale = 0.01f; | ||
11 | + float phi_scale = 0.01f; | ||
12 | + float zoom_scale = 0.1f; | ||
13 | + stim::camera cam; //create a camera object | ||
14 | + int mx, my; //mouse coordinates in the window space | ||
15 | + float d = 1.5; //initial distance between the camera and the sphere | ||
16 | + bool rotate_zoom = true; //sets the current camera mode (rotation = true, zoom = false) | ||
17 | + | ||
18 | + bool axis = false; //render the z-axis (set via a command line flag) | ||
19 | +} gt; | ||
20 | + | ||
21 | +//render the XYZ axes | ||
22 | +void render_axes() { | ||
23 | + glDisable(GL_TEXTURE_2D); //turn off texture mapping | ||
24 | + glBegin(GL_LINES); | ||
25 | + glColor3f(1.0f, 0.0f, 0.0f); //set the color to RED and render X | ||
26 | + glVertex3f(0.0, 0.0, 0.0); | ||
27 | + glVertex3f(100.0, 0.0, 0.0); | ||
28 | + | ||
29 | + glColor3f(0.0f, 1.0f, 0.0f); //set the color to RED and render X | ||
30 | + glVertex3f(0.0, 0.0, 0.0); | ||
31 | + glVertex3f(0.0, 100.0, 0.0); | ||
32 | + | ||
33 | + glColor3f(0.0f, 0.0f, 1.0f); //set the color to RED and render X | ||
34 | + glVertex3f(0.0, 0.0, 0.0); | ||
35 | + glVertex3f(0.0, 0.0, 100.0); | ||
36 | + glEnd(); | ||
37 | +} | ||
38 | + | ||
39 | +//display function executed to update every frame | ||
40 | +void glut_display() { | ||
41 | + | ||
42 | + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //clear the screen | ||
43 | + | ||
44 | + glMatrixMode(GL_PROJECTION); //put the projection matrix on the stack | ||
45 | + glLoadIdentity(); //set it to the identity matrix | ||
46 | + gluPerspective(gt.cam.getFOV(), 1, 0.001, 1000000); //set up a perspective projection | ||
47 | + glMatrixMode(GL_MODELVIEW); //load the model view matrix to the stack | ||
48 | + glLoadIdentity(); //set it to the identity matrix | ||
49 | + stim::vec3<float> p = gt.cam.getPosition(); //get the camera parameters | ||
50 | + stim::vec3<float> u = gt.cam.getUp(); | ||
51 | + stim::vec3<float> d = gt.cam.getDirection(); | ||
52 | + gluLookAt(p[0], p[1], p[2], d[0], d[1], d[2], u[0], u[1], u[2]); //specify the camera parameters to OpenGL | ||
53 | + | ||
54 | + | ||
55 | + render_axes(); //render the axes if the user requests them | ||
56 | + glutSwapBuffers(); //swap in the back buffer (double-buffering is used to prevent tearing) | ||
57 | +} | ||
58 | + | ||
59 | +//process mouse press events | ||
60 | +void glut_mouse_press(int button, int state, int x, int y) { | ||
61 | + if (button == GLUT_LEFT_BUTTON) //set the camera motion mode based on the mouse button pressed | ||
62 | + gt.rotate_zoom = true; | ||
63 | + else if (button == GLUT_RIGHT_BUTTON) | ||
64 | + gt.rotate_zoom = false; | ||
65 | + if (state == GLUT_DOWN) { //if the mouse is pressed | ||
66 | + gt.mx = x; gt.my = y; //set the current mouse position | ||
67 | + } | ||
68 | +} | ||
69 | + | ||
70 | +//process mouse drags to update the camera | ||
71 | +void glut_mouse_drag(int x, int y) { | ||
72 | + if (gt.rotate_zoom == true) { //if the camera is in rotation mode, rotate | ||
73 | + float theta = gt.theta_scale * (gt.mx - x); | ||
74 | + float phi = -gt.phi_scale * (gt.my - y); | ||
75 | + gt.cam.OrbitFocus(theta, phi); //if the mouse is dragged | ||
76 | + } | ||
77 | + else { //otherwize zoom | ||
78 | + gt.cam.Push(gt.zoom_scale*(gt.my - y)); | ||
79 | + } | ||
80 | + gt.mx = x; gt.my = y; //update the mouse position | ||
81 | + glutPostRedisplay(); | ||
82 | +} | ||
83 | + | ||
84 | +void glut_init(int argc, char* argv[]) { | ||
85 | + glutInit(&argc, argv); //initialize GLUT | ||
86 | + glutInitWindowSize(500, 500); //set the size of the GLUT window | ||
87 | + glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE); | ||
88 | + glutCreateWindow("3D Tensor Visualization"); //create the GLUT window (and an OpenGL context) | ||
89 | + glutDisplayFunc(glut_display); //set the display function (which will be called repeatedly by glutMainLoop) | ||
90 | + glutMouseFunc(glut_mouse_press); //set the mouse press function (called when a mouse button is pressed) | ||
91 | + glutMotionFunc(glut_mouse_drag); //set the mouse motion function (which will be called any time the mouse is dragged) | ||
92 | + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //set the clear color to white | ||
93 | + gt.cam.setPosition(gt.d, gt.d, gt.d); //initialize the camera | ||
94 | + gt.cam.LookAt(0, 0, 0, 0, 1, 1); | ||
95 | + gt.cam.setFOV(40); | ||
96 | + glutMainLoop(); //enter the main loop | ||
97 | +} | ||
98 | + | ||
99 | +#endif | ||
0 | \ No newline at end of file | 100 | \ No newline at end of file |