2
0

Renderer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include <string>
  10. #include <vector>
  11. #include <unordered_map>
  12. #include <SDL/SDL.h>
  13. #include "Math.h"
  14. struct DirectionalLight
  15. {
  16. // Direction of light
  17. Vector3 mDirection;
  18. // Diffuse color
  19. Vector3 mDiffuseColor;
  20. // Specular color
  21. Vector3 mSpecColor;
  22. };
  23. class Renderer
  24. {
  25. public:
  26. Renderer(class Game* game);
  27. ~Renderer();
  28. bool Initialize(float screenWidth, float screenHeight);
  29. void Shutdown();
  30. void UnloadData();
  31. void Draw();
  32. void AddSprite(class SpriteComponent* sprite);
  33. void RemoveSprite(class SpriteComponent* sprite);
  34. void AddMeshComp(class MeshComponent* mesh);
  35. void RemoveMeshComp(class MeshComponent* mesh);
  36. void AddPointLight(class PointLightComponent* light);
  37. void RemovePointLight(class PointLightComponent* light);
  38. class Texture* GetTexture(const std::string& fileName);
  39. class Mesh* GetMesh(const std::string& fileName);
  40. void SetViewMatrix(const Matrix4& view) { mView = view; }
  41. const Vector3& GetAmbientLight() const { return mAmbientLight; }
  42. void SetAmbientLight(const Vector3& ambient) { mAmbientLight = ambient; }
  43. DirectionalLight& GetDirectionalLight() { return mDirLight; }
  44. const std::vector<class PointLightComponent*>& GetPointLights() const { return mPointLights; }
  45. // Given a screen space point, unprojects it into world space,
  46. // based on the current 3D view/projection matrices
  47. // Expected ranges:
  48. // x = [-screenWidth/2, +screenWidth/2]
  49. // y = [-screenHeight/2, +screenHeight/2]
  50. // z = [0, 1) -- 0 is closer to camera, 1 is further
  51. Vector3 Unproject(const Vector3& screenPoint) const;
  52. // Gets start point and direction of screen vector
  53. void GetScreenDirection(Vector3& outStart, Vector3& outDir) const;
  54. float GetScreenWidth() const { return mScreenWidth; }
  55. float GetScreenHeight() const { return mScreenHeight; }
  56. void SetMirrorView(const Matrix4& view) { mMirrorView = view; }
  57. class Texture* GetMirrorTexture() { return mMirrorTexture; }
  58. class GBuffer* GetGBuffer() { return mGBuffer; }
  59. private:
  60. // Chapter 14 additions
  61. void Draw3DScene(unsigned int framebuffer, const Matrix4& view, const Matrix4& proj, bool lit = true);
  62. bool CreateMirrorTarget();
  63. void DrawFromGBuffer();
  64. //void DrawFromGBuffer();
  65. // End chapter 14 additions
  66. bool LoadShaders();
  67. void CreateSpriteVerts();
  68. void SetLightUniforms(class Shader* shader, const Matrix4& view);
  69. // Map of textures loaded
  70. std::unordered_map<std::string, class Texture*> mTextures;
  71. // Map of meshes loaded
  72. std::unordered_map<std::string, class Mesh*> mMeshes;
  73. // All the sprite components drawn
  74. std::vector<class SpriteComponent*> mSprites;
  75. // All (non-skeletal) mesh components drawn
  76. std::vector<class MeshComponent*> mMeshComps;
  77. std::vector<class SkeletalMeshComponent*> mSkeletalMeshes;
  78. // Game
  79. class Game* mGame;
  80. // Sprite shader
  81. class Shader* mSpriteShader;
  82. // Sprite vertex array
  83. class VertexArray* mSpriteVerts;
  84. // Mesh shader
  85. class Shader* mMeshShader;
  86. // Skinned shader
  87. class Shader* mSkinnedShader;
  88. // View/projection for 3D shaders
  89. Matrix4 mView;
  90. Matrix4 mProjection;
  91. // Lighting data
  92. Vector3 mAmbientLight;
  93. DirectionalLight mDirLight;
  94. // Window
  95. SDL_Window* mWindow;
  96. // OpenGL context
  97. SDL_GLContext mContext;
  98. // Width/height
  99. float mScreenWidth;
  100. float mScreenHeight;
  101. unsigned int mMirrorBuffer;
  102. class Texture* mMirrorTexture;
  103. Matrix4 mMirrorView;
  104. class GBuffer* mGBuffer;
  105. // GBuffer shader
  106. class Shader* mGGlobalShader;
  107. class Shader* mGPointLightShader;
  108. std::vector<class PointLightComponent*> mPointLights;
  109. class Mesh* mPointLightMesh;
  110. };