MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
Classes | Public Member Functions | Static Public Attributes | List of all members
mingfx::DefaultShader Class Reference

Detailed Description

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:

DefaultShader phong_shader;
Mesh teapot;
DefaultShader::MaterialProperties teapot_material;
void Init() {
// initialize the shader
DefaultShader::LightProperties red_light;
red_light.position = Point3(-10, 5, 5);
red_light.diffuseIntensity = Color(1,0,0);
phong_shader.AddLight(red_light);
// initialize the mesh
teapot.LoadFromOBJ(Platform::FindMinGfxDataFile("teapot.obj"));
}
void DrawUsingOpenGL() {
Matrix4 M;
Matrix4 V = Matrix4::LookAt(Point3(0,0,3), Point3(0,0,0), Vector3(0,1,0));
Matrix4 P = Matrix4::Perspective(60.0, aspect_ratio(), 0.1, 10.0);
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...
static std::string FindMinGfxDataFile(const std::string &basename)
Searches for a data file that ships with MinGfx.

Definition at line 62 of file default_shader.h.

#include <default_shader.h>

Classes

class  LightProperties
 Small data structure to hold per-light properties. More...
 
class  MaterialProperties
 Small data structure to hold properties of the material to be lit. More...
 

Public Member Functions

 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)
 

Static Public Attributes

static const unsigned int MAX_LIGHTS = 10
 If changed, this needs to also be changed in the glsl shader code. More...
 

Constructor & Destructor Documentation

◆ DefaultShader()

mingfx::DefaultShader::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().

◆ ~DefaultShader()

virtual mingfx::DefaultShader::~DefaultShader ( )
virtual

Member Function Documentation

◆ AddLight()

void mingfx::DefaultShader::AddLight ( LightProperties  light)

Multiple lights are supported, this adds one to the end of the list. Up to MAX_LIGHTS can be added.

◆ Draw()

void mingfx::DefaultShader::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.

◆ Init()

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().

◆ light()

LightProperties mingfx::DefaultShader::light ( int  i)

◆ num_lights()

int mingfx::DefaultShader::num_lights ( )

◆ SetLight()

void mingfx::DefaultShader::SetLight ( int  i,
LightProperties  light 
)

Changes the properties for a light that was already added.

◆ StopProgram()

void mingfx::DefaultShader::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.

◆ UseProgram()

void mingfx::DefaultShader::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.

Member Data Documentation

◆ MAX_LIGHTS

const unsigned int mingfx::DefaultShader::MAX_LIGHTS = 10
static

If changed, this needs to also be changed in the glsl shader code.

Definition at line 66 of file default_shader.h.


The documentation for this class was generated from the following file: