Shader.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_SHADER_HPP
26 #define CPP3DS_SHADER_HPP
27 
29 // Headers
31 #include <cpp3ds/OpenGL.hpp>
32 #include <cpp3ds/Graphics/Transform.hpp>
33 #include <cpp3ds/Graphics/Color.hpp>
34 #include <cpp3ds/System/NonCopyable.hpp>
35 #include <cpp3ds/System/Vector2.hpp>
36 #include <cpp3ds/System/Vector3.hpp>
37 #include <map>
38 #include <string>
39 #ifndef EMULATION
40 #include <3ds.h>
41 #endif
42 
43 
44 namespace cpp3ds
45 {
46 class InputStream;
47 class Texture;
48 
54 {
55 public :
56 
57  static Shader Default;
58 
63  enum Type
64  {
67  };
68 
76  struct CurrentTextureType {};
77 
85 
86 public :
87 
94  Shader();
95 
100  ~Shader();
101 
121  bool loadFromFile(const std::string& filename, Type type);
122 
142  bool loadFromFile(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename);
143 
162  bool loadFromMemory(const std::string& shader, Type type);
163 
183  bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader);
184 
185  bool loadBinary(const Uint8* data, const Uint32 size, Type type);
186 
205  bool loadFromStream(InputStream& stream, Type type);
206 
226  bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
227 
247  void setParameter(const std::string& name, float x);
248 
269  void setParameter(const std::string& name, float x, float y);
270 
292  void setParameter(const std::string& name, float x, float y, float z);
293 
316  void setParameter(const std::string& name, float x, float y, float z, float w);
317 
337  void setParameter(const std::string& name, const Vector2f& vector);
338 
358  void setParameter(const std::string& name, const Vector3f& vector);
359 
385  void setParameter(const std::string& name, const Color& color);
386 
408  void setParameter(const std::string& name, const cpp3ds::Transform& transform);
409 
440  void setParameter(const std::string& name, const Texture& texture);
441 
463  void setParameter(const std::string& name, CurrentTextureType);
464 
475  unsigned int getNativeHandle() const;
476 
498  static void bind(const Shader* shader);
499 
510  static bool isAvailable();
511 
512 private :
513 
526  bool compile(const char* vertexShaderCode, const char* fragmentShaderCode);
527 
535  void bindTextures() const;
536 
545  int getParamLocation(const std::string& name);
546 
548  // Types
550  typedef std::map<int, const Texture*> TextureTable;
551  typedef std::map<std::string, int> ParamTable;
552 
554  // Member data
556  GLuint m_shaderProgram;
557  int m_currentTexture;
558  TextureTable m_textures;
559  ParamTable m_params;
560  std::vector<char> m_shaderData;
561 
562 #ifndef EMULATION
563  DVLB_s* dvlb;
564  shaderProgram_s shader;
565 #endif
566 };
567 
568 }
569 
570 
571 #endif
572 
573 
Shader class (vertex and fragment)
Definition: Shader.hpp:53
Special type that can be passed to setParameter, and that represents the texture of the object being ...
Definition: Shader.hpp:76
Abstract class for custom file input streams.
Definition: InputStream.hpp:40
unsigned int getNativeHandle() const
Get the underlying OpenGL handle of the shader.
Utility class for manpulating RGBA colors.
Definition: Color.hpp:36
void setParameter(const std::string &name, float x)
Change a float parameter of the shader.
static bool isAvailable()
Tell whether or not the system supports shaders.
Type
Types of shaders.
Definition: Shader.hpp:63
Shader()
Default constructor.
Vertex shader.
Definition: Shader.hpp:65
bool loadFromFile(const std::string &filename, Type type)
Load either the vertex or fragment shader from a file.
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition: Shader.hpp:84
Geometry shader.
Definition: Shader.hpp:66
~Shader()
Destructor.
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:35
bool loadFromMemory(const std::string &shader, Type type)
Load either the vertex or fragment shader from a source code in memory.
Define a 3x3 transform matrix.
Definition: Transform.hpp:41
bool loadFromStream(InputStream &stream, Type type)
Load either the vertex or fragment shader from a custom stream.
static void bind(const Shader *shader)
Bind a shader for rendering.
some things could easily be broken///////
Definition: Texture.hpp:47