15#ifndef CSCI441_SIMPLE_SHADER_HPP
16#define CSCI441_SIMPLE_SHADER_HPP
21#ifdef CSCI441_USE_GLEW
28#include <glm/gtc/type_ptr.hpp>
64 [[maybe_unused]] GLuint
registerVertexArray(
const std::vector<glm::vec2>& VERTEX_POINTS,
const std::vector<glm::vec3>& VERTEX_COLORS);
75 [[maybe_unused]]
void updateVertexArray(GLuint VAOD,
const std::vector<glm::vec2>& VERTEX_POINTS,
const std::vector<glm::vec3>& VERTEX_COLORS);
84 [[maybe_unused]] GLuint
registerVertexArray(GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]);
96 [[maybe_unused]]
void updateVertexArray(GLuint VAOD, GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]);
128 [[maybe_unused]]
void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT);
158 [[maybe_unused]] GLuint
registerVertexArray(
const std::vector<glm::vec3>& VERTEX_POINTS,
const std::vector<glm::vec3>& VERTEX_NORMALS);
170 [[maybe_unused]]
void updateVertexArray(GLuint VAOD,
const std::vector<glm::vec3>& VERTEX_POINTS,
const std::vector<glm::vec3>& VERTEX_NORMALS);
179 [[maybe_unused]] GLuint
registerVertexArray(GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]);
191 [[maybe_unused]]
void updateVertexArray(GLuint VAOD, GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]);
202 [[maybe_unused]]
void setViewMatrix(
const glm::mat4& VIEW_MATRIX);
213 [[maybe_unused]]
void setLightColor(
const glm::vec3& LIGHT_COLOR);
255 [[maybe_unused]]
void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT);
263namespace CSCI441_INTERNAL {
268 GLuint
registerVertexArray(GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]);
269 void updateVertexArray(GLuint VAOD, GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]);
274 void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT);
276 inline GLboolean smoothShading =
true;
277 inline GLint shaderProgramHandle = -1;
278 inline GLint modelLocation = -1;
279 inline GLint viewLocation = -1;
280 inline GLint projectionLocation = -1;
281 inline GLint vertexLocation = -1;
282 inline GLint colorLocation = -1;
284 inline std::vector<glm::mat4> transformationStack;
285 inline glm::mat4 modelMatrix(1.0f);
292 GLuint
registerVertexArray(GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]);
293 void updateVertexArray(GLuint VAOD, GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]);
302 void setNormalMatrix();
305 void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT);
307 inline GLboolean smoothShading =
true;
308 inline GLint shaderProgramHandle = -1;
309 inline GLint modelLocation = -1;
310 inline GLint viewLocation = -1;
311 inline GLint projectionLocation = -1;
312 inline GLint normalMtxLocation = -1;
313 inline GLint lightPositionLocation = -1;
314 inline GLint lightColorLocation = -1;
315 inline GLint materialLocation = -1;
316 inline GLint vertexLocation = -1;
317 inline GLint normalLocation = -1;
318 inline GLint useLightingLocation = -1;
320 inline std::vector<glm::mat4> transformationStack;
321 inline glm::mat4 modelMatrix(1.0f);
322 inline glm::mat4 viewMatrix(1.0f);
332 CSCI441_INTERNAL::SimpleShader2::enableFlatShading();
337 CSCI441_INTERNAL::SimpleShader2::enableSmoothShading();
342 CSCI441_INTERNAL::SimpleShader2::setupSimpleShader();
347 return CSCI441_INTERNAL::SimpleShader2::registerVertexArray(VERTEX_POINTS.size(), &VERTEX_POINTS[0], &VERTEX_COLORS[0]);
352 CSCI441_INTERNAL::SimpleShader2::updateVertexArray(VAOD, VERTEX_POINTS.size(), &VERTEX_POINTS[0], &VERTEX_COLORS[0]);
357 return CSCI441_INTERNAL::SimpleShader2::registerVertexArray(NUM_POINTS, VERTEX_POINTS, VERTEX_COLORS);
362 CSCI441_INTERNAL::SimpleShader2::updateVertexArray(VAOD, NUM_POINTS, VERTEX_POINTS, VERTEX_COLORS);
367 CSCI441_INTERNAL::SimpleShader2::setProjectionMatrix(PROJECTION_MATRIX);
372 CSCI441_INTERNAL::SimpleShader2::pushTransformation(TRANSFORMATION_MATRIX);
377 CSCI441_INTERNAL::SimpleShader2::popTransformation();
382 CSCI441_INTERNAL::SimpleShader2::resetTransformationMatrix();
387 CSCI441_INTERNAL::SimpleShader2::draw(PRIMITIVE_TYPE, VAOD, VERTEX_COUNT);
394 CSCI441_INTERNAL::SimpleShader3::enableFlatShading();
399 CSCI441_INTERNAL::SimpleShader3::enableSmoothShading();
404 CSCI441_INTERNAL::SimpleShader3::setupSimpleShader();
409 return CSCI441_INTERNAL::SimpleShader3::registerVertexArray(VERTEX_POINTS.size(), &VERTEX_POINTS[0], &VERTEX_NORMALS[0]);
414 CSCI441_INTERNAL::SimpleShader3::updateVertexArray(VAOD, VERTEX_POINTS.size(), &VERTEX_POINTS[0], &VERTEX_NORMALS[0]);
419 return CSCI441_INTERNAL::SimpleShader3::registerVertexArray(NUM_POINTS, VERTEX_POINTS, VERTEX_NORMALS);
424 CSCI441_INTERNAL::SimpleShader3::updateVertexArray(VAOD, NUM_POINTS, VERTEX_POINTS, VERTEX_NORMALS);
429 CSCI441_INTERNAL::SimpleShader3::setProjectionMatrix(PROJECTION_MATRIX);
434 CSCI441_INTERNAL::SimpleShader3::setViewMatrix(VIEW_MATRIX);
439 CSCI441_INTERNAL::SimpleShader3::setLightPosition(LIGHT_POSITION);
444 CSCI441_INTERNAL::SimpleShader3::setLightColor(LIGHT_COLOR);
449 CSCI441_INTERNAL::SimpleShader3::setMaterialColor(MATERIAL_COLOR);
454 CSCI441_INTERNAL::SimpleShader3::pushTransformation(TRANSFORMATION_MATRIX);
459 CSCI441_INTERNAL::SimpleShader3::popTransformation();
464 CSCI441_INTERNAL::SimpleShader3::resetTransformationMatrix();
469 CSCI441_INTERNAL::SimpleShader3::enableLighting();
474 CSCI441_INTERNAL::SimpleShader3::disableLighting();
479 CSCI441_INTERNAL::SimpleShader3::draw(PRIMITIVE_TYPE, VAOD, VERTEX_COUNT);
486inline void CSCI441_INTERNAL::SimpleShader2::enableFlatShading() {
487 smoothShading =
false;
489inline void CSCI441_INTERNAL::SimpleShader2::enableSmoothShading() {
490 smoothShading =
true;
493inline void CSCI441_INTERNAL::SimpleShader2::setupSimpleShader() {
494 std::string vertex_shader_src =
500uniform mat4 projection;
502layout(location=0) in vec2 vPos;
503layout(location=1) in vec3 vColor;
505layout(location=0) )_";
506 vertex_shader_src += (smoothShading ? "" :
"flat ");
507 vertex_shader_src += R
"_(out vec4 fragColor;
510 gl_Position = projection * view * model * vec4(vPos, 0.0, 1.0);
511 fragColor = vec4(vColor, 1.0);
513 const char* vertexShaders[1] = { vertex_shader_src.c_str() };
515 std::string fragment_shader_src =
519layout(location=0) )_";
520 fragment_shader_src += (smoothShading ? "" :
"flat ");
521 fragment_shader_src += R
"_( in vec4 fragColor;
523layout(location=0) out vec4 fragColorOut;
526 fragColorOut = fragColor;
528 const char* fragmentShaders[1] = { fragment_shader_src.c_str() };
530 printf(
"[INFO]: /--------------------------------------------------------\\\n" );
532 GLuint vertexShaderHandle = glCreateShader( GL_VERTEX_SHADER );
533 glShaderSource(vertexShaderHandle, 1, vertexShaders,
nullptr);
534 glCompileShader(vertexShaderHandle);
535 ShaderUtils::printShaderLog(vertexShaderHandle);
537 GLuint fragmentShaderHandle = glCreateShader( GL_FRAGMENT_SHADER );
538 glShaderSource(fragmentShaderHandle, 1, fragmentShaders,
nullptr);
539 glCompileShader(fragmentShaderHandle);
540 ShaderUtils::printShaderLog(fragmentShaderHandle);
542 shaderProgramHandle = glCreateProgram();
543 glAttachShader(shaderProgramHandle, vertexShaderHandle);
544 glAttachShader(shaderProgramHandle, fragmentShaderHandle);
545 glLinkProgram(shaderProgramHandle);
546 ShaderUtils::printProgramLog(shaderProgramHandle);
548 glDetachShader(shaderProgramHandle, vertexShaderHandle);
549 glDeleteShader(vertexShaderHandle);
551 glDetachShader(shaderProgramHandle, fragmentShaderHandle);
552 glDeleteShader(fragmentShaderHandle);
554 ShaderUtils::printShaderProgramInfo(shaderProgramHandle,
true,
false,
false,
false,
true,
false,
true);
556 modelLocation = glGetUniformLocation(shaderProgramHandle,
"model");
557 viewLocation = glGetUniformLocation(shaderProgramHandle,
"view");
558 projectionLocation = glGetUniformLocation(shaderProgramHandle,
"projection");
560 vertexLocation = glGetAttribLocation(shaderProgramHandle,
"vPos");
561 colorLocation = glGetAttribLocation(shaderProgramHandle,
"vColor");
564 glm::mat4 identity(1.0f);
565 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(identity));
566 glProgramUniformMatrix4fv(shaderProgramHandle, viewLocation, 1, GL_FALSE, glm::value_ptr(identity));
567 glProgramUniformMatrix4fv(shaderProgramHandle, projectionLocation, 1, GL_FALSE, glm::value_ptr(identity));
569 transformationStack.emplace_back(identity);
571 glUseProgram(shaderProgramHandle);
574inline GLuint CSCI441_INTERNAL::SimpleShader2::registerVertexArray(
const GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]) {
576 glGenVertexArrays(1, &vaod);
577 glBindVertexArray(vaod);
580 glGenBuffers(1, &vbod);
581 glBindBuffer(GL_ARRAY_BUFFER, vbod);
582 glBufferData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*2 +
sizeof(GLfloat)*NUM_POINTS*3),
nullptr, GL_STATIC_DRAW);
583 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*2), VERTEX_POINTS);
584 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(GLfloat)*NUM_POINTS*2),
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_COLORS);
586 glEnableVertexAttribArray(vertexLocation);
587 glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr);
589 glEnableVertexAttribArray(colorLocation);
590 glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(GLfloat)*NUM_POINTS*2));
595inline void CSCI441_INTERNAL::SimpleShader2::updateVertexArray(
const GLuint VAOD,
const GLuint NUM_POINTS,
const glm::vec2 VERTEX_POINTS[],
const glm::vec3 VERTEX_COLORS[]) {
596 glBindVertexArray(VAOD);
597 glBindBuffer(GL_ARRAY_BUFFER, VAOD);
598 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*2), VERTEX_POINTS);
599 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(GLfloat)*NUM_POINTS*2),
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_COLORS);
602inline void CSCI441_INTERNAL::SimpleShader2::setProjectionMatrix(
const glm::mat4& PROJECTION_MATRIX) {
603 glProgramUniformMatrix4fv(shaderProgramHandle, projectionLocation, 1, GL_FALSE, glm::value_ptr(PROJECTION_MATRIX));
606inline void CSCI441_INTERNAL::SimpleShader2::pushTransformation(
const glm::mat4& TRANSFORMATION_MATRIX) {
607 transformationStack.emplace_back(TRANSFORMATION_MATRIX);
609 modelMatrix *= TRANSFORMATION_MATRIX;
610 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
613inline void CSCI441_INTERNAL::SimpleShader2::popTransformation() {
616 if( transformationStack.size() > 1 ) {
617 transformationStack.pop_back();
619 modelMatrix = glm::mat4(1.0f);
620 for(
auto tMtx : transformationStack ) {
623 glProgramUniformMatrix4fv( shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix) );
627inline void CSCI441_INTERNAL::SimpleShader2::resetTransformationMatrix() {
628 modelMatrix = glm::mat4(1.0f);
629 transformationStack.clear();
630 transformationStack.emplace_back(modelMatrix);
631 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
634inline void CSCI441_INTERNAL::SimpleShader2::draw(
const GLint PRIMITIVE_TYPE,
const GLuint VAOD,
const GLuint VERTEX_COUNT) {
635 glUseProgram(shaderProgramHandle);
636 glBindVertexArray(VAOD);
637 glDrawArrays(PRIMITIVE_TYPE, 0,
static_cast<GLsizei
>(VERTEX_COUNT));
642inline void CSCI441_INTERNAL::SimpleShader3::enableFlatShading() {
643 smoothShading =
false;
645inline void CSCI441_INTERNAL::SimpleShader3::enableSmoothShading() {
646 smoothShading =
true;
649inline void CSCI441_INTERNAL::SimpleShader3::setupSimpleShader() {
650 std::string vertex_shader_src =
656uniform mat4 projection;
657uniform mat3 normalMtx;
658uniform vec3 lightColor;
659uniform vec3 lightPosition;
660uniform vec3 materialColor;
662layout(location=0) in vec3 vPos;
663layout(location=2) in vec3 vNormal;
665layout(location=0) )_";
666 vertex_shader_src += (smoothShading ? "" :
"flat ");
667 vertex_shader_src += R
"_(out vec4 fragColor;
670 gl_Position = projection * view * model * vec4(vPos, 1.0);
672 vec3 vertexEye = (view * model * vec4(vPos, 1.0)).xyz;
673 vec3 lightEye = (view * vec4(lightPosition, 1.0)).xyz;
674 vec3 lightVec = normalize( lightEye - vertexEye );
675 vec3 normalVec = normalize( normalMtx * vNormal );
676 float sDotN = max(dot(lightVec, normalVec), 0.0);
677 vec3 diffColor = lightColor * materialColor * sDotN;
678 vec3 ambColor = materialColor * 0.3;
679 vec3 color = diffColor + ambColor;
680 fragColor = vec4(color, 1.0);
682 const char* vertexShaders[1] = { vertex_shader_src.c_str() };
684 std::string fragment_shader_src =
687uniform vec3 materialColor;
688uniform int useLighting;
690layout(location=0) )_";
691 fragment_shader_src += (smoothShading ? "" :
"flat ");
692 fragment_shader_src += R
"_( in vec4 fragColor;
694layout(location=0) out vec4 fragColorOut;
697 if(useLighting == 1) {
698 fragColorOut = fragColor;
700 fragColorOut = vec4(materialColor, 1.0f);
703 const char* fragmentShaders[1] = { fragment_shader_src.c_str() };
705 printf(
"[INFO]: /--------------------------------------------------------\\\n" );
707 GLuint vertexShaderHandle = glCreateShader( GL_VERTEX_SHADER );
708 glShaderSource(vertexShaderHandle, 1, vertexShaders,
nullptr);
709 glCompileShader(vertexShaderHandle);
710 ShaderUtils::printShaderLog(vertexShaderHandle);
712 GLuint fragmentShaderHandle = glCreateShader( GL_FRAGMENT_SHADER );
713 glShaderSource(fragmentShaderHandle, 1, fragmentShaders,
nullptr);
714 glCompileShader(fragmentShaderHandle);
715 ShaderUtils::printShaderLog(fragmentShaderHandle);
717 shaderProgramHandle = glCreateProgram();
718 glAttachShader(shaderProgramHandle, vertexShaderHandle);
719 glAttachShader(shaderProgramHandle, fragmentShaderHandle);
720 glLinkProgram(shaderProgramHandle);
721 ShaderUtils::printProgramLog(shaderProgramHandle);
723 glDetachShader(shaderProgramHandle, vertexShaderHandle);
724 glDeleteShader(vertexShaderHandle);
726 glDetachShader(shaderProgramHandle, fragmentShaderHandle);
727 glDeleteShader(fragmentShaderHandle);
729 ShaderUtils::printShaderProgramInfo(shaderProgramHandle,
true,
false,
false,
false,
true,
false,
true);
731 modelLocation = glGetUniformLocation(shaderProgramHandle,
"model");
732 viewLocation = glGetUniformLocation(shaderProgramHandle,
"view");
733 projectionLocation = glGetUniformLocation(shaderProgramHandle,
"projection");
734 normalMtxLocation = glGetUniformLocation(shaderProgramHandle,
"normalMtx");
735 lightPositionLocation=glGetUniformLocation(shaderProgramHandle,
"lightPosition");
736 lightColorLocation = glGetUniformLocation(shaderProgramHandle,
"lightColor");
737 materialLocation = glGetUniformLocation(shaderProgramHandle,
"materialColor");
738 useLightingLocation = glGetUniformLocation(shaderProgramHandle,
"useLighting");
740 vertexLocation = glGetAttribLocation(shaderProgramHandle,
"vPos");
741 normalLocation = glGetAttribLocation(shaderProgramHandle,
"vNormal");
743 glm::mat4 identity(1.0f);
744 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(identity));
745 glProgramUniformMatrix4fv(shaderProgramHandle, viewLocation, 1, GL_FALSE, glm::value_ptr(identity));
746 glProgramUniformMatrix4fv(shaderProgramHandle, projectionLocation, 1, GL_FALSE, glm::value_ptr(identity));
748 transformationStack.emplace_back(identity);
750 glm::vec3 white(1.0f, 1.0f, 1.0f);
751 glProgramUniform3fv(shaderProgramHandle, lightColorLocation, 1, glm::value_ptr(white));
752 glProgramUniform3fv(shaderProgramHandle, materialLocation, 1, glm::value_ptr(white));
754 glm::vec3 origin(0.0f, 0.0f, 0.0f);
755 glProgramUniform3fv(shaderProgramHandle, lightPositionLocation, 1, glm::value_ptr(origin));
757 glProgramUniform1i(shaderProgramHandle, useLightingLocation, 1);
759 glUseProgram(shaderProgramHandle);
763inline GLuint CSCI441_INTERNAL::SimpleShader3::registerVertexArray(
const GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]) {
765 glGenVertexArrays(1, &vaod);
766 glBindVertexArray(vaod);
769 glGenBuffers(1, &vbod);
770 glBindBuffer(GL_ARRAY_BUFFER, vbod);
771 glBufferData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3 +
sizeof(GLfloat)*NUM_POINTS*3),
nullptr, GL_STATIC_DRAW);
772 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_POINTS);
773 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(GLfloat)*NUM_POINTS*3),
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_NORMALS);
775 glEnableVertexAttribArray(vertexLocation);
776 glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr);
778 glEnableVertexAttribArray(normalLocation);
779 glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(GLfloat)*NUM_POINTS*2));
784inline void CSCI441_INTERNAL::SimpleShader3::updateVertexArray(
const GLuint VAOD,
const GLuint NUM_POINTS,
const glm::vec3 VERTEX_POINTS[],
const glm::vec3 VERTEX_NORMALS[]) {
785 glBindVertexArray(VAOD);
786 glBindBuffer(GL_ARRAY_BUFFER, VAOD);
787 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_POINTS);
788 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(GLfloat)*NUM_POINTS*3),
static_cast<GLsizeiptr
>(
sizeof(GLfloat)*NUM_POINTS*3), VERTEX_NORMALS);
791inline void CSCI441_INTERNAL::SimpleShader3::setProjectionMatrix(
const glm::mat4& PROJECTION_MATRIX) {
792 glProgramUniformMatrix4fv(shaderProgramHandle, projectionLocation, 1, GL_FALSE, glm::value_ptr(PROJECTION_MATRIX));
795inline void CSCI441_INTERNAL::SimpleShader3::setViewMatrix(
const glm::mat4& VIEW_MATRIX) {
796 glProgramUniformMatrix4fv(shaderProgramHandle, viewLocation, 1, GL_FALSE, glm::value_ptr(VIEW_MATRIX));
798 viewMatrix = VIEW_MATRIX;
802inline void CSCI441_INTERNAL::SimpleShader3::setLightPosition(
const glm::vec3& LIGHT_POSITION) {
803 glProgramUniform3fv(shaderProgramHandle, lightPositionLocation, 1, glm::value_ptr(LIGHT_POSITION));
806inline void CSCI441_INTERNAL::SimpleShader3::setLightColor(
const glm::vec3& LIGHT_COLOR) {
807 glProgramUniform3fv(shaderProgramHandle, lightColorLocation, 1, glm::value_ptr(LIGHT_COLOR));
810inline void CSCI441_INTERNAL::SimpleShader3::setMaterialColor(
const glm::vec3& MATERIAL_COLOR) {
811 glProgramUniform3fv(shaderProgramHandle, materialLocation, 1, glm::value_ptr(MATERIAL_COLOR));
814inline void CSCI441_INTERNAL::SimpleShader3::pushTransformation(
const glm::mat4& TRANSFORMATION_MATRIX) {
815 transformationStack.emplace_back(TRANSFORMATION_MATRIX);
817 modelMatrix *= TRANSFORMATION_MATRIX;
818 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
823inline void CSCI441_INTERNAL::SimpleShader3::popTransformation() {
826 if( transformationStack.size() > 1 ) {
827 transformationStack.pop_back();
829 modelMatrix = glm::mat4(1.0f);
830 for(
auto tMtx : transformationStack ) {
833 glProgramUniformMatrix4fv( shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix) );
839inline void CSCI441_INTERNAL::SimpleShader3::resetTransformationMatrix() {
840 modelMatrix = glm::mat4(1.0f);
841 transformationStack.clear();
842 transformationStack.emplace_back(modelMatrix);
843 glProgramUniformMatrix4fv(shaderProgramHandle, modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
847inline void CSCI441_INTERNAL::SimpleShader3::setNormalMatrix() {
848 glm::mat4 modelView = viewMatrix * modelMatrix;
849 glm::mat3 normalMatrix = glm::mat3( glm::transpose( glm::inverse( modelView ) ) );
850 glProgramUniformMatrix3fv(shaderProgramHandle, normalMtxLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
853inline void CSCI441_INTERNAL::SimpleShader3::enableLighting() {
854 glProgramUniform1i(shaderProgramHandle, useLightingLocation, 1);
857inline void CSCI441_INTERNAL::SimpleShader3::disableLighting() {
858 glProgramUniform1i(shaderProgramHandle, useLightingLocation, 0);
861inline void CSCI441_INTERNAL::SimpleShader3::draw(
const GLint PRIMITIVE_TYPE,
const GLuint VAOD,
const GLuint VERTEX_COUNT) {
862 glUseProgram(shaderProgramHandle);
863 glBindVertexArray(VAOD);
864 glDrawArrays(PRIMITIVE_TYPE, 0,
static_cast<GLsizei
>(VERTEX_COUNT));
Helper functions to work with OpenGL Shaders.
void enableFlatShading()
turns on Flat Shading
Definition: SimpleShader.hpp:393
void setupSimpleShader()
Registers a simple Gouraud shader for 2-Dimensional drawing.
Definition: SimpleShader.hpp:341
void setProjectionMatrix(const glm::mat4 &PROJECTION_MATRIX)
Sets the Projection Matrix.
Definition: SimpleShader.hpp:366
void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT)
loads associated VAO, drawing given primitive made up of corresponding number of vertices
Definition: SimpleShader.hpp:478
void setMaterialColor(const glm::vec3 &MATERIAL_COLOR)
sets current diffuse material color to apply to object
Definition: SimpleShader.hpp:448
void setupSimpleShader()
Registers a simple Gouraud Shader with Lambertian Illumination for 3-Dimensional drawing.
Definition: SimpleShader.hpp:403
void pushTransformation(const glm::mat4 &TRANSFORMATION_MATRIX)
Pushes a transformation to the stack and updates our model matrix.
Definition: SimpleShader.hpp:453
void enableFlatShading()
turns on Flat Shading
Definition: SimpleShader.hpp:331
void setLightPosition(const glm::vec3 &LIGHT_POSITION)
sets position of single global light in world space
Definition: SimpleShader.hpp:438
void popTransformation()
Pops the last transformation off the stack and updates our model matrix by the inverse of the last tr...
Definition: SimpleShader.hpp:458
void resetTransformationMatrix()
Sets the model matrix back to the identity matrix and clears the transformation stack.
Definition: SimpleShader.hpp:381
void setLightColor(const glm::vec3 &LIGHT_COLOR)
sets color of single global light
Definition: SimpleShader.hpp:443
void enableSmoothShading()
turns on Smooth Shading
Definition: SimpleShader.hpp:336
void disableLighting()
turns off lighting and applies material color to fragment
Definition: SimpleShader.hpp:473
void resetTransformationMatrix()
Sets the model matrix back to the identity matrix and clears the transformation stack.
Definition: SimpleShader.hpp:463
void enableSmoothShading()
turns on Smooth Shading
Definition: SimpleShader.hpp:398
void enableLighting()
turns on lighting and applies Phong Illumination to fragment
Definition: SimpleShader.hpp:468
void setViewMatrix(const glm::mat4 &VIEW_MATRIX)
Sets the View Matrix.
Definition: SimpleShader.hpp:433
void draw(GLint PRIMITIVE_TYPE, GLuint VAOD, GLuint VERTEX_COUNT)
loads associated VAO, drawing given primitive made up of corresponding number of vertices
Definition: SimpleShader.hpp:386
void pushTransformation(const glm::mat4 &TRANSFORMATION_MATRIX)
Pushes a transformation to the stack and updates our model matrix.
Definition: SimpleShader.hpp:371
void setProjectionMatrix(const glm::mat4 &PROJECTION_MATRIX)
Sets the Projection Matrix.
Definition: SimpleShader.hpp:428
void updateVertexArray(GLuint VAOD, const std::vector< glm::vec3 > &VERTEX_POINTS, const std::vector< glm::vec3 > &VERTEX_NORMALS)
Updates GL_ARRAY_BUFFER for the corresponding VAO.
Definition: SimpleShader.hpp:413
GLuint registerVertexArray(const std::vector< glm::vec3 > &VERTEX_POINTS, const std::vector< glm::vec3 > &VERTEX_NORMALS)
registers the associated vertex locations and colors with the GPU
Definition: SimpleShader.hpp:408
void popTransformation()
Pops the last transformation off the stack and updates our model matrix by the inverse of the last tr...
Definition: SimpleShader.hpp:376
void updateVertexArray(GLuint VAOD, const std::vector< glm::vec2 > &VERTEX_POINTS, const std::vector< glm::vec3 > &VERTEX_COLORS)
Updates GL_ARRAY_BUFFER for the corresponding VAO.
Definition: SimpleShader.hpp:351
GLuint registerVertexArray(const std::vector< glm::vec2 > &VERTEX_POINTS, const std::vector< glm::vec3 > &VERTEX_COLORS)
registers the associated vertex locations and colors with the GPU
Definition: SimpleShader.hpp:346
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17
void setVertexAttributeLocations(GLint positionLocation, GLint normalLocation=-1, GLint texCoordLocation=-1)
Sets the attribute locations for vertex positions, normals, and texture coordinates.
Definition: objects.hpp:552
Abstracts the process of working with a 2D shader program.
Abstracts the process of working with a 3D shader program.
Helper functions to draw 3D OpenGL 3.0+ objects.