Added mouse tracking on 3d view for future operations

This commit is contained in:
Michaël Lemaire 2015-09-07 01:15:59 +02:00
parent 1361c5c654
commit a96699dece
11 changed files with 100 additions and 1 deletions

View file

@ -39,6 +39,8 @@ public:
inline const Matrix4 &getTransformationMatrix() const {return projector;} inline const Matrix4 &getTransformationMatrix() const {return projector;}
inline VectorSpherical getDirectionSpherical() const {return direction;} inline VectorSpherical getDirectionSpherical() const {return direction;}
inline CameraPerspective getPerspective() const {return perspective;} inline CameraPerspective getPerspective() const {return perspective;}
inline double getWidth() const {return width;}
inline double getHeight() const {return height;}
double getRealDepth(const Vector3 &projected) const; double getRealDepth(const Vector3 &projected) const;

View file

@ -14,6 +14,7 @@ OpenGLView::OpenGLView(QQuickItem *parent) :
renderer = NULL; renderer = NULL;
setAcceptedMouseButtons(Qt::AllButtons); setAcceptedMouseButtons(Qt::AllButtons);
setAcceptHoverEvents(true);
mouse_button = Qt::NoButton; mouse_button = Qt::NoButton;
@ -120,6 +121,13 @@ void OpenGLView::mouseMoveEvent(QMouseEvent *event)
window->getCamera()->processScroll(-0.02 * factor * diff.x(), 0.02 * factor * diff.y()); window->getCamera()->processScroll(-0.02 * factor * diff.x(), 0.02 * factor * diff.y());
} }
mouse_pos = event->windowPos(); mouse_pos = event->windowPos();
renderer->setMouseLocation(event->pos().x(), height() - event->pos().y());
}
void OpenGLView::hoverMoveEvent(QHoverEvent *event)
{
renderer->setMouseLocation(event->pos().x(), height() - event->pos().y());
} }
void OpenGLView::timerEvent(QTimerEvent *) void OpenGLView::timerEvent(QTimerEvent *)

View file

@ -24,6 +24,7 @@ protected:
virtual void mousePressEvent(QMouseEvent *event) override; virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseReleaseEvent(QMouseEvent *event) override; virtual void mouseReleaseEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void hoverMoveEvent(QHoverEvent *event) override;
virtual void timerEvent(QTimerEvent *event) override; virtual void timerEvent(QTimerEvent *event) override;
private: private:

View file

@ -10,6 +10,7 @@
#include "Scenery.h" #include "Scenery.h"
#include "LightingManager.h" #include "LightingManager.h"
#include "Logs.h" #include "Logs.h"
#include "Vector3.h"
#include "GL/glu.h" // TEMP #include "GL/glu.h" // TEMP
@ -21,6 +22,13 @@ OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
vp_width = 1; vp_width = 1;
vp_height = 1; vp_height = 1;
mouse_tracking = true;
mouse_x = 0;
mouse_y = 0;
mouse_projected = new Vector3();
view_matrix = new QMatrix4x4;
render_quality = 3; render_quality = 3;
functions = new OpenGLFunctions(); functions = new OpenGLFunctions();
@ -40,6 +48,10 @@ OpenGLRenderer::~OpenGLRenderer()
water->interrupt(); water->interrupt();
skybox->interrupt(); skybox->interrupt();
delete mouse_projected;
delete view_matrix;
delete skybox; delete skybox;
delete water; delete water;
delete terrain; delete terrain;
@ -140,6 +152,11 @@ void OpenGLRenderer::paint()
terrain->render(); terrain->render();
water->render(); water->render();
if (mouse_tracking)
{
updateMouseProjection();
}
int error_code; int error_code;
while ((error_code = glGetError()) != GL_NO_ERROR) while ((error_code = glGetError()) != GL_NO_ERROR)
{ {
@ -172,6 +189,17 @@ void OpenGLRenderer::resume()
terrain->resume(); terrain->resume();
} }
void OpenGLRenderer::setMouseLocation(int x, int y)
{
mouse_x = x;
mouse_y = y;
}
const Vector3 &OpenGLRenderer::getMouseProjection()
{
return *mouse_projected;
}
void OpenGLRenderer::cameraChangeEvent(CameraDefinition *camera) void OpenGLRenderer::cameraChangeEvent(CameraDefinition *camera)
{ {
// Get camera info // Get camera info
@ -191,9 +219,11 @@ void OpenGLRenderer::cameraChangeEvent(CameraDefinition *camera)
projection.setToIdentity(); projection.setToIdentity();
projection.perspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar); projection.perspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar);
*view_matrix = projection * transform;
// Set in shaders // Set in shaders
shared_state->set("cameraLocation", location); shared_state->set("cameraLocation", location);
shared_state->set("viewMatrix", projection * transform); shared_state->set("viewMatrix", *view_matrix);
} }
double OpenGLRenderer::getPrecision(const Vector3 &) double OpenGLRenderer::getPrecision(const Vector3 &)
@ -205,3 +235,15 @@ Color OpenGLRenderer::applyMediumTraversal(Vector3, Color color)
{ {
return color; return color;
} }
void OpenGLRenderer::updateMouseProjection()
{
GLfloat z;
functions->glReadPixels(mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
QVector4D located(mouse_x / render_camera->getWidth(), mouse_y / render_camera->getHeight(), z, 1.0);
QVector4D unprojected = view_matrix->inverted() * 2.0 * (located - QVector4D(0.5, 0.5, 0.5, 0.5));
*mouse_projected = Vector3(unprojected.x() / unprojected.w(), unprojected.y() / unprojected.w(), unprojected.z() / unprojected.w());
shared_state->set("mouseProjection", *mouse_projected);
}

View file

@ -5,6 +5,8 @@
#include "SoftwareRenderer.h" #include "SoftwareRenderer.h"
class QMatrix4x4;
namespace paysages { namespace paysages {
namespace opengl { namespace opengl {
@ -42,6 +44,16 @@ public:
*/ */
void resume(); void resume();
/**
* Set the current mouse location, for use by getMouseProjection().
*/
void setMouseLocation(int x, int y);
/**
* Get the coordinates of the mouse, projected in world space.
*/
const Vector3 &getMouseProjection();
/** /**
* Change the camera location. * Change the camera location.
*/ */
@ -54,12 +66,25 @@ public:
virtual double getPrecision(const Vector3 &location) override; virtual double getPrecision(const Vector3 &location) override;
virtual Color applyMediumTraversal(Vector3 location, Color color) override; virtual Color applyMediumTraversal(Vector3 location, Color color) override;
private:
/**
* Update the mouse_projected member.
*/
void updateMouseProjection();
private: private:
bool ready; bool ready;
bool paused; bool paused;
int vp_width; int vp_width;
int vp_height; int vp_height;
bool mouse_tracking;
int mouse_x;
int mouse_y;
Vector3 *mouse_projected;
QMatrix4x4 *view_matrix;
OpenGLFunctions* functions; OpenGLFunctions* functions;
OpenGLSharedState* shared_state; OpenGLSharedState* shared_state;

View file

@ -63,6 +63,7 @@ void OpenGLTerrain::initialize()
program->addFragmentSource("atmosphere"); program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout"); program->addFragmentSource("fadeout");
program->addFragmentSource("ui");
program->addFragmentSource("terrain"); program->addFragmentSource("terrain");
// Add terrain chunks // Add terrain chunks

View file

@ -30,6 +30,7 @@ void OpenGLWater::initialize()
program->addFragmentSource("atmosphere"); program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout"); program->addFragmentSource("fadeout");
program->addFragmentSource("ui");
program->addFragmentSource("noise"); program->addFragmentSource("noise");
program->addFragmentSource("water"); program->addFragmentSource("water");

View file

@ -10,5 +10,6 @@
<file>terrain.vert</file> <file>terrain.vert</file>
<file>fadeout.frag</file> <file>fadeout.frag</file>
<file>noise.frag</file> <file>noise.frag</file>
<file>ui.frag</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -9,5 +9,7 @@ void main(void)
gl_FragColor = applyToneMapping(gl_FragColor); gl_FragColor = applyToneMapping(gl_FragColor);
gl_FragColor = applyMouseTracking(unprojected, gl_FragColor);
gl_FragColor.a = distanceFadeout(); gl_FragColor.a = distanceFadeout();
} }

View file

@ -0,0 +1,14 @@
uniform vec3 mouseProjection;
vec4 applyMouseTracking(vec3 location, vec4 color)
{
float dist = length(location - mouseProjection);
if (dist > 2.0)
{
return color;
}
else
{
return mix(color, vec4(1.0, 0.0, 0.0, 1.0), 1.0 - dist / 2.0);
}
}

View file

@ -15,5 +15,7 @@ void main(void)
gl_FragColor = applyToneMapping(gl_FragColor); gl_FragColor = applyToneMapping(gl_FragColor);
gl_FragColor = applyMouseTracking(unprojected, gl_FragColor);
gl_FragColor.a = distanceFadeout(); gl_FragColor.a = distanceFadeout();
} }