1235 | | {\scriptsize\begin{verbatim} |
1236 | | subroutine redraw |
1237 | | implicit none |
1238 | | include 'GL/fgl.h' |
1239 | | common /vertices/ n, vertex(2, 100) |
1240 | | integer n, i |
1241 | | real vertex |
1242 | | call fglClear(GL_COLOR_BUFFER_BIT) |
1243 | | call fglbegin(GL_LINE_STRIP) |
1244 | | do i = 1,n |
1245 | | call fglVertex2f(vertex(1, i), vertex(2, i)) |
1246 | | end do |
1247 | | call fglend |
1248 | | call fglFlush |
1249 | | end |
1250 | | |
1251 | | subroutine mouse (button, state, x, y) |
1252 | | implicit none |
1253 | | include 'GL/fglut.h' |
1254 | | include 'GL/fgl.h' |
1255 | | include 'GL/fglu.h' |
1256 | | common /vertices/ n, vertex(2, 100) |
1257 | | integer n, i |
1258 | | real vertex |
1259 | | integer button, state, x, y |
1260 | | integer viewport(4) |
1261 | | real*8 mvmatrix(16), projmatrix(16) |
1262 | | real*8 wx, wy, wz ! returned world x, y, z coords |
1263 | | real*8 px, py, pz ! picked window coortinates |
1264 | | integer status |
1265 | | |
1266 | | if (button .eq. GLUT_LEFT_BUTTON) then |
1267 | | if (state .eq. GLUT_DOWN) then |
1268 | | call fglGetIntegerv (GL_VIEWPORT, viewport) |
1269 | | call fglGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix) |
1270 | | call fglGetDoublev (GL_PROJECTION_MATRIX, projmatrix) |
1271 | | note viewport(4) is height of window in pixels |
1272 | | px = x |
1273 | | py = viewport(4) - y - 1 |
1274 | | pz = 0.0 |
1275 | | print *, ' Coordinates at cursor are ', px, py |
1276 | | status = fgluUnProject (px, py, pz, mvmatrix, |
1277 | | projmatrix, viewport, wx, wy, wz) |
1278 | | print *, 'World coords at z=0.0 are ', wx, wy, wz |
1279 | | n = n + 1 |
1280 | | vertex(1, n) = wx |
1281 | | vertex(2, n) = wy |
1282 | | call fglutPostRedisplay |
1283 | | end if |
1284 | | end if |
1285 | | end |
1286 | | |
1287 | | |
1288 | | program main |
1289 | | external redraw |
1290 | | external mouse |
1291 | | include 'GL/fglut.h' |
1292 | | call fglutinit |
1293 | | call fglutinitdisplaymode(ior(GLUT_SINGLE,GLUT_RGB)) |
1294 | | call fglutInitWindowSize (500, 500) |
1295 | | call fglutInitWindowPosition (100, 100) |
1296 | | window = fglutcreatewindow('Click in window') |
1297 | | call fglutdisplayfunc(redraw) |
1298 | | call fglutMouseFunc(mouse) |
1299 | | call fglutmainloop |
1300 | | end |
1301 | | \end{verbatim} |
1302 | | } |
1303 | | |
1304 | | |
1305 | | |
1306 | | \subsection{Kvaternionska rotacija} |
| 1260 | |
| 1261 | {{{ |
| 1262 | #!c |
| 1263 | #include <stdio.h> |
| 1264 | #include <stdlib.h> |
| 1265 | #include <GL/glut.h> |
| 1266 | |
| 1267 | #define MAXN 100 |
| 1268 | |
| 1269 | GLint n; |
| 1270 | GLfloat *vertex; |
| 1271 | |
| 1272 | void redraw() |
| 1273 | { |
| 1274 | int i; |
| 1275 | glClear(GL_COLOR_BUFFER_BIT); |
| 1276 | glPushMatrix(); |
| 1277 | glColor3f(1,1,1); |
| 1278 | glBegin(GL_LINE_STRIP); |
| 1279 | for (i = 0; i < n; i++) |
| 1280 | { |
| 1281 | glVertex2fv(&vertex[i*2]); |
| 1282 | } |
| 1283 | glEnd(); |
| 1284 | |
| 1285 | for (i = 0; i < n; i++) //Adding spheres on world coords |
| 1286 | { |
| 1287 | glColor3f(1,0,0); |
| 1288 | glPushMatrix(); |
| 1289 | glTranslatef(vertex[i*2],vertex[i*2+1],0); |
| 1290 | glutSolidSphere(0.01,8,8); |
| 1291 | glPopMatrix(); |
| 1292 | } |
| 1293 | glPopMatrix(); |
| 1294 | glutSwapBuffers(); |
| 1295 | } |
| 1296 | |
| 1297 | void mouse(int button, int state, int x, int y) |
| 1298 | { |
| 1299 | GLint viewport[4]; |
| 1300 | GLdouble mvmatrix[16], projmatrix[16]; |
| 1301 | GLdouble wx, wy, wz; //Returned world x, y, z coords |
| 1302 | GLdouble px, py, pz; //Picked window coordinates |
| 1303 | int status; |
| 1304 | if (button == GLUT_LEFT_BUTTON ) |
| 1305 | { |
| 1306 | if (state == GLUT_DOWN) |
| 1307 | { |
| 1308 | glGetIntegerv (GL_VIEWPORT, viewport); |
| 1309 | glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix); |
| 1310 | glGetDoublev (GL_PROJECTION_MATRIX, projmatrix); |
| 1311 | //Note viewport[4] is height in pixels |
| 1312 | px = x; |
| 1313 | py = viewport[3] - y - 1; |
| 1314 | pz = 0.0; |
| 1315 | fprintf (stderr, "Coordinates at cursor are %f, %f\n", px, py); |
| 1316 | status = gluUnProject (px, py, pz, mvmatrix, projmatrix, viewport, &wx, &wy, &wz); |
| 1317 | fprintf(stderr, "World coords at z=0.0 are %f, %f, %f\n", wx, wy, wz); |
| 1318 | if (n < MAXN) |
| 1319 | { |
| 1320 | vertex[n*2] = wx; |
| 1321 | vertex[n*2+1] = wy; |
| 1322 | n=n+1; |
| 1323 | } |
| 1324 | else |
| 1325 | { |
| 1326 | fprintf(stderr, "Maximum number of points was received!\n"); |
| 1327 | } |
| 1328 | glutPostRedisplay(); |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | int main(int argc, char *argv[]) |
| 1335 | { |
| 1336 | vertex = (GLfloat *) malloc(2 * MAXN * sizeof (GLfloat)); |
| 1337 | glutInit(&argc, argv); |
| 1338 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); |
| 1339 | glutInitWindowSize (700, 700); |
| 1340 | glutInitWindowPosition (0, 0); |
| 1341 | glutCreateWindow("Click in window"); |
| 1342 | glutDisplayFunc(redraw); |
| 1343 | glutMouseFunc(mouse); |
| 1344 | glutMainLoop(); |
| 1345 | return 0; |
| 1346 | } |
| 1347 | }}} |
| 1348 | |
| 1349 | [[BR]] |
| 1350 | |
| 1351 | == Kvaternionska rotacija == |
| 1352 | |
1311 | | začetni pritisk na gumb (podprogram \emph{mouse}) kot vse |
1312 | | naslednje pomike miške (podprogram \emph{motion}). |
1313 | | |
1314 | | |
1315 | | {\scriptsize\begin{verbatim} |
1316 | | subroutine display |
1317 | | implicit none |
1318 | | include 'GL/fgl.h' |
1319 | | common /quaternion/ last(4), cur(4) |
1320 | | real last, cur, m(4,4) |
1321 | | call build_rotmatrix(m, cur) |
1322 | | call fglLoadIdentity |
1323 | | call fglMultMatrixf(m) |
1324 | | call fglclear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT) |
1325 | | call fglutSolidTeapot(dble(0.5)) |
1326 | | call fglutSwapBuffers |
1327 | | end |
1328 | | |
1329 | | subroutine motion (x, y) |
1330 | | include 'GL/fglut.h' |
1331 | | include 'GL/fgl.h' |
1332 | | implicit none |
1333 | | integer x, y |
1334 | | common /quaternion/ last(4), cur(4) |
1335 | | common /mousestart/ beginx, beginy |
1336 | | common /viewport/ width, height |
1337 | | integer width, height |
1338 | | integer beginx, beginy |
1339 | | real last, cur |
1340 | | real p1x, p1y, p2x, p2y |
1341 | | p1x = (2.0*beginx - width)/width |
1342 | | p1y = (height - 2.0*beginy)/height |
1343 | | p2x = (2.0 * x - width) / width |
1344 | | p2y = (height - 2.0 * y) / height |
1345 | | call trackball(last,p1x, p1y, p2x, p2y) |
1346 | | call add_quats(last, cur, cur) |
1347 | | beginx = x |
1348 | | beginy = y |
1349 | | call fglutPostRedisplay |
1350 | | end |
1351 | | |
1352 | | subroutine mouse (button, state, x, y) |
1353 | | implicit none |
1354 | | integer button, state, x, y |
1355 | | include 'GL/fglut.h' |
1356 | | include 'GL/fgl.h' |
1357 | | include 'GL/fglu.h' |
1358 | | common /mousestart/ beginx, beginy |
1359 | | integer beginx, beginy |
1360 | | beginx = x |
1361 | | beginy = y |
1362 | | end |
1363 | | |
1364 | | subroutine reshape(w, h) |
1365 | | include 'GL/fgl.h' |
1366 | | integer w, h |
1367 | | real*8 l |
1368 | | common /viewport/ width, height |
1369 | | integer width, height |
1370 | | width=w |
1371 | | height=h |
1372 | | l = 1 |
1373 | | call fglViewPort(0, 0, w, h) |
1374 | | call fglMatrixMode(GL_PROJECTION) |
1375 | | call fglLoadIdentity |
1376 | | call fglOrtho(-l,l,-l,l,-l,l) |
1377 | | call fglMatrixMode(GL_MODELVIEW) |
1378 | | call fglLoadIdentity |
1379 | | end |
1380 | | |
1381 | | program trackballdemo |
1382 | | implicit none |
1383 | | include 'GL/fglut.h' |
1384 | | include 'GL/fgl.h' |
1385 | | include 'GL/fglu.h' |
1386 | | external display |
1387 | | external motion |
1388 | | external mouse |
1389 | | external reshape |
1390 | | integer window |
1391 | | common /quaternion/ last(4), cur(4) |
1392 | | real last, cur |
1393 | | call trackball(cur, 0.0, 0.0, 0.0, 0.0) |
1394 | | call fglutinit |
1395 | | call fglutinitdisplaymode(GLUT_DOUBLE+GLUT_RGB+GLUT_DEPTH) |
1396 | | window = fglutcreatewindow('Use mouse to rotate') |
1397 | | call fglutdisplayfunc(display) |
1398 | | call fglutmousefunc(mouse) |
1399 | | call fglutmotionfunc(motion) |
1400 | | call fglutreshapefunc(reshape) |
1401 | | call fglEnable(GL_LIGHTING) |
1402 | | call fglEnable(GL_LIGHT0) |
1403 | | call fglEnable(GL_DEPTH_TEST) |
1404 | | call fglutmainloop |
1405 | | end |
1406 | | \end{verbatim} |
1407 | | } |
| 1357 | začetni pritisk na gumb (podprogram ''mouse'') kot vse |
| 1358 | naslednje pomike miške (podprogram ''motion''). |
| 1359 | |
| 1360 | {{{ |
| 1361 | #!c |
| 1362 | #include <GL/glut.h> |
| 1363 | #include "trackball.h" |
| 1364 | #include "trackball.c" |
| 1365 | |
| 1366 | GLfloat m[4][4]; |
| 1367 | float last[4]; |
| 1368 | float cur[4]; |
| 1369 | int width; |
| 1370 | int height; |
| 1371 | int beginx; |
| 1372 | int beginy; |
| 1373 | float p1x; |
| 1374 | float p1y; |
| 1375 | float p2x; |
| 1376 | float p2y; |
| 1377 | |
| 1378 | void display() |
| 1379 | { |
| 1380 | glPushMatrix(); |
| 1381 | build_rotmatrix(m, cur); |
| 1382 | glLoadIdentity(); |
| 1383 | glMultMatrixf(*m); |
| 1384 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
| 1385 | glutSolidTeapot(0.5); |
| 1386 | glPopMatrix(); |
| 1387 | glutSwapBuffers(); |
| 1388 | } |
| 1389 | |
| 1390 | void mouse(int button,int state, int x, int y) |
| 1391 | { |
| 1392 | beginx = x; |
| 1393 | beginy = y; |
| 1394 | } |
| 1395 | |
| 1396 | void motion(int x,int y) |
| 1397 | { |
| 1398 | p1x = (2.0*beginx - width)/width; |
| 1399 | p1y = (height - 2.0*beginy)/height; |
| 1400 | p2x = (2.0 * x - width) / width; |
| 1401 | p2y = (height - 2.0 * y) / height; |
| 1402 | trackball(last,p1x, p1y, p2x, p2y); |
| 1403 | add_quats(last, cur, cur); |
| 1404 | beginx = x; |
| 1405 | beginy = y; |
| 1406 | glutPostRedisplay(); |
| 1407 | } |
| 1408 | |
| 1409 | void reshape (int w, int h) |
| 1410 | { |
| 1411 | width=w; |
| 1412 | height=h; |
| 1413 | double l; |
| 1414 | l = 1; |
| 1415 | glViewport (0, 0, w, h); |
| 1416 | glMatrixMode (GL_PROJECTION); |
| 1417 | glLoadIdentity(); |
| 1418 | glOrtho(-l, l, -l, l, -l, l); |
| 1419 | glMatrixMode(GL_MODELVIEW); |
| 1420 | glLoadIdentity(); |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | int main(int argc, char *argv[]) |
| 1425 | { |
| 1426 | glutInit(&argc, argv); |
| 1427 | trackball(cur, 0.0, 0.0, 0.0, 0.0); |
| 1428 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
| 1429 | glutCreateWindow("Rotacija z misko"); |
| 1430 | glutDisplayFunc(display); |
| 1431 | glutMouseFunc(mouse); |
| 1432 | glutMotionFunc(motion); |
| 1433 | glutReshapeFunc(reshape); |
| 1434 | glEnable(GL_LIGHTING); |
| 1435 | glEnable(GL_LIGHT0); |
| 1436 | glEnable(GL_DEPTH_TEST); |
| 1437 | glutMainLoop(); |
| 1438 | return 0; |
| 1439 | }}} |
| 1440 | |