Ver Fonte

*Renamed the hud and moved it to ui dir. The ui will keep all the user interface
*Rewrote the IntToStr
*Adding extension.*. This is for extensions (aka plugins) and will be used instead of scripting language. Will see how it goes
*Parallax mapping code in ms_mp_generic.glsl. Not tested at all

Panagiotis Christopoulos Charitos há 16 anos atrás
pai
commit
5eac1ac66e
8 ficheiros alterados com 118 adições e 47 exclusões
  1. 1 1
      ToDo.txt
  2. 26 20
      shaders/ms_mp_generic.glsl
  3. 8 8
      src/main.cpp
  4. 33 0
      src/resources/extension.cpp
  5. 24 0
      src/resources/extension.h
  6. 2 2
      src/ui/ui.cpp
  7. 3 3
      src/ui/ui.h
  8. 21 13
      src/utility/common.cpp

+ 1 - 1
ToDo.txt

@@ -1 +1 @@
-Make controllers generic
+create ui dir, rename hud, put it to ui/

+ 26 - 20
shaders/ms_mp_generic.glsl

@@ -37,8 +37,7 @@ varying vec3 normal_v2f;
 varying vec2 tex_coords_v2f;
 varying vec3 tangent_v2f;
 varying float w_v2f;
-varying vec3 eye_v2f; ///< In tangent space
-varying vec3 vert_pos_eye_space_v2f;
+varying vec3 vert_pos_eye_space_v2f; ///< For env mapping. AKA view_vector
 
 
 
@@ -89,24 +88,9 @@ void main()
 	#endif
 
 
-	#if defined( _ENVIRONMENT_MAPPING_ )
+	#if defined( _ENVIRONMENT_MAPPING_ ) && defined( _PARALLAX_MAPPING_ )
 		vert_pos_eye_space_v2f = vec3( gl_ModelViewMatrix * vec4(position, 1.0) );
 	#endif
-
-
-	#if defined( _PARALLAX_MAPPING_ )
-		/*vec3 t = gl_NormalMatrix * tangent.xyz;
-		vec3 n = normal;
-		vec3 b = cross( n, t )  tangent.w;
-
-		vec3 eye_pos = gl_Position.xyz - gl_ModelViewMatrixInverse[3].xyz;
-		eye_pos = eye_pos - ( gl_ModelViewMatrixInverse * gl_Vertex ).xyz;
-
-		mat3 tbn_mat = mat3( t, b, n );
-		eye = tbn_mat * eye_pos;
-		//eye.y = -eye.y;
-		//eye.x = -eye.x;*/
-	#endif
 }
 
 
@@ -159,7 +143,7 @@ void main()
 	// The code below reads the height map, makes some calculations and returns a new tex_coords                                        =
 	//===================================================================================================================================
 	#if defined( _PARALLAX_MAPPING_ )
-		const float _scale = 0.04;
+		/*const float _scale = 0.04;
 		const float _bias = scale * 0.4;
 
 		vec3 _norm_eye = normalize( eye );
@@ -167,7 +151,29 @@ void main()
 		float _h = texture2D( height_map, tex_coords_v2f ).r;
 		float _height = _scale * _h - _bias;
 
-		vec2 _super_tex_coords_v2f = _height * _norm_eye.xy + tex_coords_v2f;
+		vec2 _super_tex_coords_v2f = _height * _norm_eye.xy + tex_coords_v2f;*/
+		const float maxStepCount = 100.0;
+		float nSteps = maxStepCount * length(vert_pos_eye_space_v2f.xy);
+
+		vec3 dir = vert_pos_eye_space_v2f;
+		dir.xy /= 8.0;
+		dir /= -nSteps * dir.z;
+
+		float diff0, diff1 = 1.0 - texture2D( height_map, tex_coords_v2f ).a;
+		if( diff1 > 0.0 )
+		{
+			do 
+			{
+				tex_coords_v2f += dir;
+
+				diff0 = diff1;
+				diff1 = texCoord.z - texture2D(Bump, tex_coords_v2f.xy ).w;
+			} while( diff1 > 0.0 );
+
+			tex_coords_v2f.xy += (diff1 / (diff0 - diff1)) * dir.xy;
+		}
+
+		vec2 _super_tex_coords = texCoord.xy;
 	#else
 		#define _super_tex_coords tex_coords_v2f
 	#endif

+ 8 - 8
src/main.cpp

@@ -8,7 +8,7 @@
 #include "camera.h"
 #include "gmath.h"
 #include "renderer.h"
-#include "hud.h"
+#include "ui.h"
 #include "app.h"
 #include "particles.h"
 #include "texture.h"
@@ -53,7 +53,7 @@ void Init()
 	uint ticks = app::GetTicks();
 
 	r::Init();
-	hud::Init();
+	ui::Init();
 
 	// camera
 	main_cam = new camera_t( r::aspect_ratio*ToRad(60.0), ToRad(60.0), 0.5, 100.0 );
@@ -180,12 +180,12 @@ int main( int /*argc*/, char* /*argv*/[] )
 		//map.octree.root->bounding_box.Render();
 
 		// print some debug stuff
-		hud::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
-		hud::SetPos( -0.98, 0.95 );
-		hud::SetFontWidth( 0.03 );
-		hud::Printf( "frame:%d time:%dms\n", r::frames_num, app::GetTicks()-ticks_ );
-		//hud::Print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
-		hud::Printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translation_wspace.x, mover->translation_wspace.y, mover->translation_wspace.z,
+		ui::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
+		ui::SetPos( -0.98, 0.95 );
+		ui::SetFontWidth( 0.03 );
+		ui::Printf( "frame:%d time:%dms\n", r::frames_num, app::GetTicks()-ticks_ );
+		//ui::Print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
+		ui::Printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translation_wspace.x, mover->translation_wspace.y, mover->translation_wspace.z,
 								 ToDegrees(euler_t(mover->rotation_wspace).x), ToDegrees(euler_t(mover->rotation_wspace).y), ToDegrees(euler_t(mover->rotation_wspace).z) );
 
 		if( i::keys[SDLK_ESCAPE] ) break;

+ 33 - 0
src/resources/extension.cpp

@@ -0,0 +1,33 @@
+#include <dlfcn.h>
+#include "extension.h"
+
+
+bool extension_t::Load( const char* filename )
+{
+	// load libary
+	lib_handle = dlopen( filename, RTLD_LAZY );
+	if( lib_handle == NULL ) 
+	{
+		ERROR( "File \"" << filename << "\": " << dlerror() );
+		return false;
+	}
+	
+	// get FooBar
+	foobar_ptr = (int(*)(void*))( dlsym(lib_handle, "FooBar") );
+	if( foobar_ptr == NULL )  
+	{
+		ERROR( "File \"" << filename << "\": \"FooBar\" entry symbol not found: " << dlerror() );
+		return false;
+	}
+	
+	return true;
+}
+
+
+void extension_t::Unload()
+{
+	DEBUG_ERR( lib_handle==NULL || foobar_ptr==NULL );
+	dlclose( lib_handle );
+	lib_handle = NULL;
+	foobar_ptr = NULL;
+}

+ 24 - 0
src/resources/extension.h

@@ -0,0 +1,24 @@
+#ifndef _EXTENSION_H_
+#define _EXTENSION_H_
+
+#include "common.h"
+#include "resource.h"
+
+
+/// Extension resource
+class extension_t: public resource_t
+{
+	private:
+		void* lib_handle;
+		int(*foobar_ptr)(void*);
+	
+	public:
+		extension_t(): lib_handle(NULL), foobar_ptr(NULL) {}
+		~extension_t() {}
+		bool Load( const char* filename );
+		void Unload();
+		template<typename type_t> int FooBar( type_t* ptr ) { DEBUG_ERR(foobar_ptr==NULL); return (*foobar_ptr)( reinterpret_cast<type_t*>(ptr) ); }
+};
+
+
+#endif

+ 2 - 2
src/uncategorized/hud.cpp → src/ui/ui.cpp

@@ -1,11 +1,11 @@
 #include <stdio.h>
 #include <stdarg.h>
-#include "hud.h"
+#include "ui.h"
 #include "renderer.h"
 #include "texture.h"
 #include "resource.h"
 
-namespace hud {
+namespace ui {
 
 
 /*

+ 3 - 3
src/uncategorized/hud.h → src/ui/ui.h

@@ -1,5 +1,5 @@
-#ifndef _HUD_H_
-#define _HUD_H_
+#ifndef _UI_H_
+#define _UI_H_
 
 #include "common.h"
 
@@ -7,7 +7,7 @@ namespace m {
 	class vec4_t;
 }
 
-namespace hud { // begin namespace
+namespace ui { // begin namespace
 
 
 extern void Init(); // exec after init SDL

+ 21 - 13
src/utility/common.cpp

@@ -14,25 +14,33 @@ IntToStr
 */
 string IntToStr( int i )
 {
-	string s = "";
-	if( i == 0 )
-	{
-		s = "0";
-		return s;
-	}
+	char str [256];
+	char* pstr = str + ( (sizeof(str)/sizeof(char)) - 1 );
+	bool negative = false;
+	
+	*pstr = '\0';
+	
 	if( i < 0 )
 	{
-		s += '-';
 		i = -i;
+		negative = true;
 	}
-	int count = log10(i);
-	while( count >= 0 )
+	
+	do
 	{
-		s += ('0' + i/pow(10.0, count));
-		i -= static_cast<int>(i/pow(10.0,count)) * static_cast<int>(pow(10.0,count));
-		count--;
+		--pstr;
+		int remain = i % 10;
+		i = i / 10;
+		*pstr = '0' + remain;
+	} while( i != 0 );
+	
+	if( negative )
+	{
+		--pstr;
+		*pstr = '-';
 	}
-	return s;
+	
+	return string(pstr);
 }