MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
shader_program.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2018, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 #ifndef SRC_SHADERPROGRAM_H_
15 #define SRC_SHADERPROGRAM_H_
16 
17 #include "color.h"
18 #include "matrix4.h"
19 #include "opengl_headers.h"
20 #include "point2.h"
21 #include "point3.h"
22 #include "texture2d.h"
23 #include "vector2.h"
24 #include "vector3.h"
25 
26 #include <string>
27 #include <map>
28 
29 namespace mingfx {
30 
67 public:
70 
71  virtual ~ShaderProgram();
72 
73 
74  // ---- These should be called during startup (e.g., in InitOpenGL()) ----
75 
80  bool AddVertexShaderFromFile(const std::string &file);
81 
84  bool AddVertexShaderFromSource(const std::string &code);
85 
90  bool AddFragmentShaderFromFile(const std::string &file);
91 
94  bool AddFragmentShaderFromSource(const std::string &code);
95 
99  bool LinkProgram();
100 
101 
102 
103  // ---- These should be called during rendering (e.g., in DrawUsingOpenGL()) ----
104 
110  void UseProgram();
111 
112  // Set Uniform Variables in the Shader
113 
114  // MinGfx types
115 
118  void SetUniform(const std::string &name, const Point2 &p);
119 
122  void SetUniform(const std::string &name, const Vector2 &v);
123 
126  void SetUniform(const std::string &name, const Point3 &p);
127 
130  void SetUniform(const std::string &name, const Vector3 &v);
131 
134  void SetUniform(const std::string &name, const Matrix4 &m);
135 
138  void SetUniform(const std::string &name, const Color &c);
139 
140 
141  // built-in types
142 
145  void SetUniform(const std::string &name, int i);
146 
149  void SetUniform(const std::string &name, unsigned int ui);
150 
153  void SetUniform(const std::string &name, float f);
154 
155 
156  // built-in types (arrays)
157 
160  void SetUniformArray1(const std::string &name, int *i, int count);
161 
164  void SetUniformArray1(const std::string &name, unsigned int *ui, int count);
165 
168  void SetUniformArray1(const std::string &name, float *f, int count);
169 
170 
173  void SetUniformArray2(const std::string &name, int *i, int count);
174 
177  void SetUniformArray2(const std::string &name, unsigned int *ui, int count);
178 
181  void SetUniformArray2(const std::string &name, float *f, int count);
182 
183 
186  void SetUniformArray3(const std::string &name, int *i, int count);
187 
190  void SetUniformArray3(const std::string &name, unsigned int *ui, int count);
191 
194  void SetUniformArray3(const std::string &name, float *f, int count);
195 
196 
199  void SetUniformArray4(const std::string &name, int *i, int count);
200 
203  void SetUniformArray4(const std::string &name, unsigned int *ui, int count);
204 
207  void SetUniformArray4(const std::string &name, float *f, int count);
208 
209 
210  // Set Textures (Sampler Variables in the Shader)
211 
215  void BindTexture(const std::string &name, const Texture2D &tex);
216 
219  void BindTexture(const std::string &name, const Texture2D &tex, int texUnit);
220 
221 
223  void StopProgram();
224 
226  bool initialized();
227 
228 private:
229  GLuint vertexShader_;
230  GLuint fragmentShader_;
231  GLuint program_;
232  std::map<std::string, int> texBindings_;
233 };
234 
235 
236 } // end namespace
237 
238 #endif
Represents a 4-component (R,G,B,A) color, stored internally in a float array to be compatable with Op...
Definition: color.h:41
A 4x4 transformation matrix stored internally as an array of floats in column-major order so as to be...
Definition: matrix4.h:50
A 2D Point with floating point coordinates, used for storing 2D texture coordinates,...
Definition: point2.h:28
A 3D Point with floating point coordinates, used for storing vertices and all sorts of other 3D graph...
Definition: point3.h:52
A wrapper around GLSL shader programs.
void SetUniform(const std::string &name, const Vector3 &v)
Passes the x,y,z,0 values of vector v to the shader program and stores the result in the shader varia...
ShaderProgram()
Creates an empty ShaderProgram object.
void SetUniformArray4(const std::string &name, unsigned int *ui, int count)
Passes an array of count 4D unsigned int arrays to the shader program and stores the result in the sh...
void StopProgram()
Call this after rendering geometry to deactivate the shader.
bool initialized()
Returns true if the shader program has been successfully compiled and linked.
void SetUniform(const std::string &name, const Point2 &p)
Passes the x,y values of point p to the shader program and stores the result in the shader variable n...
void BindTexture(const std::string &name, const Texture2D &tex)
Binds a Texture2D to a sampler2D in the shader program. This version automatically selects an availab...
void SetUniformArray1(const std::string &name, int *i, int count)
Passes an array of count ints to the shader program and stores the result in the shader variable name...
void SetUniformArray1(const std::string &name, float *f, int count)
Passes an array of count floats to the shader program and stores the result in the shader variable na...
bool AddVertexShaderFromFile(const std::string &file)
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL())....
void SetUniform(const std::string &name, float f)
Passes the float to the shader program and stores the result in the shader variable named name,...
void SetUniformArray2(const std::string &name, unsigned int *ui, int count)
Passes an array of count 2D unsigned int arrays to the shader program and stores the result in the sh...
void SetUniform(const std::string &name, unsigned int ui)
Passes the unsigned int to the shader program and stores the result in the shader variable named name...
void SetUniform(const std::string &name, const Matrix4 &m)
Passes the column-major 16 float values of matrix m to the shader program and stores the result in th...
bool AddFragmentShaderFromSource(const std::string &code)
This loads and compiles a shader from a string. An error will be printed to stderr if there are any c...
void SetUniformArray3(const std::string &name, unsigned int *ui, int count)
Passes an array of count 3D unsigned int arrays to the shader program and stores the result in the sh...
void SetUniformArray2(const std::string &name, float *f, int count)
Passes an array of count 2D float arrays to the shader program and stores the result in the shader va...
void SetUniformArray4(const std::string &name, float *f, int count)
Passes an array of count 4D float arrays to the shader program and stores the result in the shader va...
void SetUniformArray2(const std::string &name, int *i, int count)
Passes an array of count 2D int arrays to the shader program and stores the result in the shader vari...
void SetUniform(const std::string &name, const Color &c)
Passes the r,g,b,a values of color c to the shader program and stores the result in the shader variab...
void SetUniformArray4(const std::string &name, int *i, int count)
Passes an array of count 4D int arrays to the shader program and stores the result in the shader vari...
void SetUniform(const std::string &name, int i)
Passes the int to the shader program and stores the result in the shader variable named name,...
bool AddVertexShaderFromSource(const std::string &code)
This loads and compiles a shader from a string. An error will be printed to stderr if there are any c...
void SetUniformArray1(const std::string &name, unsigned int *ui, int count)
Passes an array of count unsigned ints to the shader program and stores the result in the shader vari...
void SetUniformArray3(const std::string &name, int *i, int count)
Passes an array of count 3D int arrays to the shader program and stores the result in the shader vari...
void SetUniform(const std::string &name, const Point3 &p)
Passes the x,y,z,1 values of point p to the shader program and stores the result in the shader variab...
bool AddFragmentShaderFromFile(const std::string &file)
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL())....
bool LinkProgram()
Call this after adding vertex and fragment shaders in order to link them together to create the full ...
void SetUniform(const std::string &name, const Vector2 &v)
Passes the x,y values of vector v to the shader program and stores the result in the shader variable ...
void UseProgram()
Call this first to make the shader program active, then call SetUniform() to pass data from your C++ ...
void BindTexture(const std::string &name, const Texture2D &tex, int texUnit)
Binds a Texture2D to a sampler2D in the shader program. This version allows you to specify the textur...
void SetUniformArray3(const std::string &name, float *f, int count)
Passes an array of count 3D float arrays to the shader program and stores the result in the shader va...
A wrapper around a 2D texture that supports loading images from files or setting texture color data d...
Definition: texture2d.h:42
A 2D Vector with floating point coordinates, used for storing 2D translations, mouse movements,...
Definition: vector2.h:28
A 3D Vector with floating point coordinates, used for storing normals and all sorts of other 3D graph...
Definition: vector3.h:62
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21