Completed touch interface

This commit is contained in:
Vladislav Khorev 2025-12-05 22:27:45 +03:00
parent 2b19a3eaba
commit 0322098efa
7 changed files with 129 additions and 29 deletions

View File

@ -15,12 +15,6 @@ bool Environment::rightPressed = false;
bool Environment::upPressed = false;
bool Environment::downPressed = false;
Vector3f Environment::cameraShift = {0, 0, 0};
Vector3f Environment::characterPos = {0, 0, 0};
float Environment::cameraPhi = 0.f;
float Environment::cameraAlpha = 0.3*M_PI / 2.0;
bool Environment::settings_inverseVertical = false;
SDL_Window* Environment::window = nullptr;
@ -29,6 +23,13 @@ bool Environment::showMouse = false;
bool Environment::exitGameLoop = false;
Matrix3f Environment::shipMatrix = Matrix3f::Identity();
bool Environment::tapDownHold = false;
Vector2f Environment::tapDownStartPos = { 0, 0 };
Vector2f Environment::tapDownCurrentPos = { 0, 0 };
} // namespace ZL

View File

@ -19,21 +19,21 @@ public:
static bool upPressed;
static bool downPressed;
static Vector3f cameraShift;
static Vector3f characterPos;
static float cameraPhi;
static float cameraAlpha;
static bool settings_inverseVertical;
static Matrix3f shipMatrix;
static SDL_Window* window;
static bool showMouse;
static bool exitGameLoop;
static bool tapDownHold;
static Vector2f tapDownStartPos;
static Vector2f tapDownCurrentPos;
};
} // namespace ZL

View File

@ -61,7 +61,7 @@ void Game::setup() {
});
cubemap.data = ZL::CreateCubemap(1000);
cubemap.data = ZL::CreateCubemap(500);
cubemap.RefreshVBO();
//Load texture
@ -99,6 +99,7 @@ void Game::drawScene() {
1, 1000);
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.RotateMatrix(Environment::shipMatrix);
CheckGlError();
@ -127,9 +128,9 @@ void Game::drawScene() {
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.TranslateMatrix({ 0,0, -1.0f*Environment::zoom });
renderer.RotateMatrix(QuatFromRotateAroundX(M_PI/6.0));
//renderer.RotateMatrix(QuatFromRotateAroundX(Environment::cameraAlpha));
//renderer.RotateMatrix(Environment::shipMatrix);
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
renderer.DrawVertexRenderStruct(spaceship);
@ -158,8 +159,38 @@ void Game::processTickCount() {
//gameObjects.updateScene(delta);
Environment::cameraAlpha = Environment::cameraAlpha + delta * M_PI / 10000.f;
if (Environment::tapDownHold) {
float diffx = Environment::tapDownCurrentPos.v[0] - Environment::tapDownStartPos.v[0];
float diffy = Environment::tapDownCurrentPos.v[1] - Environment::tapDownStartPos.v[1];
if (abs(diffy) > 5.0 || abs(diffx) > 5.0) //threshold
{
float rotationPower = sqrtf(diffx * diffx + diffy * diffy);
std::cout << rotationPower << std::endl;
float deltaAlpha = rotationPower * delta * M_PI / 500000.f;
Vector3f rotationDirection = { -diffy, -diffx, 0 };
rotationDirection = rotationDirection.normalized();
Vector4f rotateQuat = {
rotationDirection.v[0] * sin(deltaAlpha * 0.5f),
rotationDirection.v[1] * sin(deltaAlpha * 0.5f),
rotationDirection.v[2] * sin(deltaAlpha * 0.5f),
cos(deltaAlpha * 0.5f) };
Matrix3f rotateMat = QuatToMatrix(rotateQuat);
Environment::shipMatrix = MultMatrixMatrix(rotateMat, Environment::shipMatrix);
}
}
lastTickCount = newTickCount;
}
}
@ -176,13 +207,34 @@ void Game::render() {
SDL_GL_SwapWindow(ZL::Environment::window);
}
void Game::update() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
Environment::exitGameLoop = true;
}
else if (event.type == SDL_MOUSEBUTTONDOWN) {
// 1. Îáðàáîòêà íàæàòèÿ êíîïêè ìûøè
Environment::tapDownHold = true;
// Êîîðäèíàòû íà÷àëüíîãî íàæàòèÿ
Environment::tapDownStartPos.v[0] = event.button.x;
Environment::tapDownStartPos.v[1] = event.button.y;
// Íà÷àëüíàÿ ïîçèöèÿ òàêæå ñòàíîâèòñÿ òåêóùåé
Environment::tapDownCurrentPos.v[0] = event.button.x;
Environment::tapDownCurrentPos.v[1] = event.button.y;
}
else if (event.type == SDL_MOUSEBUTTONUP) {
// 2. Îáðàáîòêà îòïóñêàíèÿ êíîïêè ìûøè
Environment::tapDownHold = false;
}
else if (event.type == SDL_MOUSEMOTION) {
// 3. Îáðàáîòêà ïåðåìåùåíèÿ ìûøè
if (Environment::tapDownHold) {
// Îáíîâëåíèå òåêóùåé ïîçèöèè, åñëè êíîïêà óäåðæèâàåòñÿ
Environment::tapDownCurrentPos.v[0] = event.motion.x;
Environment::tapDownCurrentPos.v[1] = event.motion.y;
}
}
else if (event.type == SDL_MOUSEWHEEL) {
static const float zoomstep = 2.0f;
@ -195,15 +247,7 @@ void Game::update() {
if (Environment::zoom < zoomstep) {
Environment::zoom = zoomstep;
}
/*if (Environment::zoom > 4) {
Environment::zoom = 4;
}*/
//this->modelMeshRender.data.Scale(0.5);
//this->modelMeshRender.RefreshVBO();
}
}
render();
}

View File

@ -655,6 +655,17 @@ namespace ZL {
return r;
}
Vector3f MultMatrixVector(Matrix3f mt, Vector3f v)
{
Vector3f r;
r.v[0] = v.v[0] * mt.m[0] + v.v[1] * mt.m[3] + v.v[2] * mt.m[6];
r.v[1] = v.v[0] * mt.m[1] + v.v[1] * mt.m[4] + v.v[2] * mt.m[7];
r.v[2] = v.v[0] * mt.m[2] + v.v[1] * mt.m[5] + v.v[2] * mt.m[8];
return r;
}
Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t)
{
const float epsilon = 1e-6f;

14
Math.h
View File

@ -31,8 +31,21 @@ namespace ZL {
struct Vector3f
{
std::array<float, 3> v = { 0.f, 0.f, 0.f };
Vector3f normalized() const {
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
Vector3f r;
r.v[0] = v[0] / norm;
r.v[1] = v[1] / norm;
r.v[2] = v[2] / norm;
return r;
}
};
struct Vector2f
{
std::array<float, 2> v = {0.f, 0.f};
@ -99,6 +112,7 @@ namespace ZL {
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt);
Vector4f MultVectorMatrix(Vector4f v, Matrix4f mt);
Vector4f MultMatrixVector(Matrix4f mt, Vector4f v);
Vector3f MultMatrixVector(Matrix3f mt, Vector3f v);
Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t);
Matrix3f InverseMatrix(const Matrix3f& m);

View File

@ -603,6 +603,35 @@ namespace ZL {
SetMatrix();
}
void Renderer::RotateMatrix(const Matrix3f& m3)
{
Matrix4f m = Matrix4f::Identity();
m.m[0] = m3.m[0];
m.m[1] = m3.m[1];
m.m[2] = m3.m[2];
m.m[4] = m3.m[3];
m.m[5] = m3.m[4];
m.m[6] = m3.m[5];
m.m[8] = m3.m[6];
m.m[9] = m3.m[7];
m.m[10] = m3.m[8];
m = ModelviewMatrixStack.top() * m;
if (ModelviewMatrixStack.size() == 0)
{
throw std::runtime_error("Modelview matrix stack underflow!!!!");
}
ModelviewMatrixStack.pop();
ModelviewMatrixStack.push(m);
SetMatrix();
}
void Renderer::PushSpecialMatrix(const Matrix4f& m)
{
if (ModelviewMatrixStack.size() > 64)

View File

@ -102,6 +102,7 @@ namespace ZL {
void ScaleMatrix(float scale);
void ScaleMatrix(const Vector3f& scale);
void RotateMatrix(const Vector4f& q);
void RotateMatrix(const Matrix3f& m3);
void PushSpecialMatrix(const Matrix4f& m);
void PopMatrix();