Created
June 4, 2016 03:58
-
-
Save krysseltillada/4c4fed96cfef20b80a46062d0752c75b to your computer and use it in GitHub Desktop.
ex 17 (transformations, Rotations)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
smooth in vec4 outputVertexColor; | |
out vec4 fragmentColor; | |
void main () | |
{ | |
fragmentColor = outputVertexColor; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GLEW_STATIC | |
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#include <glm.hpp> | |
#include <gtc/type_ptr.hpp> | |
#include <cstdlib> | |
#include <iostream> | |
#include <string> | |
#include <stdexcept> | |
#include <cstring> | |
#include "Shader.hpp" | |
namespace windowConfiguration { | |
const std::string windowTitle("exercise17 (Rotations)"); | |
const GLuint width = 800; | |
const GLuint height = 600; | |
const GLuint windowXPOS = 200; | |
const GLuint windowYPOS = 200; | |
const GLushort displayMode = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH; | |
const GLuint coreProfile = GLUT_CORE_PROFILE; | |
const GLushort openGLMINVERSION = 3, openGLMAXVERSION = 3; | |
} | |
const GLuint vertices = 36; | |
const float vertexData[] = { | |
//** position of every vertex of a Cube **// | |
0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, -0.25f, -1.25f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, -0.25f, -1.25f, 1.0f, | |
-0.25f, -0.25f, -1.25f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, 0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -2.75f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -2.75f, 1.0f, | |
-0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
-0.25f, -0.25f, -1.25f, 1.0f, | |
-0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
-0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -2.75f, 1.0f, | |
0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
0.25f, -0.25f, -1.25f, 1.0f, | |
0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, 0.25f, -2.75f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
0.25f, 0.25f, -2.75f, 1.0f, | |
0.25f, 0.25f, -1.25f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
0.25f, 0.25f, -2.75f, 1.0f, | |
-0.25f, 0.25f, -1.25f, 1.0f, | |
-0.25f, 0.25f, -2.75f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, -0.25f, -1.25f, 1.0f, | |
0.25f, -0.25f, -1.25f, 1.0f, | |
0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, -0.25f, -2.75f, 1.0f, | |
-0.25f, -0.25f, -1.25f, 1.0f, | |
//** color of each vertex of the cube **// | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
}; | |
const int numberOfVertices2 = 8; | |
#define GREEN_COLOR 0.0f, 1.0f, 0.0f, 1.0f | |
#define BLUE_COLOR 0.0f, 0.0f, 1.0f, 1.0f | |
#define RED_COLOR 1.0f, 0.0f, 0.0f, 1.0f | |
#define GREY_COLOR 0.8f, 0.8f, 0.8f, 1.0f | |
#define BROWN_COLOR 0.5f, 0.5f, 0.0f, 1.0f | |
const float vertexData2[] = | |
{ | |
+1.0f, +1.0f, +1.0f, | |
-1.0f, -1.0f, +1.0f, | |
-1.0f, +1.0f, -1.0f, | |
+1.0f, -1.0f, -1.0f, | |
-1.0f, -1.0f, -1.0f, | |
+1.0f, +1.0f, -1.0f, | |
+1.0f, -1.0f, +1.0f, | |
-1.0f, +1.0f, +1.0f, | |
GREEN_COLOR, | |
BLUE_COLOR, | |
RED_COLOR, | |
BROWN_COLOR, | |
GREEN_COLOR, | |
BLUE_COLOR, | |
RED_COLOR, | |
BROWN_COLOR, | |
}; | |
const GLshort indexData2[] = | |
{ | |
0, 1, 2, | |
1, 0, 3, | |
2, 3, 0, | |
3, 2, 1, | |
5, 4, 6, | |
4, 5, 7, | |
7, 6, 4, | |
6, 7, 5, | |
}; | |
bool ifInit = GL_FALSE; | |
GLuint vbo; | |
GLuint vao; | |
GLuint ebo; | |
GLuint shaderProgram; | |
glm::mat4 TranslationMatrix(1.0f); | |
glm::mat4 scaleMatrix; | |
glm::mat4 cameraTranslationMatrix(1.0f); | |
glm::mat4 cameraToClipSpace; | |
glm::mat4 XrotationMatrix(1.0f); | |
glm::mat4 YrotationMatrix(1.0f); | |
glm::mat4 ZrotationMatrix(1.0f); | |
glm::mat4 axisRotationMatrix(1.0f); | |
glm::vec3 axis; | |
GLushort elementArray[6 * 6]; | |
GLuint TranslationMatrixUniformLocation; | |
GLuint scaleMatrixUniformLocation; | |
GLuint cameraToClipSpaceMatrixUniformLocation; | |
GLuint cameraTranslationMatrixUniformLocation; | |
GLuint XrotationMatrixUniformLocation; | |
GLuint YrotationMatrixUniformLocation; | |
GLuint ZrotationMatrixUniformLocation; | |
GLuint axisRotationMatrixUniformLocation; | |
float xAngle = 0.0, yAngle = 0.0f, zAngle = 0.0f; | |
float Angle = 0; | |
float zNear = 1.0f, zFar = 91.0f, fov = 45.0f; | |
float xObject = 0.0f, yObject = 0.0f, zObject = -12.5f; | |
float xCamera = 0.0f, yCamera = 0.0f, zCamera = 0.0f; | |
float xScale = 1.0f, yScale = 1.0f, zScale = 1.0f; | |
glm::vec3 offset; | |
float calcAngleFrustumScale(const float &angle) { | |
return 1 / glm::tan(glm::radians(angle) / 2.0f); | |
} | |
void Update() { | |
offset.x = xObject; | |
offset.y = yObject; | |
offset.z = zObject; | |
XrotationMatrix[1].y = glm::cos(xAngle); | |
XrotationMatrix[1].z = glm::sin(xAngle); | |
XrotationMatrix[2].y = -glm::sin(xAngle); | |
XrotationMatrix[2].z = glm::cos(xAngle); | |
YrotationMatrix[0].x = glm::cos(yAngle); | |
YrotationMatrix[0].z = -glm::sin(yAngle); | |
YrotationMatrix[2].x = glm::sin(yAngle); | |
YrotationMatrix[2].z = glm::cos(yAngle); | |
ZrotationMatrix[0].x = glm::cos(zAngle); | |
ZrotationMatrix[0].y = glm::sin(zAngle); | |
ZrotationMatrix[1].x = -glm::sin(zAngle); | |
ZrotationMatrix[1].y = glm::cos(zAngle); | |
float C = glm::cos(Angle); | |
float iC = 1 - C; | |
float S = glm::sin(Angle); | |
float iS = 1 - S; | |
axisRotationMatrix[0].x = (axis.x * axis.x) + ((1 - axis.x * axis.x) * C); | |
axisRotationMatrix[1].x = axis.x * axis.y * (iC)-(axis.z * S); | |
axisRotationMatrix[2].x = axis.x * axis.z * (iC)+(axis.y * S); | |
axisRotationMatrix[0].y = axis.x * axis.y * (iC)+(axis.z * S); | |
axisRotationMatrix[1].y = (axis.y * axis.y) + ((1 - axis.y * axis.y) * C); | |
axisRotationMatrix[2].y = axis.y * axis.z * (iC)-(axis.x * S); | |
axisRotationMatrix[0].z = axis.x * axis.z * (iC)-(axis.y * S); | |
axisRotationMatrix[1].z = axis.y * axis.z * (iC)+(axis.x * S); | |
axisRotationMatrix[2].z = (axis.z * axis.z) + ((1 - axis.z * axis.z) * C); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(XrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(XrotationMatrix)); | |
glUniformMatrix4fv(YrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(YrotationMatrix)); | |
glUniformMatrix4fv(ZrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ZrotationMatrix)); | |
glUniformMatrix4fv(TranslationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(TranslationMatrix)); | |
glUniformMatrix4fv(axisRotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(axisRotationMatrix)); | |
glUseProgram(0); | |
} | |
void Clear() { | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClearDepth(1.0f); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
} | |
void Render() { | |
glUseProgram(shaderProgram); | |
glBindVertexArray(vao); | |
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_SHORT, (void *)0); | |
glBindVertexArray(0); | |
glUseProgram(0); | |
glutSwapBuffers(); | |
glutPostRedisplay(); | |
} | |
void initObjects() { | |
axis.x = 1.0f; | |
axis.y = 1.0f; | |
axis.z = 1.0f; | |
offset.x = xObject; | |
offset.y = yObject; | |
offset.z = zObject; | |
axis = glm::normalize(axis); | |
const std::string vertexShaderCode = Shader::ShaderUtility::LoadShaderFile("transformation.vert"); | |
const std::string fragmentShaderCode = Shader::ShaderUtility::LoadShaderFile("fragmentColor.frag"); | |
GLuint vertexShaderObject = Shader::ShaderUtility::CompileShader(vertexShaderCode.c_str(), GL_VERTEX_SHADER); | |
GLuint fragmentShaderObject = Shader::ShaderUtility::CompileShader(fragmentShaderCode.c_str(), GL_FRAGMENT_SHADER); | |
shaderProgram = Shader::ShaderUtility::BuildProgram({ vertexShaderObject, fragmentShaderObject }); | |
GLuint inputVertexPositionAttribLocation = glGetAttribLocation(shaderProgram, "inputVertexPosition"); | |
GLuint inputVertexColorAttribLocation = glGetAttribLocation(shaderProgram, "inputVertexColor"); | |
TranslationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "TranslationMatrix"); | |
scaleMatrixUniformLocation = glGetUniformLocation(shaderProgram, "scaleMatrix"); | |
cameraToClipSpaceMatrixUniformLocation = glGetUniformLocation(shaderProgram, "cameraToClipSpaceMatrix"); | |
cameraTranslationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "cameraTranslationMatrix"); | |
XrotationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "XrotationMatrix"); | |
YrotationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "YrotationMatrix"); | |
ZrotationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "ZrotationMatrix"); | |
axisRotationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "axisRotationMatrix"); | |
for (GLushort elementCount = 0; elementCount != vertices; ++elementCount) | |
elementArray[elementCount] = elementCount; | |
for (GLuint count = 0; count != vertices; ++count) | |
std::cout << elementArray[count] << " "; | |
glGenBuffers(1, &vbo); | |
glGenBuffers(1, &ebo); | |
glGenVertexArrays(1, &vao); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData2), vertexData2, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof (indexData2), indexData2, GL_STATIC_DRAW); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
cameraToClipSpace[0].x = calcAngleFrustumScale(fov) / (windowConfiguration::width / windowConfiguration::height); | |
cameraToClipSpace[1].y = calcAngleFrustumScale(fov); | |
cameraToClipSpace[2].z = (zNear + zFar) + (zNear - zFar); | |
cameraToClipSpace[2].w = -1.0f; | |
cameraToClipSpace[3].z = (2 * zNear * zFar) / (zNear - zFar); | |
TranslationMatrix[3].x = xObject; | |
TranslationMatrix[3].y = yObject; | |
TranslationMatrix[3].z = zObject; | |
scaleMatrix[0].x = xScale; | |
scaleMatrix[1].y = yScale; | |
scaleMatrix[2].z = zScale; | |
cameraTranslationMatrix[3].x = xCamera; | |
cameraTranslationMatrix[3].y = yCamera; | |
cameraTranslationMatrix[3].z = zCamera; | |
XrotationMatrix[1].y = glm::cos(xAngle); | |
XrotationMatrix[1].z = glm::sin(xAngle); | |
XrotationMatrix[2].y = -glm::sin(xAngle); | |
XrotationMatrix[2].z = glm::cos(xAngle); | |
XrotationMatrix[3] = glm::vec4(offset, 1.0f); | |
YrotationMatrix[0].x = glm::cos(yAngle); | |
YrotationMatrix[0].z = -glm::sin(yAngle); | |
YrotationMatrix[2].x = glm::sin(yAngle); | |
YrotationMatrix[2].z = glm::cos(yAngle); | |
ZrotationMatrix[0].x = glm::cos(zAngle); | |
ZrotationMatrix[0].y = glm::sin(zAngle); | |
ZrotationMatrix[1].x = -glm::sin(zAngle); | |
ZrotationMatrix[1].y = glm::cos(zAngle); | |
float C = glm::cos(Angle); | |
float iC = 1 - C; | |
float S = glm::sin(Angle); | |
float iS = 1 - S; | |
axisRotationMatrix[0].x = (axis.x * axis.x) + ((1 - axis.x * axis.x) * C); | |
axisRotationMatrix[1].x = axis.x * axis.y * (iC) - (axis.z * S); | |
axisRotationMatrix[2].x = axis.x * axis.z * (iC) + (axis.y * S); | |
axisRotationMatrix[0].y = axis.x * axis.y * (iC) + (axis.z * S); | |
axisRotationMatrix[1].y = (axis.y * axis.y) + ((1 - axis.y * axis.y) * C); | |
axisRotationMatrix[2].y = axis.y * axis.z * (iC) - (axis.x * S); | |
axisRotationMatrix[0].z = axis.x * axis.z * (iC) - (axis.y * S); | |
axisRotationMatrix[1].z = axis.y * axis.z * (iC) + (axis.x * S); | |
axisRotationMatrix[2].z = (axis.z * axis.z) + ((1 - axis.z * axis.z) * C); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(cameraToClipSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraToClipSpace)); | |
glUniformMatrix4fv(cameraTranslationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraTranslationMatrix) ); | |
glUniformMatrix4fv(TranslationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(TranslationMatrix)); | |
glUniformMatrix4fv(scaleMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(scaleMatrix)); | |
glUniformMatrix4fv(XrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(XrotationMatrix)); | |
glUniformMatrix4fv(YrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(YrotationMatrix)); | |
glUniformMatrix4fv(ZrotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ZrotationMatrix)); | |
glUniformMatrix4fv(axisRotationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(axisRotationMatrix)); | |
glUseProgram(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBindVertexArray(vao); | |
size_t colorDataOffset = sizeof(float)* 3 * numberOfVertices2; | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)colorDataOffset); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); | |
glBindVertexArray(0); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
glFrontFace(GL_CW); | |
glEnable(GL_DEPTH_TEST); | |
glDepthFunc(GL_LEQUAL); | |
glDepthRange(0.0f, 1.0f); | |
glDepthMask(GL_TRUE); | |
glEnable(GL_DEPTH_CLAMP); | |
} | |
void Run() { | |
if (!ifInit) { | |
initObjects(); | |
ifInit = GL_TRUE; | |
} | |
Clear(); | |
Render(); | |
Update(); | |
} | |
void ResizeWindow(int w, int h) { | |
glViewport(0, 0, w, h); | |
cameraToClipSpace[0].x = calcAngleFrustumScale(fov) / (w / (float)h); | |
cameraToClipSpace[1].y = calcAngleFrustumScale(fov); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(cameraToClipSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraToClipSpace)); | |
glUseProgram(0); | |
} | |
void KeyboardEvent(unsigned char key, int x, int y) { | |
switch (key) { | |
case 'z': | |
zAngle += 0.5f; | |
break; | |
case 'x': | |
xAngle += 0.5f; | |
break; | |
case 'c': | |
yAngle += 0.5f; | |
break; | |
case 'v': | |
zAngle -= 0.5f; | |
break; | |
case 'b': | |
xAngle -= 0.5f; | |
break; | |
case 'n': | |
yAngle -= 0.5f; | |
break; | |
case 'j': | |
Angle += 0.5f; | |
break; | |
case 'k': | |
Angle -= 0.5f; | |
break; | |
default: | |
break; | |
} | |
} | |
void initWindow(int *const c_pargn, char **const c_argc) { | |
glutInit(c_pargn, c_argc); | |
glutInitContextVersion(windowConfiguration::openGLMAXVERSION, windowConfiguration::openGLMINVERSION); | |
glutInitContextProfile(windowConfiguration::coreProfile); | |
glutInitDisplayMode(windowConfiguration::displayMode); | |
glutInitWindowPosition(windowConfiguration::windowXPOS, windowConfiguration::windowYPOS); | |
glutInitWindowSize(windowConfiguration::width, windowConfiguration::height); | |
glutCreateWindow(windowConfiguration::windowTitle.c_str()); | |
glewExperimental = GL_TRUE; | |
if (glewInit()) | |
std::cerr << "cannot initialized the context" << std::endl; | |
glutDisplayFunc(&Run); | |
glutKeyboardFunc(&KeyboardEvent); | |
glutReshapeFunc(&ResizeWindow); | |
glutMainLoop(); | |
} | |
int main(int argn, char **argc) | |
{ | |
initWindow(&argn, argc); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SHADER_UTILITY_HEADER | |
#define SHADER_UTILITY_HEADER | |
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#include <fstream> | |
#include <vector> | |
#include <iostream> | |
#include <initializer_list> | |
#include <string> | |
namespace Shader { | |
class ShaderUtility { | |
public: | |
static std::string LoadShaderFile(const std::string &fileName) { | |
std::fstream streamReader(fileName, std::fstream::in, std::fstream::trunc); | |
std::string sourceCode, line; | |
if (!streamReader) | |
std::cerr << "cannot load file " << fileName << std::endl; | |
for (; std::getline(streamReader, line);) | |
sourceCode += line + '\n'; | |
if (sourceCode[sourceCode.length() - 1] != '\0') | |
sourceCode.push_back('\0'); | |
streamReader.close(); | |
return sourceCode; | |
} | |
static GLuint CompileShader(const char *sourceCode, GLenum shaderType) { | |
GLuint ShaderObject = glCreateShader(shaderType); | |
GLint compileStatus; | |
glShaderSource(ShaderObject, 1, &sourceCode, nullptr); | |
glCompileShader(ShaderObject); | |
glGetShaderiv(ShaderObject, GL_COMPILE_STATUS, &compileStatus); | |
if (!compileStatus) { | |
GLchar *errLog; | |
GLint logLength; | |
glGetShaderiv(ShaderObject, GL_INFO_LOG_LENGTH, &logLength); | |
errLog = new GLchar[logLength + 1]; | |
glGetShaderInfoLog(ShaderObject, logLength + 1, nullptr, errLog); | |
std::cerr << "shader error:: " << ((shaderType == GL_VERTEX_SHADER) ? "vertex " : (shaderType == GL_FRAGMENT_SHADER) ? "fragment " : "none") << std::endl | |
<< errLog << std::endl; | |
delete[]errLog; | |
errLog = nullptr; | |
} | |
return ShaderObject; | |
} | |
static GLuint BuildProgram(std::initializer_list <GLuint> shaderList) { | |
GLuint programObject = glCreateProgram(); | |
GLint linkStatus; | |
for (auto shaderListIt = shaderList.begin(); shaderListIt != shaderList.end(); ++shaderListIt) | |
glAttachShader(programObject, *shaderListIt); | |
glLinkProgram(programObject); | |
glGetProgramiv(programObject, GL_LINK_STATUS, &linkStatus); | |
if (!linkStatus) { | |
GLchar *errLog; | |
GLint logLength; | |
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &logLength); | |
errLog = new GLchar[logLength + 1]; | |
glGetProgramInfoLog(programObject, logLength + 1, nullptr, errLog); | |
std::cerr << "linking error" << std::endl | |
<< errLog << std::endl; | |
delete[]errLog; | |
errLog = nullptr; | |
} | |
for (auto shaderListIt = shaderList.begin(); shaderListIt != shaderList.end(); ++shaderListIt) { | |
glDetachShader(programObject, *shaderListIt); | |
glDeleteShader(*shaderListIt); | |
} | |
return programObject; | |
} | |
}; | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
layout (location = 0) in vec4 inputVertexPosition; | |
layout (location = 1) in vec4 inputVertexColor; | |
smooth out vec4 outputVertexColor; | |
uniform mat4 TranslationMatrix; | |
uniform mat4 scaleMatrix; | |
uniform mat4 XrotationMatrix; | |
uniform mat4 YrotationMatrix; | |
uniform mat4 ZrotationMatrix; | |
uniform mat4 axisRotationMatrix; | |
uniform mat4 cameraTranslationMatrix; | |
uniform mat4 cameraToClipSpaceMatrix; | |
void main () | |
{ | |
vec4 cameraSpace = TranslationMatrix * (XrotationMatrix * YrotationMatrix * ZrotationMatrix * axisRotationMatrix * (scaleMatrix * inputVertexPosition)); | |
cameraSpace = cameraTranslationMatrix * cameraSpace; | |
vec4 cameraClipSpace = cameraToClipSpaceMatrix * cameraSpace; | |
gl_Position = cameraClipSpace; | |
outputVertexColor = inputVertexColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment