main_projection_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "common.h"
  5. #include "input.h"
  6. #include "camera.h"
  7. #include "math.h"
  8. #include "renderer.h"
  9. #include "hud.h"
  10. #include "handlers.h"
  11. #include "particles.h"
  12. #include "primitives.h"
  13. #include "texture.h"
  14. #include "geometry.h"
  15. #include "shaders.h"
  16. #include "lights.h"
  17. #include "collision.h"
  18. #include "model.h"
  19. #include "spatial.h"
  20. #include "material.h"
  21. #include "assets.h"
  22. #include "level.h"
  23. #include "scanner.h"
  24. #include "fbos.h"
  25. #include "skybox.h"
  26. camera_t main_cam( r::aspect_ratio*ToRad(60.0), ToRad(60.0), 0.5, 200.0 );
  27. class cube_t: public object_t
  28. {
  29. public:
  30. cube_t()
  31. {
  32. local_translation = vec3_t( 3.2, 0.05, 1.75 );
  33. local_scale = 2.0;
  34. }
  35. void Render()
  36. {
  37. glPushMatrix();
  38. r::MultMatrix( world_transformation );
  39. r::Color4( vec4_t(1.0, 1.0, 0.0, 1) );
  40. r::RenderCube( true );
  41. glPopMatrix();
  42. }
  43. }cube;
  44. class shpere_t: public object_t
  45. {
  46. public:
  47. shpere_t()
  48. {
  49. local_translation = vec3_t( -1.2, 1.05, -1.75 );
  50. local_scale = 1.5;
  51. }
  52. void Render()
  53. {
  54. glPushMatrix();
  55. r::MultMatrix( world_transformation );
  56. r::Color4( vec4_t(1.0, 0.5, 0.25, 1) );
  57. r::RenderSphere( 1.0, 32 );
  58. glPopMatrix();
  59. }
  60. }sphere;
  61. class flore_t: public object_t
  62. {
  63. public:
  64. flore_t()
  65. {
  66. local_scale = 10.0;
  67. }
  68. void Render()
  69. {
  70. glPushMatrix();
  71. r::MultMatrix( world_transformation );
  72. r::Color4( vec4_t(1.0, 1., 1., 1) );
  73. glNormal3fv( &vec3_t( 0.0, 1.0, 0.0 )[0] );
  74. glBegin( GL_QUADS );
  75. glVertex3f( 1.0, 0.0, -1.0 );
  76. glVertex3f( -1.0, 0.0, -1.0 );
  77. glVertex3f( -1.0, 0.0, 1.0 );
  78. glVertex3f( 1.0, 0.0, 1.0 );
  79. glEnd();
  80. glPopMatrix();
  81. }
  82. }flore;
  83. vec3_t point( 1.0, 2.0, 0.5 );
  84. skybox_t skybox;
  85. /*
  86. =======================================================================================================================================
  87. main =
  88. =======================================================================================================================================
  89. */
  90. int main( int argc, char* argv[] )
  91. {
  92. //mem::Enable( mem::THREADS );
  93. MathSanityChecks();
  94. hndl::InitWindow( r::w, r::h, "GODlike's Engine" );
  95. scn::Init();
  96. r::Init();
  97. hud::Init();
  98. main_cam.MoveLocalY( 2.5 );
  99. main_cam.MoveLocalZ( 5.0f );
  100. lvl::Register( (object_t*)&cube );
  101. lvl::Register( (object_t*)&sphere );
  102. lvl::Register( (object_t*)&flore );
  103. lvl::Register( &main_cam );
  104. const char* skybox_fnames [] = { "env/hellsky4_forward.tga", "env/hellsky4_back.tga", "env/hellsky4_left.tga", "env/hellsky4_right.tga",
  105. "env/hellsky4_up.tga", "env/hellsky4_down.tga" };
  106. skybox.Load( skybox_fnames );
  107. /************************************************************************************************************************************
  108. * MAIN LOOP *
  109. *************************************************************************************************************************************/
  110. do
  111. {
  112. StartBench();
  113. i::HandleEvents();
  114. r::PrepareNextFrame();
  115. float dist = 0.2;
  116. float ang = ToRad(3.0);
  117. float scale = 0.01;
  118. // move the camera
  119. static object_t* mover = &main_cam;
  120. if( i::keys[ SDLK_1 ] ) mover = &main_cam;
  121. if( i::keys[SDLK_a] ) mover->MoveLocalX( -dist );
  122. if( i::keys[SDLK_d] ) mover->MoveLocalX( dist );
  123. if( i::keys[SDLK_LSHIFT] ) mover->MoveLocalY( dist );
  124. if( i::keys[SDLK_SPACE] ) mover->MoveLocalY( -dist );
  125. if( i::keys[SDLK_w] ) mover->MoveLocalZ( -dist );
  126. if( i::keys[SDLK_s] ) mover->MoveLocalZ( dist );
  127. if( i::keys[SDLK_UP] ) mover->RotateLocalX( ang );
  128. if( i::keys[SDLK_DOWN] ) mover->RotateLocalX( -ang );
  129. if( i::keys[SDLK_LEFT] ) mover->RotateLocalY( ang );
  130. if( i::keys[SDLK_RIGHT] ) mover->RotateLocalY( -ang );
  131. if( i::keys[SDLK_q] ) mover->RotateLocalZ( ang );
  132. if( i::keys[SDLK_e] ) mover->RotateLocalZ( -ang );
  133. if( i::keys[SDLK_PAGEUP] ) mover->local_scale += scale ;
  134. if( i::keys[SDLK_PAGEDOWN] ) mover->local_scale -= scale ;
  135. if( i::keys[SDLK_k] ) main_cam.LookAtPoint( point );
  136. mover->local_rotation.Reorthogonalize();
  137. lvl::InterpolateAllModels();
  138. lvl::UpdateAllWorldTrfs();
  139. lvl::UpdateAllCameras();
  140. r::SetProjectionViewMatrices( main_cam );
  141. r::SetViewport( 0, 0, r::w, r::h );
  142. glClearColor( 0.1f, 0.1f, 0.2f, 0.0f );
  143. glClear( GL_DEPTH_BUFFER_BIT );
  144. skybox.Render( main_cam.view_mat.GetRotationPart() );
  145. r::NoShaders();
  146. glDisable( GL_BLEND );
  147. glEnable( GL_DEPTH_TEST );
  148. glDisable( GL_TEXTURE_2D );
  149. lvl::RenderAll();
  150. r::RenderGrid();
  151. glPointSize( 10.0 );
  152. glBegin( GL_POINTS );
  153. r::Color3( vec3_t(1.0, 0.0, 1.0) );
  154. glVertex3fv( &point[0] );
  155. glEnd();
  156. /// /
  157. vec4_t pos_viewspace = main_cam.view_mat * vec4_t(point, 1.0);
  158. vec4_t pos_clipspace = main_cam.projection_mat * pos_viewspace;
  159. vec4_t point_scrspace = pos_clipspace / pos_clipspace.w;
  160. float depth;
  161. glReadPixels( i::mouse_pos.x, r::h-i::mouse_pos.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );
  162. int viewpoint[] = {0, 0, r::w, r::h};
  163. vec3_t point_rc; // point recreated
  164. r::UnProject( i::mouse_pos.x, (float)viewpoint[3]-i::mouse_pos.y, depth, main_cam.view_mat, main_cam.projection_mat, viewpoint, point_rc.x, point_rc.y, point_rc.z );
  165. /// test for deffered
  166. // do some crap for pos_eyespace
  167. vec3_t v[4];
  168. {
  169. //int pixels[4][2]={ { 0,0 },{0,r::h},{r::w,r::h},{r::w,0} };
  170. int pixels[4][2]={ {r::w,r::h}, {0,r::h}, { 0,0 }, {r::w,0} };
  171. int viewport[4]={ 0,0,r::w,r::h };
  172. mat3_t view_rotation = main_cam.view_mat.GetRotationPart();
  173. float d[3];
  174. for( int i=0; i<4; i++ )
  175. {
  176. r::UnProject( pixels[i][0],pixels[i][1],10, main_cam.view_mat, main_cam.projection_mat, viewport, d[0],d[1],d[2] );
  177. v[i] = vec3_t ( d[0], d[1], d[2] );
  178. v[i] -= main_cam.world_translation;
  179. v[i].Normalize();
  180. v[i] = v[i]*view_rotation;
  181. }
  182. }
  183. // operations that happen in the shader:
  184. vec2_t planes;
  185. planes.x = -main_cam.zfar / (main_cam.zfar - main_cam.znear);
  186. planes.y = -main_cam.zfar * main_cam.znear / (main_cam.zfar - main_cam.znear);
  187. point_rc.z = - planes.y / (planes.x + depth);
  188. //vec3_t view( )
  189. //pos.xy = view.xy / view.z*pos.z;
  190. /// print some debug stuff
  191. hud::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
  192. hud::SetPos( -0.98, 0.95 );
  193. hud::SetFontWidth( 0.03 );
  194. hud::Printf( "frame:%d time:%dms\n", r::frames_num, StopBench() );
  195. hud::Printf( "mouse: %f %f\n", i::mouse_pos.x, i::mouse_pos.y );
  196. hud::Printf( "depth: %f\n", depth );
  197. hud::Printf( "wspac: %f %f %f %f\n", point.x, point.y, point.z, 1.0 );
  198. hud::Printf( "vspac: %f %f %f %f\n", pos_viewspace.x, pos_viewspace.y, pos_viewspace.z, pos_viewspace.w );
  199. hud::Printf( "vsp_r: %f %f %f\n", point_rc.x, point_rc.y, point_rc.z );
  200. if( i::keys[SDLK_ESCAPE] ) break;
  201. if( i::keys[SDLK_F11] ) hndl::TogleFullScreen();
  202. SDL_GL_SwapBuffers();
  203. r::PrintLastError();
  204. hndl::WaitForNextFrame();
  205. //if( r::frames_num == 5000 ) break;
  206. }while( true );
  207. INFO( "Appl quits after " << hndl::GetTicks() << " ticks" );
  208. hndl::QuitApp( EXIT_SUCCESS );
  209. return 0;
  210. }