@@ -58,13 +58,60 @@ rpr_material_node g_diffuse=nullptr;
58
58
rpr_shape g_plane=nullptr ;
59
59
float * g_fbdata = nullptr ;
60
60
float g_camera_posX = 0.0 ;
61
+ float g_camera_posY = 5.0 ;
62
+ int g_lastMouseDownUpdateX = -1 ;
63
+ int g_lastMouseDownUpdateY = -1 ;
61
64
62
65
void Update ()
63
66
{
64
67
// Send update event
65
68
glutPostRedisplay ();
66
69
}
67
70
71
+ void MoveCamera ()
72
+ {
73
+ CHECK ( rprCameraLookAt (
74
+ g_camera, g_camera_posX, g_camera_posY, 20 , // position
75
+ 0 , 1 , 0 , // look at point
76
+ 0 , 1 , 0 // up vector
77
+ ) );
78
+
79
+ // camera moved, so we need to redraw the framebuffer.
80
+ CHECK ( rprFrameBufferClear (g_frame_buffer) );
81
+ }
82
+
83
+ void OnMouseMoveEvent (int x, int y)
84
+ {
85
+ g_camera_posX += (x-g_lastMouseDownUpdateX)/4 ;
86
+ g_camera_posY += (y-g_lastMouseDownUpdateY)/4 ;
87
+
88
+ // avoid to have a camera under the floor.
89
+ if ( g_camera_posY < 0.1 ) { g_camera_posY = 0.1 ; }
90
+
91
+ g_lastMouseDownUpdateX = x;
92
+ g_lastMouseDownUpdateY = y;
93
+
94
+ MoveCamera ();
95
+ return ;
96
+ }
97
+
98
+ void OnMouseEvent (int button, int state, int x, int y)
99
+ {
100
+ if ( button == GLUT_LEFT_BUTTON )
101
+ {
102
+ if ( state == GLUT_DOWN )
103
+ {
104
+ g_lastMouseDownUpdateX = x;
105
+ g_lastMouseDownUpdateY = y;
106
+ }
107
+ else if ( state == GLUT_UP )
108
+ {
109
+ }
110
+ }
111
+
112
+ return ;
113
+ }
114
+
68
115
void OnKeyboardEvent (unsigned char key, int xmouse, int ymouse)
69
116
{
70
117
bool cameraMoves = false ;
@@ -90,12 +137,8 @@ void OnKeyboardEvent(unsigned char key, int xmouse, int ymouse)
90
137
}
91
138
92
139
if ( cameraMoves )
93
- {
94
- CHECK ( rprCameraLookAt (g_camera, g_camera_posX, 5 , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
95
-
96
- // camera moved, so we need to redraw the framebuffer.
97
- CHECK ( rprFrameBufferClear (g_frame_buffer) );
98
- }
140
+ MoveCamera ();
141
+
99
142
}
100
143
101
144
void Display ()
@@ -294,7 +337,7 @@ int main(int argc, char** argv)
294
337
CHECK ( rprContextCreateCamera (g_context, &g_camera) );
295
338
296
339
// Position camera in world space:
297
- CHECK ( rprCameraLookAt (g_camera, g_camera_posX, 5 , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
340
+ CHECK ( rprCameraLookAt (g_camera, g_camera_posX, g_camera_posY , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
298
341
299
342
CHECK ( rprCameraSetFocalLength (g_camera, 75 .f ) );
300
343
@@ -376,6 +419,8 @@ int main(int argc, char** argv)
376
419
glutDisplayFunc (Display);
377
420
glutIdleFunc (Update);
378
421
glutKeyboardFunc (OnKeyboardEvent);
422
+ glutMouseFunc (OnMouseEvent);
423
+ glutMotionFunc (OnMouseMoveEvent);
379
424
glutMainLoop ();
380
425
381
426
0 commit comments