Add mouse position to world coord translation.

master
Jeremy Hu 2017-01-16 22:19:47 +09:30
parent b8b724204b
commit feac7aea69
3 changed files with 60 additions and 4 deletions

View File

@ -22,6 +22,7 @@ int drawText(int x, int y, char *text);
int drawPrintf(int x, int y, const char *fmt, ...);
int drawDebugPrintf(const char *fmt, ...);
void drawDebugPoint(vec3 *origin, int colorIndex);
void drawTestUnproject(void);
#ifdef __cplusplus
}

View File

@ -17,7 +17,7 @@ static const float bmeshBallColors[][4] {
static const float bmeshBoneColor[3] = {1, 1, 0};
static QGLWidget *_this = 0;
static Render *_this = 0;
static int debugOutputTop = 0;
int drawDebugPrintf(const char *fmt, ...) {
@ -35,7 +35,14 @@ int drawDebugPrintf(const char *fmt, ...) {
}
static int drawBmeshBall(bmesh *bm, bmeshBall *ball) {
glColor4fv(bmeshBallColors[ball->type]);
float color[4];
memcpy(color, bmeshBallColors[ball->type], sizeof(color));
vec3 mousePos[] = {{_this->mouseWorldX, _this->mouseWorldY,
_this->mouseWorldZ}};
if (vec3Distance(&ball->position, mousePos) <= ball->radius * 5) {
color[3] = 1;
}
glColor4fv(color);
drawSphere(&ball->position, ball->radius, 36, 24);
return 0;
}
@ -43,7 +50,7 @@ static int drawBmeshBall(bmesh *bm, bmeshBall *ball) {
static void drawBmeshBallRecursively(bmesh *bm, bmeshBall *ball) {
bmeshBallIterator iterator;
bmeshBall *child;
//drawBmeshBall(bm, ball);
drawBmeshBall(bm, ball);
for (child = bmeshGetBallFirstChild(bm, ball, &iterator);
child;
child = bmeshGetBallNextChild(bm, ball, &iterator)) {
@ -213,6 +220,31 @@ void Render::initializeGL() {
drawInit();
}
void screenCoordsToWorld(float winX, float winY,
float *worldX, float *worldY, float *worldZ){
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winZ = 0;
GLdouble x, y, z;
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
winX = (float)winX;
winY = (float)viewport[3] - (float)winY;
winZ = 0;
gluUnProject((GLdouble)winX, (GLdouble)winY, (GLdouble)winZ,
(const GLdouble *)modelview, (const GLdouble *)projection, viewport,
&x, &y, &z);
*worldX = (float)x;
*worldY = (float)y;
*worldZ = (float)z;
}
#include "../data/bmesh_test_2.h"
void Render::paintGL() {
@ -237,6 +269,12 @@ void Render::paintGL() {
glEnable(GL_LIGHTING);
screenCoordsToWorld(_this->mouseX, _this->mouseY,
&_this->mouseWorldX, &_this->mouseWorldY, &_this->mouseWorldZ);
glColor3f(0.0f, 0.0f, 0.0f);
drawDebugPrintf("%d,%d -> %f,%f,%f", _this->mouseX, _this->mouseY,
_this->mouseWorldX, _this->mouseWorldY, _this->mouseWorldZ);
if (0 == bm) {
bmeshBall ball;
bmeshBone bone;
@ -313,6 +351,9 @@ void Render::paintGL() {
}
}
//glColor3f(0.0f, 0.0f, 0.0f);
//drawTestUnproject();
glPopMatrix();
//if (bm) {
@ -332,6 +373,17 @@ void Render::resizeGL(int w, int h) {
glLoadIdentity();
}
/*
void drawTestUnproject(void) {
float worldX, worldY, worldZ;
if (_this) {
screenCoordsToWorld(_this->mouseX, _this->mouseY, &worldX, &worldY, &worldZ);
glColor3f(0.0f, 0.0f, 0.0f);
drawDebugPrintf("%d,%d -> %f,%f,%f", _this->mouseX, _this->mouseY,
worldX, worldY, worldZ);
}
}*/
void Render::mousePressEvent(QMouseEvent *event) {
mouseX = event->x();
mouseY = event->y();

View File

@ -18,9 +18,12 @@ protected:
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent * event);
private:
public:
int mouseX;
int mouseY;
float mouseWorldX;
float mouseWorldY;
float mouseWorldZ;
float cameraAngleX;
float cameraAngleY;
float cameraDistance;