A simple GLSL shader for textured per-fragment Phong shading with multiple light sources.
This can be used to draw 3D models stored in a mingfx::Mesh data structure or you can use it with your own geometry data structures. Lighting properties are stored within the class itself since these are considered part of the shading model. Material properties are considered properties of the meshes or other materials you wish to draw so these are stored outside of the class and passed into the Draw() or UseProgram() functions.
An example of using DefaultShader to render a mesh:
Mesh teapot;
DefaultShader::MaterialProperties teapot_material;
DefaultShader::LightProperties red_light;
red_light.position = Point3(-10, 5, 5);
red_light.diffuseIntensity = Color(1,0,0);
phong_shader.AddLight(red_light);
}
void DrawUsingOpenGL() {
Matrix4 M;
phong_shader.Draw(M, V, P, teapot, teapot_material);
}
void Init()
This loads vertex and fragment shaders from files, compiles them, and links them. So,...
DefaultShader(bool add_default_light=true)
The constructor defaults to adding a single white light to the scene at (10,10,10)....
static Matrix4 Perspective(float fov_y_in_degrees, float aspect_ratio, float near_plane_dist, float far_plane_dist)
Returns a perspective projection matrix equivalent to the one gluPerspective creates.
static Matrix4 LookAt(Point3 eye, Point3 target, Vector3 up)
Returns a view matrix that centers the camera at the 'eye' position and orients it to look at the des...
Definition at line 62 of file default_shader.h.
|
| DefaultShader (bool add_default_light=true) |
| The constructor defaults to adding a single white light to the scene at (10,10,10). Change this by passing it 'false'. The constructor does not load and compile the shader right away. This is done inside Init(). More...
|
|
virtual | ~DefaultShader () |
|
void | AddLight (LightProperties light) |
| Multiple lights are supported, this adds one to the end of the list. Up to MAX_LIGHTS can be added. More...
|
|
void | SetLight (int i, LightProperties light) |
| Changes the properties for a light that was already added. More...
|
|
void | Init () |
| This loads vertex and fragment shaders from files, compiles them, and links them. So, it must be called from within an active OpenGL context, for example, from within GraphicsApp::Init() or GraphicsApp::DrawUsingOpenGL(). If you call Draw() before calling Init(), then Init() will be called as the first step within Draw(). So, if you do not mind a slowdown on the very first frame of your program, it is fine to skip calling Init(). More...
|
|
void | Draw (const Matrix4 &model, const Matrix4 &view, const Matrix4 &projection, Mesh *mesh, const MaterialProperties &material) |
| This starts the shader and sets its uniform variables based upon the current set of lights, the material properties passed in, and the model, view, and projection matrices. Then, it calls mesh->Draw(). After drawing, it disables the shader. More...
|
|
void | UseProgram (const Matrix4 &model, const Matrix4 &view, const Matrix4 &projection, const MaterialProperties &material) |
| Only needed if you do not want to draw a Mesh. This does all of the same setup for drawing that the Draw() function does and then it returns so that you may draw your own geometry however you want. After doing your draw must call StopProgram() to turn off the shader. More...
|
|
void | StopProgram () |
| Only needed if you do not want to draw a Mesh. Call this after UseProgram() and after drawing your geometry to turn off the shader. More...
|
|
int | num_lights () |
|
LightProperties | light (int i) |
|
void mingfx::DefaultShader::Init |
( |
| ) |
|
This loads vertex and fragment shaders from files, compiles them, and links them. So, it must be called from within an active OpenGL context, for example, from within GraphicsApp::Init() or GraphicsApp::DrawUsingOpenGL(). If you call Draw() before calling Init(), then Init() will be called as the first step within Draw(). So, if you do not mind a slowdown on the very first frame of your program, it is fine to skip calling Init().