From 4bbcd5daef1bd5bc5a55e7a3caafea31f66ec127 Mon Sep 17 00:00:00 2001 From: mittorn Date: Thu, 4 Apr 2019 01:32:06 +0700 Subject: [PATCH] ref_soft: Use r_math, this will fix beamfollow and glow sprites --- r_context.c | 4 - r_local.h | 5 +- r_main.c | 16 +++- r_math.c | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++++ r_sprite.c | 1 + r_triapi.c | 7 +- 6 files changed, 285 insertions(+), 15 deletions(-) create mode 100644 r_math.c diff --git a/r_context.c b/r_context.c index 12257fde..b9e32f1d 100644 --- a/r_context.c +++ b/r_context.c @@ -447,10 +447,6 @@ byte *Mod_GetCurrentVis() return NULL; } -void R_ScreenToWorld(const vec3_t screen, vec3_t point) -{ - -} void GL_SetupAttributes( int safegl ) { gEngfuncs.Con_Reportf( "Creating an extended GL context for debug...\n" ); diff --git a/r_local.h b/r_local.h index 5f7cd8f9..7906dfc5 100644 --- a/r_local.h +++ b/r_local.h @@ -196,6 +196,7 @@ typedef struct int cached_contents; // in water int cached_waterlevel; // was in water + float farClip; float skyMins[2][6]; float skyMaxs[2][6]; @@ -474,7 +475,7 @@ void R_FindViewLeaf( void ); void R_PushScene( void ); void R_PopScene( void ); void R_DrawFog( void ); -#if 0 + // // gl_rmath.c // @@ -492,7 +493,7 @@ void Matrix4x4_CreateScale3( matrix4x4 out, float x, float y, float z ); void Matrix4x4_CreateProjection(matrix4x4 out, float xMax, float xMin, float yMax, float yMin, float zNear, float zFar); void Matrix4x4_CreateOrtho(matrix4x4 m, float xLeft, float xRight, float yBottom, float yTop, float zNear, float zFar); void Matrix4x4_CreateModelview( matrix4x4 out ); - +#if 0 // // gl_rmisc.c // diff --git a/r_main.c b/r_main.c index f2ce33bd..bc788275 100644 --- a/r_main.c +++ b/r_main.c @@ -240,7 +240,7 @@ static int R_TransEntityCompare( const cl_entity_t **a, const cl_entity_t **b ) return 0; } -#if 0 +#if 1 /* =============== @@ -465,7 +465,7 @@ R_SetupFrustum void R_SetupFrustum( void ) { #if 1 - ref_overview_t *ov = gEngfuncs.GetOverviewParms(); + //ref_overview_t *ov = gEngfuncs.GetOverviewParms(); /*if( RP_NORMALPASS() && ( ENGINE_GET_PARM( PARM_WATER_LEVEL ) >= 3 )) { @@ -497,8 +497,8 @@ R_SetupProjectionMatrix */ static void R_SetupProjectionMatrix( matrix4x4 m ) { -#if 0 - GLdouble xMin, xMax, yMin, yMax, zNear, zFar; +#if 1 + double xMin, xMax, yMin, yMax, zNear, zFar; if( RI.drawOrtho ) { @@ -529,7 +529,7 @@ R_SetupModelviewMatrix */ static void R_SetupModelviewMatrix( matrix4x4 m ) { -#if 0 +#if 1 Matrix4x4_CreateModelview( m ); Matrix4x4_ConcatRotate( m, -RI.viewangles[2], 1, 0, 0 ); Matrix4x4_ConcatRotate( m, -RI.viewangles[0], 0, 1, 0 ); @@ -1568,6 +1568,7 @@ void R_MarkLeaves (void) #endif + /* ================ R_RenderScene @@ -1593,6 +1594,11 @@ void R_RenderScene( void ) R_SetupFrame(); R_PushDlights(); + R_SetupModelviewMatrix( RI.worldviewMatrix ); + R_SetupProjectionMatrix( RI.projectionMatrix ); + + Matrix4x4_Concat( RI.worldviewProjectionMatrix, RI.projectionMatrix, RI.worldviewMatrix ); + // R_SetupGL( true ); //R_Clear( ~0 ); diff --git a/r_math.c b/r_math.c new file mode 100644 index 00000000..c60e09fa --- /dev/null +++ b/r_math.c @@ -0,0 +1,267 @@ +/* +gl_rmath.c - renderer mathlib +Copyright (C) 2010 Uncle Mike + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + +#include "r_local.h" +#include "mathlib.h" + +/* +======================================================================== + + Matrix4x4 operations (private to renderer) + +======================================================================== +*/ +void Matrix4x4_Concat( matrix4x4 out, const matrix4x4 in1, const matrix4x4 in2 ) +{ + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0] + in1[0][3] * in2[3][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1] + in1[0][3] * in2[3][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2] + in1[0][3] * in2[3][2]; + out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3] * in2[3][3]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0] + in1[1][3] * in2[3][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1] + in1[1][3] * in2[3][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2] + in1[1][3] * in2[3][2]; + out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3] * in2[3][3]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0] + in1[2][3] * in2[3][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1] + in1[2][3] * in2[3][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2] + in1[2][3] * in2[3][2]; + out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3] * in2[3][3]; + out[3][0] = in1[3][0] * in2[0][0] + in1[3][1] * in2[1][0] + in1[3][2] * in2[2][0] + in1[3][3] * in2[3][0]; + out[3][1] = in1[3][0] * in2[0][1] + in1[3][1] * in2[1][1] + in1[3][2] * in2[2][1] + in1[3][3] * in2[3][1]; + out[3][2] = in1[3][0] * in2[0][2] + in1[3][1] * in2[1][2] + in1[3][2] * in2[2][2] + in1[3][3] * in2[3][2]; + out[3][3] = in1[3][0] * in2[0][3] + in1[3][1] * in2[1][3] + in1[3][2] * in2[2][3] + in1[3][3] * in2[3][3]; +} + +/* +================ +Matrix4x4_CreateProjection + +NOTE: produce quake style world orientation +================ +*/ +void Matrix4x4_CreateProjection( matrix4x4 out, float xMax, float xMin, float yMax, float yMin, float zNear, float zFar ) +{ + out[0][0] = ( 2.0f * zNear ) / ( xMax - xMin ); + out[1][1] = ( 2.0f * zNear ) / ( yMax - yMin ); + out[2][2] = -( zFar + zNear ) / ( zFar - zNear ); + out[3][3] = out[0][1] = out[1][0] = out[3][0] = out[0][3] = out[3][1] = out[1][3] = 0.0f; + + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[0][2] = ( xMax + xMin ) / ( xMax - xMin ); + out[1][2] = ( yMax + yMin ) / ( yMax - yMin ); + out[3][2] = -1.0f; + out[2][3] = -( 2.0f * zFar * zNear ) / ( zFar - zNear ); +} + +void Matrix4x4_CreateOrtho( matrix4x4 out, float xLeft, float xRight, float yBottom, float yTop, float zNear, float zFar ) +{ + out[0][0] = 2.0f / (xRight - xLeft); + out[1][1] = 2.0f / (yTop - yBottom); + out[2][2] = -2.0f / (zFar - zNear); + out[3][3] = 1.0f; + out[0][1] = out[0][2] = out[1][0] = out[1][2] = out[3][0] = out[3][1] = out[3][2] = 0.0f; + + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[0][3] = -(xRight + xLeft) / (xRight - xLeft); + out[1][3] = -(yTop + yBottom) / (yTop - yBottom); + out[2][3] = -(zFar + zNear) / (zFar - zNear); +} + +/* +================ +Matrix4x4_CreateModelview + +NOTE: produce quake style world orientation +================ +*/ +void Matrix4x4_CreateModelview( matrix4x4 out ) +{ + out[0][0] = out[1][1] = out[2][2] = 0.0f; + out[3][0] = out[0][3] = 0.0f; + out[3][1] = out[1][3] = 0.0f; + out[3][2] = out[2][3] = 0.0f; + out[3][3] = 1.0f; + out[1][0] = out[0][2] = out[2][1] = 0.0f; + out[2][0] = out[0][1] = -1.0f; + out[1][2] = 1.0f; +} + +void Matrix4x4_ToArrayFloatGL( const matrix4x4 in, float out[16] ) +{ + out[ 0] = in[0][0]; + out[ 1] = in[1][0]; + out[ 2] = in[2][0]; + out[ 3] = in[3][0]; + out[ 4] = in[0][1]; + out[ 5] = in[1][1]; + out[ 6] = in[2][1]; + out[ 7] = in[3][1]; + out[ 8] = in[0][2]; + out[ 9] = in[1][2]; + out[10] = in[2][2]; + out[11] = in[3][2]; + out[12] = in[0][3]; + out[13] = in[1][3]; + out[14] = in[2][3]; + out[15] = in[3][3]; +} + +void Matrix4x4_FromArrayFloatGL( matrix4x4 out, const float in[16] ) +{ + out[0][0] = in[0]; + out[1][0] = in[1]; + out[2][0] = in[2]; + out[3][0] = in[3]; + out[0][1] = in[4]; + out[1][1] = in[5]; + out[2][1] = in[6]; + out[3][1] = in[7]; + out[0][2] = in[8]; + out[1][2] = in[9]; + out[2][2] = in[10]; + out[3][2] = in[11]; + out[0][3] = in[12]; + out[1][3] = in[13]; + out[2][3] = in[14]; + out[3][3] = in[15]; +} + +void Matrix4x4_CreateTranslate( matrix4x4 out, float x, float y, float z ) +{ + out[0][0] = 1.0f; + out[0][1] = 0.0f; + out[0][2] = 0.0f; + out[0][3] = x; + out[1][0] = 0.0f; + out[1][1] = 1.0f; + out[1][2] = 0.0f; + out[1][3] = y; + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[2][2] = 1.0f; + out[2][3] = z; + out[3][0] = 0.0f; + out[3][1] = 0.0f; + out[3][2] = 0.0f; + out[3][3] = 1.0f; +} + +void Matrix4x4_CreateRotate( matrix4x4 out, float angle, float x, float y, float z ) +{ + float len, c, s; + + len = x * x + y * y + z * z; + if( len != 0.0f ) len = 1.0f / sqrt( len ); + x *= len; + y *= len; + z *= len; + + angle *= (-M_PI / 180.0f); + SinCos( angle, &s, &c ); + + out[0][0]=x * x + c * (1 - x * x); + out[0][1]=x * y * (1 - c) + z * s; + out[0][2]=z * x * (1 - c) - y * s; + out[0][3]=0.0f; + out[1][0]=x * y * (1 - c) - z * s; + out[1][1]=y * y + c * (1 - y * y); + out[1][2]=y * z * (1 - c) + x * s; + out[1][3]=0.0f; + out[2][0]=z * x * (1 - c) + y * s; + out[2][1]=y * z * (1 - c) - x * s; + out[2][2]=z * z + c * (1 - z * z); + out[2][3]=0.0f; + out[3][0]=0.0f; + out[3][1]=0.0f; + out[3][2]=0.0f; + out[3][3]=1.0f; +} + +void Matrix4x4_CreateScale( matrix4x4 out, float x ) +{ + out[0][0] = x; + out[0][1] = 0.0f; + out[0][2] = 0.0f; + out[0][3] = 0.0f; + out[1][0] = 0.0f; + out[1][1] = x; + out[1][2] = 0.0f; + out[1][3] = 0.0f; + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[2][2] = x; + out[2][3] = 0.0f; + out[3][0] = 0.0f; + out[3][1] = 0.0f; + out[3][2] = 0.0f; + out[3][3] = 1.0f; +} + +void Matrix4x4_CreateScale3( matrix4x4 out, float x, float y, float z ) +{ + out[0][0] = x; + out[0][1] = 0.0f; + out[0][2] = 0.0f; + out[0][3] = 0.0f; + out[1][0] = 0.0f; + out[1][1] = y; + out[1][2] = 0.0f; + out[1][3] = 0.0f; + out[2][0] = 0.0f; + out[2][1] = 0.0f; + out[2][2] = z; + out[2][3] = 0.0f; + out[3][0] = 0.0f; + out[3][1] = 0.0f; + out[3][2] = 0.0f; + out[3][3] = 1.0f; +} + +void Matrix4x4_ConcatTranslate( matrix4x4 out, float x, float y, float z ) +{ + matrix4x4 base, temp; + + Matrix4x4_Copy( base, out ); + Matrix4x4_CreateTranslate( temp, x, y, z ); + Matrix4x4_Concat( out, base, temp ); +} + +void Matrix4x4_ConcatRotate( matrix4x4 out, float angle, float x, float y, float z ) +{ + matrix4x4 base, temp; + + Matrix4x4_Copy( base, out ); + Matrix4x4_CreateRotate( temp, angle, x, y, z ); + Matrix4x4_Concat( out, base, temp ); +} + +void Matrix4x4_ConcatScale( matrix4x4 out, float x ) +{ + matrix4x4 base, temp; + + Matrix4x4_Copy( base, out ); + Matrix4x4_CreateScale( temp, x ); + Matrix4x4_Concat( out, base, temp ); +} + +void Matrix4x4_ConcatScale3( matrix4x4 out, float x, float y, float z ) +{ + matrix4x4 base, temp; + + Matrix4x4_Copy( base, out ); + Matrix4x4_CreateScale3( temp, x, y, z ); + Matrix4x4_Concat( out, base, temp ); +} diff --git a/r_sprite.c b/r_sprite.c index 91fd76e3..f28816a0 100644 --- a/r_sprite.c +++ b/r_sprite.c @@ -699,6 +699,7 @@ qboolean R_SpriteOccluded( cl_entity_t *e, vec3_t origin, float *pscale ) { float blend; vec3_t v; + //return false; TriWorldToScreen( origin, v ); diff --git a/r_triapi.c b/r_triapi.c index db971c94..386fb1c1 100644 --- a/r_triapi.c +++ b/r_triapi.c @@ -345,7 +345,7 @@ void TriVertex3f( float x, float y, float z ) } #endif } -void R_AliasWorldToScreen( const float *v, float *out ); + /* ============= TriWorldToScreen @@ -357,15 +357,14 @@ int TriWorldToScreen( const float *world, float *screen ) { int retval; - R_AliasWorldToScreen( world, screen ); - retval = 0; - + retval = R_WorldToScreen( world, screen ); screen[0] = 0.5f * screen[0] * (float)RI.viewport[2]; screen[1] = -0.5f * screen[1] * (float)RI.viewport[3]; screen[0] += 0.5f * (float)RI.viewport[2]; screen[1] += 0.5f * (float)RI.viewport[3]; + return retval; }