camera movement(wip)
This commit is contained in:
parent
901b509991
commit
ac1ed354ce
1 changed files with 52 additions and 9 deletions
61
mmn_17.c
61
mmn_17.c
|
@ -6,11 +6,19 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "ui.h"
|
||||
// close enough
|
||||
#define PI 3.14
|
||||
|
||||
int lastMouseButton = GLUT_LEFT_BUTTON;
|
||||
ui_slider* draggedSlider = NULL;
|
||||
|
||||
// Camera rotation and stuff
|
||||
float camRadius = 10.0;
|
||||
vec3 camCenter = { 0.0, 0.0, 0.0 };
|
||||
vec3 camRot = { PI * 0.125, 0.0, 0.0 };
|
||||
|
||||
// Ui stuff
|
||||
int lastMouseButton = GLUT_LEFT_BUTTON;
|
||||
int lastMouseX = 0, lastMouseY = 0;
|
||||
ui_slider* draggedSlider = NULL;
|
||||
void testButtonClick();
|
||||
ui_button buttons[] = {
|
||||
{
|
||||
|
@ -24,7 +32,7 @@ ui_button buttons[] = {
|
|||
ui_slider sliders[] = {
|
||||
{
|
||||
/* pos */ { { 0.5, 0.7, 0.0 }, {-50.0, -5.0, 0.0 } },
|
||||
/* size */ { 100.0, 10.0, 10.0 },
|
||||
/* size */ { 100.0, 10.0, 8.0 },
|
||||
0.1
|
||||
}
|
||||
};
|
||||
|
@ -32,6 +40,7 @@ ui_slider sliders[] = {
|
|||
void testButtonClick() {
|
||||
printf("Clicked!\n");
|
||||
sliders[0].value = 0.5;
|
||||
camCenter = vec3_new(0.0, 1.0, 0.0);
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
|
@ -65,15 +74,27 @@ void drawWorld(void) {
|
|||
// set perspective camera
|
||||
GLdouble wWidth = (GLdouble) glutGet(GLUT_WINDOW_WIDTH);
|
||||
GLdouble wHeight = (GLdouble) glutGet(GLUT_WINDOW_HEIGHT);
|
||||
// Calculate current camera position from rotation
|
||||
vec3 cp = vec3_new(0.0, 0.0, 0.0);
|
||||
// rotate along the x axis(yz plane)
|
||||
cp.z = cosf(camRot.x);
|
||||
cp.y = sinf(camRot.x);
|
||||
// rotate along the y axis(xy plane)
|
||||
cp.x = cp.z * sinf(camRot.y);
|
||||
cp.z *= cosf(camRot.y);
|
||||
cp = vec3_mult(cp, camRadius);
|
||||
cp = vec3_add(cp, camCenter);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(0.0, 10.0, 20.0, (0.5 - sliders[0].value) * 10.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
||||
gluLookAt(cp.x, cp.y, cp.z, camCenter.x, camCenter.y, camCenter.z, 0.0, 1.0, 0.0);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
gluPerspective(90, wWidth / wHeight , 1.0, 100.0);
|
||||
// Lights
|
||||
glEnable(GL_LIGHTING);
|
||||
glEnable(GL_LIGHT0);
|
||||
// Directional light - aka light
|
||||
GLfloat light1[] = { 0.0, 1.0, 0.0, 0.0 };
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, light1);
|
||||
// World
|
||||
|
@ -100,6 +121,7 @@ void display(void) {
|
|||
}
|
||||
|
||||
void mouseEvent(int button, int state, int x, int y) {
|
||||
lastMouseX = x; lastMouseY = y;
|
||||
if(state == 0) lastMouseButton = button;
|
||||
if(state == 0 && button == GLUT_LEFT_BUTTON) {
|
||||
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
|
||||
|
@ -134,18 +156,44 @@ void mouseMotionEvent(int x, int y) {
|
|||
}
|
||||
}
|
||||
}
|
||||
// TODO: replace with wasd/space/ctrl movement instead
|
||||
else if(lastMouseButton == GLUT_MIDDLE_BUTTON) {
|
||||
// get xz plane rotation
|
||||
float mx = cosf(camRot.y);
|
||||
float mz = -sinf(camRot.y);
|
||||
float m = (x - lastMouseX);
|
||||
|
||||
camCenter.y += (y - lastMouseY) * 0.5;
|
||||
camCenter.x -= m * mx;
|
||||
camCenter.z -= m * mz;
|
||||
printf("new cam center %f %f %f\n", camCenter.x, camCenter.y, camCenter.z);
|
||||
}
|
||||
else if(lastMouseButton == GLUT_RIGHT_BUTTON) {
|
||||
// rotate the camera
|
||||
camRot.y -= (x - lastMouseX) * PI * 0.01;
|
||||
if(camRot.y > 2 * PI) camRot.y -= 2 * PI;
|
||||
else if(camRot.y < 0.0) camRot.y += 2 * PI;
|
||||
|
||||
camRot.x = fmin(PI * 0.5, fmax(-PI * 0.5, camRot.x + (y - lastMouseY) * 0.01 * PI));
|
||||
}
|
||||
lastMouseX = x; lastMouseY = y;
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void mouseWheelEvent(int wheel, int dir, int x, int y) {
|
||||
char slided = 0;
|
||||
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
|
||||
if(ui_slider_mouse_over(s, x, y)) {
|
||||
s->value += dir * 0.05;
|
||||
if(s->value < 0.0) s->value = 0.0;
|
||||
if(s->value > 1.0) s->value = 1.0;
|
||||
printf("Updated %p value to %f\n", s, s->value);
|
||||
slided = 1;
|
||||
}
|
||||
}
|
||||
if(!slided) {
|
||||
camRadius *= 1.0 + (dir * 0.1);
|
||||
}
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
|
@ -173,11 +221,6 @@ int main(int argc, char** argv) {
|
|||
glutMouseWheelFunc(mouseWheelEvent);
|
||||
glutMotionFunc(mouseMotionEvent);
|
||||
|
||||
/* set projection and camera */
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
gluOrtho2D(0.0, 800.0, 0.0, 500.0);
|
||||
|
||||
glutMainLoop();
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue