Commit cfbc922a0a3c5022af99c64482c4b7aca3c2393e

Authored by Pavel Govyadinov
1 parent 80bc3149

Added support for rotating cube as well as a square that scrolls through all of …

…the slices using the arrow keys.
Showing 2 changed files with 112 additions and 31 deletions   Show diff stats
TextureTest.cu
... ... @@ -24,9 +24,11 @@
24 24 {1.0, 1.0, -1.0}, {1.0, 1.0, 1.0}}; */
25 25 unsigned long tick = 0;
26 26 static bool button1 = FALSE;
27   - //static GLfloat viewDistance = 9.0;
28   - //static float pos = 1.0;
29   -
  27 + //static GLfloat viewDistance = 1.0;
  28 + static float pos = 0.8;
  29 + static GLfloat ori = 0.0;
  30 + static GLfloat adjust = 1.0/426.0;
  31 +//init for a cube
30 32 void
31 33 glInit(int w, int h)
32 34 {
... ... @@ -36,17 +38,89 @@ glInit(int w, int h)
36 38 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
37 39 glClearColor(0.0, 0.0, 0.0, 0.0);
38 40 glColor4d(0.0, 0.0, 0.0, 1.0);
39   - //glPointSize(3.0);
40 41 glShadeModel(GL_FLAT);
41 42  
42 43 glViewport(0, 0, (GLsizei) 1024, (GLsizei) 1024);
43 44 glMatrixMode(GL_PROJECTION);
44 45 glLoadIdentity();
45   - gluPerspective(180, 16.0/9.0, 5.0, 100.0);
  46 + gluPerspective(90, 4.0/3.0, 0.1, 100.0);
46 47 glMatrixMode(GL_MODELVIEW);
47 48  
48 49 CHECK_OPENGL_ERROR
49 50 }
  51 +//init for a square.
  52 +void
  53 +glInitSquare(int w, int h)
  54 +{
  55 + glEnable(GL_TEXTURE_3D);
  56 + glEnable(GL_DEPTH_TEST);
  57 + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  58 + glClearColor(0.0, 0.0, 0.0, 0.0);
  59 + glColor4d(0.0, 0.0, 0.0, 1.0);
  60 + //glPointSize(3.0);
  61 + glShadeModel(GL_FLAT);
  62 +
  63 + glViewport(0, 0, (GLsizei) 1024, (GLsizei) 1024);
  64 + glMatrixMode(GL_PROJECTION);
  65 + glLoadIdentity();
  66 + gluPerspective(90, 4.0/3.0, 0.1, 100.0);
  67 + glMatrixMode(GL_MODELVIEW);
  68 +
  69 +}
  70 +//render square
  71 +void
  72 +renderSceneSquare()
  73 +{
  74 + glEnable(GL_TEXTURE_3D);
  75 + glBindTexture(GL_TEXTURE_3D, texID);
  76 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77 + glMatrixMode(GL_MODELVIEW);
  78 + glLoadIdentity();
  79 + glTranslatef(-0.5, -0.5, -pos);
  80 + glBegin(GL_QUADS);
  81 + glTexCoord3f(vertex[faces[4][0]][0],
  82 + vertex[faces[4][0]][1],
  83 + ori);
  84 +
  85 + glVertex3f(vertex[faces[4][0]][0],
  86 + vertex[faces[4][0]][1],
  87 + vertex[faces[4][0]][2]);
  88 +
  89 + //second vertex
  90 + glTexCoord3f(vertex[faces[4][1]][0],
  91 + vertex[faces[4][1]][1],
  92 + ori);
  93 +
  94 + glVertex3f(vertex[faces[4][1]][0],
  95 + vertex[faces[4][1]][1],
  96 + vertex[faces[4][1]][2]);
  97 +
  98 + //third vertex
  99 + glTexCoord3f(vertex[faces[4][2]][0],
  100 + vertex[faces[4][2]][1],
  101 + ori);
  102 +
  103 + glVertex3f(vertex[faces[4][2]][0],
  104 + vertex[faces[4][2]][1],
  105 + vertex[faces[4][2]][2]);
  106 +
  107 + //fourth vertex
  108 + glTexCoord3f(vertex[faces[4][3]][0],
  109 + vertex[faces[4][3]][1],
  110 + ori);
  111 +
  112 + glVertex3f(vertex[faces[4][3]][0],
  113 + vertex[faces[4][3]][1],
  114 + vertex[faces[4][3]][2]);
  115 + glEnd();
  116 +
  117 + CHECK_OPENGL_ERROR
  118 + glutSwapBuffers();
  119 +}
  120 +
  121 +
  122 +
  123 +//render cube
50 124 void
51 125 renderScene()
52 126 {
... ... @@ -54,9 +128,10 @@ renderScene()
54 128 glBindTexture(GL_TEXTURE_3D, texID);
55 129 CHECK_OPENGL_ERROR
56 130 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
57   - glMatrixMode(GL_PROJECTION);
  131 + glMatrixMode(GL_MODELVIEW);
58 132 glLoadIdentity();
59   - glTranslatef(-0.4, -0.4, 0.4);
  133 + glTranslatef(-0.5, -0.5, -pos);
  134 + //glTranslatef(0.0, 0.0, -1.0);
60 135 //gluLookAt(0,0, -viewDistance, 0, 0, -1, 0, 1, 0);
61 136 //gluLookAt(0,0, 0, 0, 0, 0, 0, 0, 0);
62 137 glRotatef(-90, 1.0, 0.0, 0.0);
... ... @@ -117,21 +192,21 @@ renderScene()
117 192 //glutPostRedisplay();
118 193 }
119 194  
  195 +
120 196 void MouseButton(int button, int state, int x, int y)
121 197 {
122 198 if (button == GLUT_LEFT_BUTTON)
123 199 {
124 200 button1 = (state == GLUT_DOWN) ? TRUE : FALSE;
125   - //tick = y;
  201 + //prev_pos = y;
126 202 }
127 203 }
128 204  
129 205 void MouseMotion(int x, int y)
130 206 {
131 207 if(button1)
132   - {
133   - tick = y*10.0;
134   - // pos = x*10.0;
  208 + {
  209 + pos = (float)y/100.0;
135 210 glutPostRedisplay();
136 211 }
137 212 }
... ... @@ -143,6 +218,24 @@ idleFunction()
143 218 glutPostRedisplay();
144 219 }
145 220  
  221 +void
  222 +processSpecialKeys(int key, int xx, int yy)
  223 +{
  224 +switch(key) {
  225 + case GLUT_KEY_UP:
  226 + ori = ori+adjust;
  227 + if (ori > 1.0){
  228 + ori = 0.0;
  229 + }
  230 + break;
  231 + case GLUT_KEY_DOWN:
  232 + ori = ori-adjust;
  233 + if (ori < 0.0){
  234 + ori = 0.0;
  235 + }
  236 + break;
  237 + }
  238 +}
146 239  
147 240 void
148 241 processKeys(unsigned char key, int x, int y)
... ... @@ -155,19 +248,11 @@ processKeys(unsigned char key, int x, int y)
155 248 void
156 249 changeSize(int w, int h)
157 250 {
158   - //if (h==0)
159   - // h=1;
160   - //float ratio = w*1.0/h;
161 251 CHECK_OPENGL_ERROR
162 252 glViewport(0,0,w,h);
163 253 glMatrixMode(GL_PROJECTION);
164   -
165 254 glLoadIdentity();
166   -
167   -
168   - //gluPerspective(90.0, 4.0/3.0, 0.1, 10.0);
169 255 gluPerspective(90, 4.0/3.0, 0.1, 100.0);
170   -
171 256 glMatrixMode(GL_MODELVIEW);
172 257 CHECK_OPENGL_ERROR
173 258 }
... ... @@ -177,25 +262,25 @@ int
177 262 main(int argc, char **argv)
178 263 {
179 264 CHECK_OPENGL_ERROR
180   -
181   -
182   - CHECK_OPENGL_ERROR
183 265 glutInit(&argc, argv);
184 266 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
185 267 glutInitWindowSize(1024, 1024);
186 268 glutCreateWindow("gl_texture");
187 269 std::cout << " Initialization is complete in Glut" << std::endl;
188   -
  270 +
189 271 CHECK_OPENGL_ERROR
190 272 glutKeyboardFunc(processKeys);
191 273 CHECK_OPENGL_ERROR
  274 + glutSpecialFunc(processSpecialKeys);
  275 + CHECK_OPENGL_ERROR
192 276 glutReshapeFunc(changeSize);
193 277 CHECK_OPENGL_ERROR
194   - glutDisplayFunc(renderScene);
  278 + glutDisplayFunc(renderSceneSquare);
195 279 CHECK_OPENGL_ERROR
196 280 glutMouseFunc(MouseButton);
197   -
  281 + CHECK_OPENGL_ERROR
198 282 glutMotionFunc(MouseMotion);
  283 +
199 284 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
200 285 glPixelStorei(GL_PACK_ALIGNMENT,1);
201 286 std::cerr << " Keyboard and Reshape is complete in Glut" << std::endl;
... ... @@ -203,14 +288,10 @@ main(int argc, char **argv)
203 288 stim::gl_texture<unsigned char> stack ("/home/pavel/Documents/Test_Data/");
204 289 stack.createTexture();
205 290 texID = stack.getTexture();
206   - //stack.save_images("test\\????.jpg");
207   - //stim::vec<unsigned long, 4> dims = stack.getDims();
208   - //unsigned char* dat = stack.getData();
209   - //createTexture(dims, dat);
210 291 std::cout << ("I have started an instance of the class") << std::endl;
211 292  
212 293  
213   - glInit(6, 6);
  294 + glInitSquare(6, 6);
214 295 CHECK_OPENGL_ERROR
215 296 glutIdleFunc(idleFunction);
216 297 CHECK_OPENGL_ERROR
... ...
1   -Subproject commit 63fcb5c8a420daa9261623df18565b18ddbdbe61
  1 +Subproject commit 846040bee9413a1b7f5fbd004bc6504503542c8f
... ...