I have modifed the pyramid opengl sample code to let the mouse wheel to change the fov camera angle.
Here is the patch file.
From e474a1bd96dd17943d3213be9052345fef81e966 Mon Sep 17 00:00:00 2001
From: asmwarrior <asmwa...@gmail.com>
Date: Mon, 24 Apr 2023 15:57:32 +0800
Subject: [PATCH] Implement the mouse wheel event to change the fov of the
camera
---
samples/opengl/pyramid/oglstuff.cpp | 22 ++++++++++++++++++++++
samples/opengl/pyramid/oglstuff.h | 4 ++++
samples/opengl/pyramid/pyramid.cpp | 13 +++++++++++++
3 files changed, 39 insertions(+)
diff --git a/samples/opengl/pyramid/oglstuff.cpp b/samples/opengl/pyramid/oglstuff.cpp
index 367f681..bd7a92e 100644
--- a/samples/opengl/pyramid/oglstuff.cpp
+++ b/samples/opengl/pyramid/oglstuff.cpp
@@ -1092,6 +1092,23 @@ double myOGLCamera::GetTrackballZ(double x, double y, double r)
return (d < r2/2.0) ? sqrt(r2 - d) : r2/2.0/sqrt(d);
}
+void myOGLCamera::MouseWheel(int wheel)
+{
+ // update the m_fov
+ // From degrees to radians
+ const double degToRad = (double) 4.0 * atan(1.0) / 180.0;
+ // Angle of the field of view
+ // the original m_fov = 40.0 * degToRad; //radians
+ m_fov += wheel/120.0 * degToRad;
+
+ // Calculate the projection matrix
+ double aspect = (double) m_winWidth / m_winHeight;
+ MyPerspective(m_fov, aspect, m_nearD, m_farD, m_dProj);
+
+ // Inform we need to calculate MVP matrix
+ m_needMVPUpdate = true;
+}
+
// ----------------------------------------------------------------------------
// myOGLManager
@@ -1261,3 +1278,8 @@ void myOGLManager::OnMouseRotDragging(int posX, int posY)
m_mousePrevX = posX;
m_mousePrevY = posY;
}
+
+void myOGLManager::OnMouseWheel(int wheel)
+{
+ m_Camera.MouseWheel(wheel);
+}
diff --git a/samples/opengl/pyramid/oglstuff.h b/samples/opengl/pyramid/oglstuff.h
index 65b4c7f..4adc9d2 100644
--- a/samples/opengl/pyramid/oglstuff.h
+++ b/samples/opengl/pyramid/oglstuff.h
@@ -257,6 +257,9 @@ public:
void MouseRotation(int fromX, int fromY, int toX, int toY);
double GetTrackballZ(double x, double y, double r);
+ // when mouse wheel, we just change the fov angle
+ void MouseWheel(int wheel);
+
// The used matrices
double m_dMode[16]; // The model matrix, rotation in this sample
double m_dView[16]; // The view matrix
@@ -318,6 +321,7 @@ public:
// Action events in OpenGL win coordinates (bottom is y=0)
void OnMouseButDown(int posX, int posY);
void OnMouseRotDragging(int posX, int posY);
+ void OnMouseWheel(int wheel);
private:
// Members
diff --git a/samples/opengl/pyramid/pyramid.cpp b/samples/opengl/pyramid/pyramid.cpp
index b8f988f..558df69 100644
--- a/samples/opengl/pyramid/pyramid.cpp
+++ b/samples/opengl/pyramid/pyramid.cpp
@@ -593,5 +593,18 @@ void MyGLCanvas::OnMouse(wxMouseEvent& event)
Refresh(false);
}
}
+ else if ( event.GetWheelRotation() )
+ {
+#if wxUSE_LOGWINDOW
+ wxString message = wxString::Format("%s wheel rotation %+d",
+ event.GetWheelAxis() == wxMOUSE_WHEEL_VERTICAL ? "Vertical" : "Horizontal",
+ event.GetWheelRotation());
+ wxLogMessage(message);
+#endif // wxUSE_LOGWINDOW
+
+ // mouse wheel, to change the fov
+ m_oglManager->OnMouseWheel( event.GetWheelRotation() );
+ Refresh(false);
+ }
}
--
2.40.0.windows.1
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
This is the result of a screen cast (gif file)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
Thanks, but I'm not really sure if/why do we want to add this. There are infinitely many things that could be done with OpenGL and this one doesn't seem any different from many other ones. Personally I think we should keep the sample as simple as possible, but if people find some value in adding this to it, why not...
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
I think the good thing "pyramid" sample code has is that it only depends on wxWidgets library, and it does not depend on other related libraries, such as glm library or glew library. So, it could be a start point to learn opengl+wxWidgets combination.
It shows a lot of common and basic features of modern core profile opengl code: mouse navigation(rotate), 3D text on screen, 3D text on a surface, 3D surface construction, colors, camera.
zoom in/out feature is a very common feature for the beginners, also maybe there are other common mouse features like pan on the scene.
Besides that, I think it is feature complete as a wx sample code.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
Changing the fov angle is a way to simulate zooming. But it has the issue of distorting the image for vales out of [20-80] degrees.
Another way, more realistic in most cases, is moving the camera in the direction of the view.
Any how, I agree that pyramid sample is simple enough but contains several basic methods. In this zoom case, due to two ways of achieve it, I prefer not to touch the existing sample.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
Hi, Manolo, thanks for the reply.
I'm OK with your option, and I think this issue can be closed. Thanks.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
Closed #23483 as completed.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()