Example #1
0
		ShaderProgram::ShaderProgram(Shader &vert, Shader &frag)
			: m_VertShader(vert.ID()), m_FragShader(frag.ID())
		{
			CreateProgram();
		}
Example #2
0
void display_handler(void) {
    // clear scene
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1,1,1);
	shader.Bind();

	// pass uniform variables to shader
	GLint projectionMatrix_location    = glGetUniformLocation(shader.ID(), "projectionMatrix");
	GLint viewMatrix_location          = glGetUniformLocation(shader.ID(), "viewMatrix");
	GLint modelMatrix_location         = glGetUniformLocation(shader.ID(), "modelMatrix");
	GLint normalMatrix_location        = glGetUniformLocation(shader.ID(), "normalMatrix");
	GLint materialAmbient_location     = glGetUniformLocation(shader.ID(), "materialAmbient");
	GLint materialDiffuse_location     = glGetUniformLocation(shader.ID(), "materialDiffuse");
	GLint materialSpecular_location    = glGetUniformLocation(shader.ID(), "materialSpecular");
	GLint lightPosition_location       = glGetUniformLocation(shader.ID(), "lightPosition");
	GLint lightAmbient_location        = glGetUniformLocation(shader.ID(), "lightAmbient");
	GLint lightDiffuse_location        = glGetUniformLocation(shader.ID(), "lightDiffuse");
	GLint lightSpecular_location       = glGetUniformLocation(shader.ID(), "lightSpecular");
	GLint lightGlobal_location         = glGetUniformLocation(shader.ID(), "lightGlobal");
	GLint materialShininess_location   = glGetUniformLocation(shader.ID(), "materialShininess");
	GLint constantAttenuation_location = glGetUniformLocation(shader.ID(), "constantAttenuation");
	GLint linearAttenuation_location   = glGetUniformLocation(shader.ID(), "linearAttenuation");
	GLint useTexture_location          = glGetUniformLocation(shader.ID(), "useTexture");
	glUniformMatrix4fv( projectionMatrix_location, 1, GL_FALSE, &projectionMatrix[0][0]);
	glUniformMatrix4fv( viewMatrix_location,       1, GL_FALSE, &viewMatrix[0][0]);
	glUniformMatrix4fv( modelMatrix_location,      1, GL_FALSE, &modelMatrix[0][0]);
	glUniformMatrix3fv( normalMatrix_location,     1, GL_FALSE, &normalMatrix[0][0]);
    glUniform3fv(       materialAmbient_location,  1, materialAmbient);
    glUniform3fv(       materialDiffuse_location,  1, materialDiffuse);
    glUniform3fv(       materialSpecular_location, 1, materialSpecular);
    glUniform3fv(       lightPosition_location,    1, lightPosition);
    glUniform3fv(       lightAmbient_location,     1, lightAmbient);
    glUniform3fv(       lightDiffuse_location,     1, lightDiffuse);
    glUniform3fv(       lightSpecular_location,    1, lightSpecular);
    glUniform3fv(       lightGlobal_location,      1, lightGlobal);
    glUniform1f(        materialShininess_location,   materialShininess);
    glUniform1f(        constantAttenuation_location, constantAttenuation);
    glUniform1f(        linearAttenuation_location,   linearAttenuation);
    glUniform1i(        useTexture_location,          useTexture);

    // bind texture to shader
    GLint texture0_location = glGetAttribLocation(shader.ID(), "texture0");
    if (texture0_location != -1) {
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureID);
        glUniform1i(texture0_location, 0);
    }
    // bind vertex uv coordinates to shader
	GLint uv_location = glGetAttribLocation(shader.ID(), "vertex_uv");
	if (uv_location != -1) {
        glEnableVertexAttribArray(uv_location);
        glBindBuffer(GL_ARRAY_BUFFER, vertex_uv_buffer);
        glVertexAttribPointer(uv_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
    }
    // bind vertex positions to shader
	GLint position_location = glGetAttribLocation(shader.ID(), "vertex_position");
	if (position_location != -1) {
        glEnableVertexAttribArray(position_location);
        glBindBuffer(GL_ARRAY_BUFFER, vertex_position_buffer);
        glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
    }
    // bind vertex normals to shader
	GLint normal_location = glGetAttribLocation(shader.ID(), "vertex_normal");
	if (normal_location != -1) {
        glEnableVertexAttribArray(normal_location);
        glBindBuffer(GL_ARRAY_BUFFER, vertex_normal_buffer);
        glVertexAttribPointer(normal_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
    }

    // draw the scene
	glDrawArrays(GL_TRIANGLES, 0, trig.VertexCount());
	glDisableVertexAttribArray(position_location);
	glDisableVertexAttribArray(uv_location);
	glDisableVertexAttribArray(normal_location);
	shader.Unbind();
	glFlush();
}