Skip to content

Commit f75575c

Browse files
committed
add mouse support in OpenGL demo
1 parent 1c8e8a8 commit f75575c

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

tutorials/32_gl_interop/main.cpp

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,60 @@ rpr_material_node g_diffuse=nullptr;
5858
rpr_shape g_plane=nullptr;
5959
float* g_fbdata = nullptr;
6060
float g_camera_posX = 0.0;
61+
float g_camera_posY = 5.0;
62+
int g_lastMouseDownUpdateX = -1;
63+
int g_lastMouseDownUpdateY = -1;
6164

6265
void Update()
6366
{
6467
// Send update event
6568
glutPostRedisplay();
6669
}
6770

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+
68115
void OnKeyboardEvent(unsigned char key, int xmouse, int ymouse)
69116
{
70117
bool cameraMoves = false;
@@ -90,12 +137,8 @@ void OnKeyboardEvent(unsigned char key, int xmouse, int ymouse)
90137
}
91138

92139
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+
99142
}
100143

101144
void Display()
@@ -294,7 +337,7 @@ int main(int argc, char** argv)
294337
CHECK( rprContextCreateCamera(g_context, &g_camera) );
295338

296339
// 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) );
298341

299342
CHECK( rprCameraSetFocalLength(g_camera, 75.f) );
300343

@@ -376,6 +419,8 @@ int main(int argc, char** argv)
376419
glutDisplayFunc(Display);
377420
glutIdleFunc(Update);
378421
glutKeyboardFunc(OnKeyboardEvent);
422+
glutMouseFunc(OnMouseEvent);
423+
glutMotionFunc(OnMouseMoveEvent);
379424
glutMainLoop();
380425

381426

0 commit comments

Comments
 (0)