Merge: crisel -> main

This commit is contained in:
PedroEdiaz
2024-10-09 20:45:05 -06:00
parent e25f509ce1
commit 252aab3dd2
4 changed files with 130 additions and 73 deletions

View File

@@ -2,9 +2,14 @@
#include <cglm/quat.h>
#include <GLFW/glfw3.h>
#define ANGLE ((float)0x02/0xff*2*GLM_PI)
#define ANGLE ((float)0x01/0xff*2*GLM_PI)
unsigned char selected_axis = 0;
int window_width;
int window_height;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] =
{
{1, 0, 0},
@@ -12,42 +17,85 @@ vec3 axis[3] =
{0, 0, 1},
};
void __window_callback(GLFWwindow * window, int w, int h)
{
int m;
m = (w < h) ? w : h;
window_width = w;
window_height = h;
glViewport((w - m) / 2, (h - m) / 2, m, m);
}
void __mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
unsigned char green_value;
double xpos, ypos;
if( button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS )
return;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN, GL_UNSIGNED_BYTE, &green_value);
switch(green_value)
{
case 0xD3:
case 0xD4:
case 0xD5:
selected_axis = green_value-0xD3;
}
}
void __scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
versor p = GLM_QUAT_IDENTITY_INIT;
glm_quatv(p, yoffset*ANGLE, axis[selected_axis]);
glm_quat_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]);
glm_quat_rotatev(p, axis[1], axis[1]);
glm_quat_rotatev(p, axis[2], axis[2]);
}
quat_t poll_input(window_t window)
{
versor p = GLM_QUAT_IDENTITY_INIT;
if( glfwGetKey( (void*)window, 'Q' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'Q') == GLFW_PRESS )
{
glm_quatv(p, ANGLE, axis[0]);
goto end;
}
if( glfwGetKey( (void*)window, 'W' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'W') == GLFW_PRESS )
{
glm_quatv(p, -ANGLE, axis[0]);
goto end;
}
if( glfwGetKey( (void*)window, 'A' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'A') == GLFW_PRESS )
{
glm_quatv(p, ANGLE, axis[1]);
goto end;
}
if( glfwGetKey( (void*)window, 'S' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'S') == GLFW_PRESS )
{
glm_quatv(p, -ANGLE, axis[1]);
goto end;
}
if( glfwGetKey( (void*)window, 'Z' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'Z') == GLFW_PRESS )
{
glm_quatv(p, ANGLE, axis[2]);
goto end;
}
if( glfwGetKey( (void*)window, 'X' ) == GLFW_PRESS )
if( glfwGetKey((GLFWwindow*)window, 'X') == GLFW_PRESS )
{
glm_quatv(p, -ANGLE, axis[2]);
goto end;
}
end:
glm_quat_mul( p, q, q );
glm_quat_rotatev( p, axis[0], axis[0] );
@@ -56,3 +104,4 @@ end:
glm_quat_normalize( q );
return q;
}

View File

@@ -3,7 +3,6 @@
#include "data/axis.h"
#include "data/shaders.h"
#include <GL/glew.h>
//#include <stdlib.h>
#define WIDTH 512
#define HEIGHT 512

View File

@@ -1,3 +1,6 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
typedef const void * window_t;
typedef unsigned int id_t;
typedef void * mesh_t;

View File

@@ -1,15 +1,15 @@
#include "main.h"
#include <GLFW/glfw3.h>
void __window_callback( GLFWwindow * window, int w, int h )
{
int m = ( w<h )? w: h;
glViewport( (w-m)/2, (h-m)/2, m, m );
}
void __window_callback(GLFWwindow *, int, int);
void __mouse_callback(GLFWwindow *, int, int, int);
void __scroll_callback(GLFWwindow *, double, double);
window_t init_window(unsigned int w, unsigned int h, const char * name)
{
void * window;
void __mouse_callback(GLFWwindow *window, int button, int action, int mods);
void __scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
if( !glfwInit() )
return NULL;
@@ -27,6 +27,11 @@ window_t init_window( unsigned int w, unsigned int h, const char * name )
}
glfwSetWindowSizeCallback(window, __window_callback);
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback);
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback);
__window_callback( window, w, h );
return window;
}
@@ -39,6 +44,7 @@ int is_open_window( window_t window )
{
glfwSwapBuffers((void*)window);
glfwPollEvents();
return !glfwWindowShouldClose((void*)window);
}