/* Engine globals */
GLFWwindow* vg_window;
-m3x3f vg_pv;
+
+#ifdef VG_3D
+ m4x4f vg_pv;
+#else
+ m3x3f vg_pv;
+#endif
#ifdef VG_CAPTURE_MODE
int vg_window_x = 1920;
#ifndef VG_RELEASE
void vg_checkgl( const char *src_info )
{
- GLenum err;
- while( (err = glGetError()) != GL_NO_ERROR )
- {
- vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
- }
+ GLenum err;
+ while( (err = glGetError()) != GL_NO_ERROR )
+ {
+ vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
+ }
}
#define VG_STRINGIT( X ) #X
void vg_register_exit( void( *funcptr )(void), const char *name )
{
- vg_info( "exit registered: (%u)'%s'\n", vg_exit_count, name );
- vg_on_exit[ vg_exit_count ++ ] = funcptr;
+ vg_info( "exit registered: (%u)'%s'\n", vg_exit_count, name );
+ vg_on_exit[ vg_exit_count ++ ] = funcptr;
}
void vg_exit(void)
{
- for( int i = vg_exit_count-1; i >= 0; i -- )
- {
- vg_info( "engine_exit[%d]()\n", i );
- vg_on_exit[i]();
- }
-
- vg_info( "done\n" );
+ for( int i = vg_exit_count-1; i >= 0; i -- )
+ {
+ vg_info( "engine_exit[%d]()\n", i );
+ vg_on_exit[i]();
+ }
+
+ vg_info( "done\n" );
}
void vg_exiterr( const char *strErr )
{
- vg_error( "Engine Fatal: %s\n", strErr );
- vg_exit();
- exit(0);
+ vg_error( "Engine Fatal: %s\n", strErr );
+ vg_exit();
+ exit(0);
}
void vg_mouse_callback( GLFWwindow* ptrW, double xpos, double ypos )
{
- vg_mouse[0] = xpos;
- vg_mouse[1] = ypos;
+ vg_mouse[0] = xpos;
+ vg_mouse[1] = ypos;
}
void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset )
{
- vg_mouse_wheel[0] += xoffset;
- vg_mouse_wheel[1] += yoffset;
+ vg_mouse_wheel[0] += xoffset;
+ vg_mouse_wheel[1] += yoffset;
}
void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
{
- vg_window_x = w;
- vg_window_y = h;
+ vg_window_x = w;
+ vg_window_y = h;
#ifdef VG_FRAMEBUFFER_RESIZE
vg_framebuffer_resize(w,h);
static void vg_init( int argc, char *argv[], const char *window_name )
{
#ifdef VG_STEAM
- if( !sw_init() )
- return;
+ if( !sw_init() )
+ return;
#endif
-
- glfwInit();
- glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
- glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
- glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
- glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
-
+
+ glfwInit();
+ glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
+ glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
+ glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
+ glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
+
#ifdef VG_CAPTURE_MODE
- glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
+ glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
#else
- glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
+ glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
#endif
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
#if 0
glfwWindowHint(GLFW_SAMPLES,4);
#endif
-
- GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
-
- const GLFWvidmode *mode = glfwGetVideoMode( monitor_primary );
- glfwWindowHint( GLFW_RED_BITS, mode->redBits );
- glfwWindowHint( GLFW_GREEN_BITS, mode->greenBits );
- glfwWindowHint( GLFW_BLUE_BITS, mode->blueBits );
+
+ GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
+
+ const GLFWvidmode *mode = glfwGetVideoMode( monitor_primary );
+ glfwWindowHint( GLFW_RED_BITS, mode->redBits );
+ glfwWindowHint( GLFW_GREEN_BITS, mode->greenBits );
+ glfwWindowHint( GLFW_BLUE_BITS, mode->blueBits );
int refresh_rate = mode->refreshRate;
if( refresh_rate < 28 || refresh_rate >= 144 )
refresh_rate = 60;
- glfwWindowHint( GLFW_REFRESH_RATE, refresh_rate );
+ glfwWindowHint( GLFW_REFRESH_RATE, refresh_rate );
- if( !(vg_window = glfwCreateWindow( vg_window_x, vg_window_y,
+ if( !(vg_window = glfwCreateWindow( vg_window_x, vg_window_y,
window_name, NULL, NULL)) )
- vg_exiterr( "GLFW Failed to initialize" );
- else
- vg_register_exit( &glfwTerminate, "glfwTerminate" );
-
- glfwMakeContextCurrent( vg_window );
- glfwSwapInterval( 1 );
+ vg_exiterr( "GLFW Failed to initialize" );
+ else
+ vg_register_exit( &glfwTerminate, "glfwTerminate" );
+
+ glfwMakeContextCurrent( vg_window );
+ glfwSwapInterval( 1 );
glfwSetWindowSizeLimits( vg_window, 800, 600, GLFW_DONT_CARE,GLFW_DONT_CARE);
- glfwSetFramebufferSizeCallback( vg_window, vg_framebuffer_resize_callback );
+ glfwSetFramebufferSizeCallback( vg_window, vg_framebuffer_resize_callback );
- glfwSetCursorPosCallback( vg_window, vg_mouse_callback );
- glfwSetScrollCallback( vg_window, vg_scroll_callback );
-
- glfwSetCharCallback( vg_window, console_proc_wchar );
- glfwSetKeyCallback( vg_window, console_proc_key );
+ glfwSetCursorPosCallback( vg_window, vg_mouse_callback );
+ glfwSetScrollCallback( vg_window, vg_scroll_callback );
+
+ glfwSetCharCallback( vg_window, console_proc_wchar );
+ glfwSetKeyCallback( vg_window, console_proc_key );
#if 0
- glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
+ glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
#endif
- if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) )
- vg_exiterr( "Glad failed to initialize" );
-
- const unsigned char* glver = glGetString( GL_VERSION );
- vg_success( "Load setup complete, OpenGL version: %s\n", glver );
-
- vg_run_gfx_diagnostics();
-
- for( int id = 0; id <= GLFW_JOYSTICK_LAST; id ++ )
- {
- if( glfwJoystickIsGamepad( id ) )
- {
- vg_gamepad_name = glfwGetGamepadName( id );
- vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name );
-
- vg_gamepad_ready = 1;
- vg_gamepad_id = id;
-
- break;
- }
- }
-
- vg_lines_init();
- vg_register_exit( &vg_lines_free, "vg_lines_free" );
- ui_default_init();
- vg_register_exit( &ui_default_free, "UI" );
-
- vg_register();
- vg_register_exit( &vg_free, "vg_free" );
-
- if( vg_shaders_compile() )
- {
- vg_start();
-
- vg_console_init();
- vg_register_exit( &vg_console_free, "Console" );
-
- vg_audio_init();
- vg_register_exit( &vg_audio_free, "vg_audio_free" );
-
- vg_debugtools_setup();
-
+ if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) )
+ vg_exiterr( "Glad failed to initialize" );
+
+ const unsigned char* glver = glGetString( GL_VERSION );
+ vg_success( "Load setup complete, OpenGL version: %s\n", glver );
+
+ vg_run_gfx_diagnostics();
+
+ for( int id = 0; id <= GLFW_JOYSTICK_LAST; id ++ )
+ {
+ if( glfwJoystickIsGamepad( id ) )
+ {
+ vg_gamepad_name = glfwGetGamepadName( id );
+ vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name );
+
+ vg_gamepad_ready = 1;
+ vg_gamepad_id = id;
+
+ break;
+ }
+ }
+
+ vg_lines_init();
+ vg_register_exit( &vg_lines_free, "vg_lines_free" );
+ ui_default_init();
+ vg_register_exit( &ui_default_free, "UI" );
+
+ vg_register();
+ vg_register_exit( &vg_free, "vg_free" );
+
+ if( vg_shaders_compile() )
+ {
+ vg_start();
+
+ vg_console_init();
+ vg_register_exit( &vg_console_free, "Console" );
+
+ vg_audio_init();
+ vg_register_exit( &vg_audio_free, "vg_audio_free" );
+
+ vg_debugtools_setup();
+
/*
* Main gameloop
*/
- while( !glfwWindowShouldClose( vg_window ) )
- {
- v2_copy( (v2f){ 0.0f, 0.0f }, vg_mouse_wheel );
-
- glfwPollEvents();
-
- #ifdef VG_STEAM
- sw_event_loop();
- #endif
-
- vg_time_last = vg_time;
- vg_time = glfwGetTime();
- vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
-
- vg_update_inputs();
- vg_update();
- vg_render();
-
- vg_lines_drawall((float*)vg_pv);
-
- {
- ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
- ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1],
+ while( !glfwWindowShouldClose( vg_window ) )
+ {
+ v2_copy( (v2f){ 0.0f, 0.0f }, vg_mouse_wheel );
+
+ glfwPollEvents();
+
+ #ifdef VG_STEAM
+ sw_event_loop();
+ #endif
+
+ vg_time_last = vg_time;
+ vg_time = glfwGetTime();
+ vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
+
+ vg_update_inputs();
+ vg_update();
+ vg_render();
+
+ vg_lines_drawall((float*)vg_pv);
+
+ {
+ ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
+ ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1],
vg_get_button_state( "primary" ) );
-
- vg_ui();
- vg_console_draw();
- vg_debugtools_draw();
-
- ui_resolve( &ui_global_ctx );
- ui_draw( &ui_global_ctx, NULL );
- }
-
- glfwSwapBuffers( vg_window );
- VG_CHECK_GL();
- }
- }
-
- vg_exit();
+
+ vg_ui();
+ vg_console_draw();
+ vg_debugtools_draw();
+
+ ui_resolve( &ui_global_ctx );
+ ui_draw( &ui_global_ctx, NULL );
+ }
+
+ glfwSwapBuffers( vg_window );
+ VG_CHECK_GL();
+ }
+ }
+
+ vg_exit();
}
+#ifndef VG_3D
void vg_projection_update(void)
{
/*
* Reproject screenspace mouse into world
*/
- vg_mouse_ws[0] = vg_mouse[0];
- vg_mouse_ws[1] = vg_mouse[1];
- vg_mouse_ws[2] = 1.0f;
-
- vg_mouse_ws[0] = (2.0f * vg_mouse_ws[0]) / ((float)vg_window_x) - 1.0f;
- vg_mouse_ws[1] = -((2.0f * vg_mouse_ws[1]) / ((float)vg_window_y) - 1.0f);
-
- m3x3f inverse;
- m3x3_inv( vg_pv, inverse );
- m3x3_mulv( inverse, vg_mouse_ws, vg_mouse_ws );
+ vg_mouse_ws[0] = vg_mouse[0];
+ vg_mouse_ws[1] = vg_mouse[1];
+ vg_mouse_ws[2] = 1.0f;
+
+ vg_mouse_ws[0] = (2.0f * vg_mouse_ws[0]) / ((float)vg_window_x) - 1.0f;
+ vg_mouse_ws[1] = -((2.0f * vg_mouse_ws[1]) / ((float)vg_window_y) - 1.0f);
+
+ m3x3f inverse;
+ m3x3_inv( vg_pv, inverse );
+ m3x3_mulv( inverse, vg_mouse_ws, vg_mouse_ws );
}
+#endif
#endif
-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
-SHADER_DEFINE( vg_line_shader,
+#ifdef VG_3D
+ typedef v3f line_co;
+#else
+ typedef v2f line_co;
+#endif
- // VERTEX
+SHADER_DEFINE( vg_line_shader,
+#ifdef VG_3D
+ "uniform mat4 uPv;"
+ "layout (location=0) in vec3 a_co;"
+#else
+ "uniform mat3 uPv;"
"layout (location=0) in vec2 a_co;"
+#endif
"layout (location=1) in vec4 a_colour;"
- "uniform mat3 uPv;"
""
"out vec4 s_colour;"
""
"void main()"
"{"
+#ifdef VG_3D
+ " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
+#else
" vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
+#endif
" s_colour = a_colour;"
" gl_Position = vert_pos;"
"}",
{
struct vg_lines_vert
{
+#ifdef VG_3D
+ v3f co;
+#else
v2f co;
+#endif
+
u32 colour;
}
*buffer;
free( vg_lines.buffer );
}
-static void vg_lines_drawall(float* projection)
+static void vg_lines_drawall( float* projection )
{
SHADER_USE( vg_line_shader );
- glUniformMatrix3fv( SHADER_UNIFORM( vg_line_shader, "uPv" ), 1, GL_FALSE, projection );
-
+
+#ifdef VG_3D
+ glUniformMatrix4fv
+#else
+ glUniformMatrix3fv
+#endif
+ ( SHADER_UNIFORM( vg_line_shader, "uPv" ), 1, GL_FALSE, projection );
+
glBindVertexArray( vg_lines.vao );
glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
- glBufferSubData( GL_ARRAY_BUFFER, 0, vg_lines.draw_idx * sizeof(struct vg_lines_vert), vg_lines.buffer );
+ glBufferSubData( GL_ARRAY_BUFFER, 0, vg_lines.draw_idx *
+ sizeof(struct vg_lines_vert), vg_lines.buffer );
+
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBlendEquation( GL_FUNC_ADD );
vg_lines.draw_idx = 0;
}
-static void vg_line2( v2f from, v2f to, u32 fc, u32 tc )
+static void vg_line2( line_co from, line_co to, u32 fc, u32 tc )
{
- struct vg_lines_vert *v = vg_lines.buffer + vg_lines.draw_idx;
+ struct vg_lines_vert *v = &vg_lines.buffer[vg_lines.draw_idx];
+
+#ifdef VG_3D
+ v3_copy( from, v[0].co );
+ v3_copy( to, v[1].co );
+#else
v2_copy( from, v[0].co );
v2_copy( to, v[1].co );
+#endif
+
v[0].colour = fc;
v[1].colour = tc;
vg_lines.draw_idx += 2;
}
-static void vg_line( v2f from, v2f to, u32 colour )
+static void vg_line( line_co from, line_co to, u32 colour )
{
vg_line2( from, to, colour, colour );
}
-static void vg_line_box( v2f min, v2f max, u32 colour )
+static void vg_line_box( line_co min, line_co max, u32 colour )
{
+#ifdef VG_3D
+ /* TODO... */
+#else
vg_line( min, (v2f){min[0],max[1]}, colour );
vg_line( (v2f){min[0],max[1]}, max, colour );
vg_line( max, (v2f){max[0],min[1]}, colour );
vg_line( (v2f){max[0],min[1]}, min, colour );
+#endif
}
return vg_minf( max, vg_maxf( a, min ) );
}
+static inline float vg_signf( float a )
+{
+ return a < 0.0f? -1.0f: 1.0f;
+}
+
#define VG_MIN( A, B ) ((A)<(B)?(A):(B))
#define VG_MAX( A, B ) ((A)>(B)?(A):(B))
a[2] = v;
}
+static inline void v3_floor( v3f a, v3f b )
+{
+ b[0] = floorf( a[0] );
+ b[1] = floorf( a[1] );
+ b[2] = floorf( a[2] );
+}
+
+static inline void v3_negate( v3f a, v3f b )
+{
+ b[0] = -a[0];
+ b[1] = -a[1];
+ b[2] = -a[2];
+}
+
/*
* Vector 4
*/
a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f;
}
+static inline void v4_muladds( v3f a, v3f b, float s, v3f d )
+{
+ d[0] = a[0]+b[0]*s;
+ d[1] = a[1]+b[1]*s;
+ d[2] = a[2]+b[2]*s;
+ d[3] = a[3]+b[3]*s;
+}
+
/*
* Matrix 2x2
*/
m4x3_mul( m, t, m );
}
+static inline void m4x3_expand( m4x3f m, m4x4f d )
+{
+ v3_copy( m[0], d[0] );
+ v3_copy( m[1], d[1] );
+ v3_copy( m[2], d[2] );
+ v3_copy( m[3], d[3] );
+ d[0][3] = 0.0f;
+ d[1][3] = 0.0f;
+ d[2][3] = 0.0f;
+ d[3][3] = 1.0f;
+}
+
static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
{
v3f v;
m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } );
}
+/*
+ * Matrix 4x4
+ */
+
+#define M4X4_IDENTITY {{1.0f, 0.0f, 0.0f, 0.0f },\
+ { 0.0f, 1.0f, 0.0f, 0.0f },\
+ { 0.0f, 0.0f, 1.0f, 0.0f },\
+ { 0.0f, 0.0f, 0.0f, 1.0f }}
+
+static void m4x4_projection( m4x4f m, float angle,
+ float ratio, float near, float far )
+{
+ float scale = tanf( angle * 0.5f * VG_PIf / 180.0f ) * near,
+ r = ratio * scale,
+ l = -r,
+ t = scale,
+ b = -t;
+
+ m[0][0] = 2.0f * near / (r - l);
+ m[0][1] = 0.0f;
+ m[0][2] = 0.0f;
+ m[0][3] = 0.0f;
+ m[1][0] = 0.0f;
+ m[1][1] = 2.0f * near / (t - b);
+ m[1][2] = 0.0f;
+ m[1][3] = 0.0f;
+ m[2][0] = (r + l) / (r - l);
+ m[2][1] = (t + b) / (t - b);
+ m[2][2] = -(far + near) / (far - near);
+ m[2][3] = -1.0f;
+ m[3][0] = 0.0f;
+ m[3][1] = 0.0f;
+ m[3][2] = -2.0f * far * near / (far - near);
+ m[3][3] = 0.0f;
+}
+
+static void m4x4_translate( m4x4f m, v3f v )
+{
+ v4_muladds( m[3], m[0], v[0], m[3] );
+ v4_muladds( m[3], m[1], v[1], m[3] );
+ v4_muladds( m[3], m[2], v[2], m[3] );
+}
+
+static inline void m4x4_copy( m4x4f a, m4x4f b )
+{
+ v4_copy( a[0], b[0] );
+ v4_copy( a[1], b[1] );
+ v4_copy( a[2], b[2] );
+ v4_copy( a[3], b[3] );
+}
+
+static inline void m4x4_identity( m4x4f a )
+{
+ m4x4f id = M4X4_IDENTITY;
+ m4x4_copy( id, a );
+}
+
+static inline void m4x4_mul( m4x4f a, m4x4f b, m4x4f d )
+{
+ float a00 = a[0][0], a01 = a[0][1], a02 = a[0][2], a03 = a[0][3],
+ a10 = a[1][0], a11 = a[1][1], a12 = a[1][2], a13 = a[1][3],
+ a20 = a[2][0], a21 = a[2][1], a22 = a[2][2], a23 = a[2][3],
+ a30 = a[3][0], a31 = a[3][1], a32 = a[3][2], a33 = a[3][3],
+
+ b00 = b[0][0], b01 = b[0][1], b02 = b[0][2], b03 = b[0][3],
+ b10 = b[1][0], b11 = b[1][1], b12 = b[1][2], b13 = b[1][3],
+ b20 = b[2][0], b21 = b[2][1], b22 = b[2][2], b23 = b[2][3],
+ b30 = b[3][0], b31 = b[3][1], b32 = b[3][2], b33 = b[3][3];
+
+ d[0][0] = a00*b00 + a10*b01 + a20*b02 + a30*b03;
+ d[0][1] = a01*b00 + a11*b01 + a21*b02 + a31*b03;
+ d[0][2] = a02*b00 + a12*b01 + a22*b02 + a32*b03;
+ d[0][3] = a03*b00 + a13*b01 + a23*b02 + a33*b03;
+ d[1][0] = a00*b10 + a10*b11 + a20*b12 + a30*b13;
+ d[1][1] = a01*b10 + a11*b11 + a21*b12 + a31*b13;
+ d[1][2] = a02*b10 + a12*b11 + a22*b12 + a32*b13;
+ d[1][3] = a03*b10 + a13*b11 + a23*b12 + a33*b13;
+ d[2][0] = a00*b20 + a10*b21 + a20*b22 + a30*b23;
+ d[2][1] = a01*b20 + a11*b21 + a21*b22 + a31*b23;
+ d[2][2] = a02*b20 + a12*b21 + a22*b22 + a32*b23;
+ d[2][3] = a03*b20 + a13*b21 + a23*b22 + a33*b23;
+ d[3][0] = a00*b30 + a10*b31 + a20*b32 + a30*b33;
+ d[3][1] = a01*b30 + a11*b31 + a21*b32 + a31*b33;
+ d[3][2] = a02*b30 + a12*b31 + a22*b32 + a32*b33;
+ d[3][3] = a03*b30 + a13*b31 + a23*b32 + a33*b33;
+}
+
/*
* Planes (double precision)
*/