gamepad input
This commit is contained in:
parent
850cfd9671
commit
392b767f29
2
Makefile
2
Makefile
@ -116,6 +116,8 @@ MAIN_OBJS = \
|
|||||||
src/make.o \
|
src/make.o \
|
||||||
src/shader.o \
|
src/shader.o \
|
||||||
src/render.o \
|
src/render.o \
|
||||||
|
src/state.o \
|
||||||
|
src/input.o \
|
||||||
model/test_scene_color.data.o \
|
model/test_scene_color.data.o \
|
||||||
$(SHADER_OBJS) \
|
$(SHADER_OBJS) \
|
||||||
$(GLFW)
|
$(GLFW)
|
||||||
|
|||||||
11
include/input.h
Normal file
11
include/input.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void input();
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -1,13 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "state.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void render(const struct state * state,
|
void render(unsigned int program,
|
||||||
unsigned int program,
|
|
||||||
unsigned int program__trans,
|
unsigned int program__trans,
|
||||||
unsigned int program__texture0,
|
unsigned int program__texture0,
|
||||||
unsigned int color,
|
unsigned int color,
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct state {
|
|
||||||
float rx;
|
|
||||||
float ry;
|
|
||||||
float rz;
|
|
||||||
float tx;
|
|
||||||
float ty;
|
|
||||||
float tz;
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
10
include/state.hpp
Normal file
10
include/state.hpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "math/float_types.hpp"
|
||||||
|
|
||||||
|
struct state {
|
||||||
|
mat4x4 world_to_view;
|
||||||
|
int button[16];
|
||||||
|
};
|
||||||
|
|
||||||
|
extern state g_state;
|
||||||
62
src/input.cpp
Normal file
62
src/input.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include "math/transform.hpp"
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
#include "state.hpp"
|
||||||
|
|
||||||
|
static inline float deadzone(float n)
|
||||||
|
{
|
||||||
|
if (fabsf(n) < 0.05f) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool button_down(const GLFWgamepadstate& state, int i)
|
||||||
|
{
|
||||||
|
bool down = state.buttons[i] && (g_state.button[i] != state.buttons[i]);
|
||||||
|
g_state.button[i] = state.buttons[i];
|
||||||
|
return down;
|
||||||
|
}
|
||||||
|
|
||||||
|
void input()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 16; i++) {
|
||||||
|
GLFWgamepadstate state;
|
||||||
|
int jid = GLFW_JOYSTICK_1 + i;
|
||||||
|
int ret = glfwGetGamepadState(jid, &state);
|
||||||
|
if (ret == false)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (button_down(state, GLFW_GAMEPAD_BUTTON_START)) {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
printf("%3.06f, %3.06f, %3.06f, %3.06f\n",
|
||||||
|
g_state.world_to_view[i][0],
|
||||||
|
g_state.world_to_view[i][1],
|
||||||
|
g_state.world_to_view[i][2],
|
||||||
|
g_state.world_to_view[i][3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float lx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_X]);
|
||||||
|
float ly = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]);
|
||||||
|
float rx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X]);
|
||||||
|
float ry = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]);
|
||||||
|
float tl = state.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];
|
||||||
|
float tr = state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
|
||||||
|
float y = 0.01 * (tl - tr);
|
||||||
|
|
||||||
|
g_state.world_to_view
|
||||||
|
= rotate_x(ly * 0.01f)
|
||||||
|
* rotate_y(lx * 0.01f)
|
||||||
|
* translate(vec3(rx * -0.01f, y, ry * -0.01f))
|
||||||
|
* g_state.world_to_view;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/main.c
20
src/main.c
@ -11,7 +11,7 @@
|
|||||||
#include "make.h"
|
#include "make.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "state.h"
|
#include "input.h"
|
||||||
#include "model/test_scene.h"
|
#include "model/test_scene.h"
|
||||||
#include "model/test_scene_color.data.h"
|
#include "model/test_scene_color.data.h"
|
||||||
#include "shader/scene.vs.glsl.h"
|
#include "shader/scene.vs.glsl.h"
|
||||||
@ -152,24 +152,15 @@ int main()
|
|||||||
const double first_frame = glfwGetTime();
|
const double first_frame = glfwGetTime();
|
||||||
double last_frame = first_frame;
|
double last_frame = first_frame;
|
||||||
|
|
||||||
struct state state = {};
|
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
|
|
||||||
state.rx += 0.01f;
|
input();
|
||||||
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
|
|
||||||
state.rx -= 0.01f;
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
|
|
||||||
state.ry += 0.01f;
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
|
|
||||||
state.ry -= 0.01f;
|
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||||
|
|
||||||
render(&state,
|
render(program,
|
||||||
program,
|
|
||||||
program__trans,
|
program__trans,
|
||||||
program__texture0,
|
program__texture0,
|
||||||
scene_color,
|
scene_color,
|
||||||
@ -178,8 +169,7 @@ int main()
|
|||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
render(&state,
|
render(program,
|
||||||
program,
|
|
||||||
program__trans,
|
program__trans,
|
||||||
program__texture0,
|
program__texture0,
|
||||||
scene_color,
|
scene_color,
|
||||||
|
|||||||
@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
#include "glad/gl.h"
|
#include "glad/gl.h"
|
||||||
|
|
||||||
#include "render.h"
|
|
||||||
#include "math/float_types.hpp"
|
#include "math/float_types.hpp"
|
||||||
#include "math/transform.hpp"
|
#include "math/transform.hpp"
|
||||||
|
|
||||||
|
#include "render.h"
|
||||||
|
#include "state.hpp"
|
||||||
|
|
||||||
mat4x4 perspective()
|
mat4x4 perspective()
|
||||||
{
|
{
|
||||||
mat4x4 m1 = mat4x4(1, 0, 0, 0,
|
mat4x4 m1 = mat4x4(1, 0, 0, 0,
|
||||||
@ -17,15 +19,14 @@ mat4x4 perspective()
|
|||||||
return m1;
|
return m1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(const struct state * state,
|
void render(unsigned int program,
|
||||||
unsigned int program,
|
|
||||||
unsigned int program__trans,
|
unsigned int program__trans,
|
||||||
unsigned int program__texture0,
|
unsigned int program__texture0,
|
||||||
unsigned int color,
|
unsigned int color,
|
||||||
unsigned int vertex_array,
|
unsigned int vertex_array,
|
||||||
int triangles_length)
|
int triangles_length)
|
||||||
{
|
{
|
||||||
mat4x4 trans = perspective() * translate(vec3(0, 0, -2)) * scale(1.0f) * rotate_y(state->ry) * rotate_x(state->rx);
|
mat4x4 trans = perspective() * g_state.world_to_view * translate(vec3(0, 0, -2)) * scale(1.0f);
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glClearDepth(-1000.0f);
|
glClearDepth(-1000.0f);
|
||||||
|
|||||||
10
src/state.cpp
Normal file
10
src/state.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "state.hpp"
|
||||||
|
|
||||||
|
state g_state = {
|
||||||
|
.world_to_view = mat4x4(
|
||||||
|
0.827267, -0.007020, -0.561762, -1.162349,
|
||||||
|
-0.375750, 0.736448, -0.562542, -1.042329,
|
||||||
|
0.417658, 0.676455, 0.606601, -1.447194,
|
||||||
|
0.000000, 0.000000, 0.000000, 1.000000
|
||||||
|
)
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user