2
0

Renderer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class Texture* GetTexture(const std::string& fileName);
  37. class Mesh* GetMesh(const std::string& fileName);
  38. void SetViewMatrix(const Matrix4& view) { mView = view; }
  39. void SetAmbientLight(const Vector3& ambient) { mAmbientLight = ambient; }
  40. DirectionalLight& GetDirectionalLight() { return mDirLight; }
  41. // Given a screen space point, unprojects it into world space,
  42. // based on the current 3D view/projection matrices
  43. // Expected ranges:
  44. // x = [-screenWidth/2, +screenWidth/2]
  45. // y = [-screenHeight/2, +screenHeight/2]
  46. // z = [0, 1) -- 0 is closer to camera, 1 is further
  47. Vector3 Unproject(const Vector3& screenPoint) const;
  48. // Gets start point and direction of screen vector
  49. void GetScreenDirection(Vector3& outStart, Vector3& outDir) const;
  50. float GetScreenWidth() const { return mScreenWidth; }
  51. float GetScreenHeight() const { return mScreenHeight; }
  52. private:
  53. bool LoadShaders();
  54. void CreateSpriteVerts();
  55. void SetLightUniforms(class Shader* shader);
  56. // Map of textures loaded
  57. std::unordered_map<std::string, class Texture*> mTextures;
  58. // Map of meshes loaded
  59. std::unordered_map<std::string, class Mesh*> mMeshes;
  60. // All the sprite components drawn
  61. std::vector<class SpriteComponent*> mSprites;
  62. // All (non-skeletal) mesh components drawn
  63. std::vector<class MeshComponent*> mMeshComps;
  64. std::vector<class SkeletalMeshComponent*> mSkeletalMeshes;
  65. // Game
  66. class Game* mGame;
  67. // Sprite shader
  68. class Shader* mSpriteShader;
  69. // Sprite vertex array
  70. class VertexArray* mSpriteVerts;
  71. // Mesh shader
  72. class Shader* mMeshShader;
  73. // Skinned shader
  74. class Shader* mSkinnedShader;
  75. // View/projection for 3D shaders
  76. Matrix4 mView;
  77. Matrix4 mProjection;
  78. // Width/height of screen
  79. float mScreenWidth;
  80. float mScreenHeight;
  81. // Lighting data
  82. Vector3 mAmbientLight;
  83. DirectionalLight mDirLight;
  84. // Window
  85. SDL_Window* mWindow;
  86. // OpenGL context
  87. SDL_GLContext mContext;
  88. };