Created
May 25, 2016 11:49
-
-
Save krysseltillada/4cadbc2f9a1fc51fade11c1fb02abc43 to your computer and use it in GitHub Desktop.
ex 16 (transformation, scaling)
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 | |
out vec4 fragmentColor; | |
smooth in vec4 outputVertexColor; | |
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 <iostream> | |
#include <stdexcept> | |
#include <string> | |
#include <cstdlib> | |
#include "Shader.hpp" | |
#define DEBUG_LOG | |
#ifdef DEBUG_LOG | |
#define LOG(X) { \ | |
std::cout << #X << std::endl; } \ | |
#endif | |
#define WINDOW_HEIGHT 600 | |
#define WINDOW_WIDTH 800 | |
#define WINDOW_XPOS 200 | |
#define WINDOW_YPOS 200 | |
const char *windowTitle = "exercise 16 (scale)"; | |
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, | |
}; | |
GLuint vao; | |
GLuint vbo; | |
GLuint shaderProgram; | |
float fovDegrees = 45.0f; | |
float zNear = 1.0f, zFar = 90.0f; | |
glm::vec3 cameraPosition; | |
glm::vec3 objectPosition; | |
glm::vec3 scaleValue; | |
glm::mat4 ModelToCameraMatrix; | |
glm::mat4 cameraTranslationMatrix(1.0f); | |
glm::mat4 translationMatrix(1.0f); | |
glm::mat4 scaleMatrix(1.0f); | |
GLuint ModelToCameraMatrixUniformLocation; | |
GLuint cameraTranlationMatrixUniformLocation; | |
GLuint translationMatrixUniformLocation; | |
GLuint scaleMatrixUniformLocation; | |
enum class ScalePoint{ | |
XVERTEX, | |
YVERTEX, | |
ZVERTEX | |
}; | |
ScalePoint scaleCoord; | |
float fovInDegrees(const float &f) { | |
return (1.0f / glm::tan(glm::radians(f) / 2.0f)); | |
} | |
bool ifInit = GL_FALSE; | |
void initObjects() { | |
const std::string vertexSourceCode = Shader::ShaderUtility::LoadShaderFile("transformation.vert"); | |
const std::string fragmentSourceCode = Shader::ShaderUtility::LoadShaderFile("fragmentColors.frag"); | |
GLuint vertexShaderObject = Shader::ShaderUtility::CompileShader(vertexSourceCode.c_str(), GL_VERTEX_SHADER); | |
GLuint fragmentShaderObject = Shader::ShaderUtility::CompileShader(fragmentSourceCode.c_str(), GL_FRAGMENT_SHADER); | |
shaderProgram = Shader::ShaderUtility::BuildProgram({ vertexShaderObject, fragmentShaderObject }); | |
scaleValue.x = 1.2f; | |
scaleValue.y = 1.0f; | |
scaleValue.z = 1.0f; | |
cameraPosition.x = 0.3f; | |
cameraPosition.y = 0.4f; | |
cameraPosition.z = 0.0f; | |
ModelToCameraMatrix[0].x = fovInDegrees(fovDegrees) / (WINDOW_WIDTH / (float)WINDOW_HEIGHT); | |
ModelToCameraMatrix[1].y = fovInDegrees(fovDegrees); | |
ModelToCameraMatrix[2].z = (zNear + zFar) / (zNear - zFar); | |
ModelToCameraMatrix[2].w = -1.0f; | |
ModelToCameraMatrix[3].z = (2 * zNear * zFar) / (zNear - zFar); | |
cameraTranslationMatrix[3].x = cameraPosition.x; | |
cameraTranslationMatrix[3].y = cameraPosition.y; | |
cameraTranslationMatrix[3].z = cameraPosition.z; | |
translationMatrix[3].x = objectPosition.x; | |
translationMatrix[3].y = objectPosition.y; | |
translationMatrix[3].z = objectPosition.z; | |
scaleMatrix[0].x = scaleValue.x; | |
scaleMatrix[1].y = scaleValue.y; | |
scaleMatrix[2].z = scaleValue.z; | |
ModelToCameraMatrixUniformLocation = glGetUniformLocation(shaderProgram, "ModelToCameraMatrix"); | |
cameraTranlationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "cameraTranslationMatrix"); | |
scaleMatrixUniformLocation = glGetUniformLocation(shaderProgram, "scaleMatrix"); | |
translationMatrixUniformLocation = glGetUniformLocation(shaderProgram, "translationMatrix"); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(ModelToCameraMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ModelToCameraMatrix)); | |
glUniformMatrix4fv(cameraTranlationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraTranslationMatrix)); | |
glUniformMatrix4fv(scaleMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(scaleMatrix)); | |
glUniformMatrix4fv(translationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(translationMatrix)); | |
glUseProgram(shaderProgram); | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void *)0); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *)576); | |
glBindVertexArray(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 Update() { | |
translationMatrix[3].x = objectPosition.x; | |
translationMatrix[3].y = objectPosition.y; | |
translationMatrix[3].z = objectPosition.z; | |
cameraTranslationMatrix[3].x = cameraPosition.x; | |
cameraTranslationMatrix[3].y = cameraPosition.y; | |
cameraTranslationMatrix[3].z = cameraPosition.z; | |
scaleMatrix[0].x = scaleValue.x; | |
scaleMatrix[1].y = scaleValue.y; | |
scaleMatrix[2].z = scaleValue.z; | |
if (scaleMatrix[0].x < 0 || scaleMatrix[1].y < 0 || scaleMatrix[2].z < 0) | |
glDisable(GL_CULL_FACE); | |
else | |
glEnable(GL_CULL_FACE); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(translationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr (translationMatrix)); | |
glUniformMatrix4fv(cameraTranlationMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraTranslationMatrix)); | |
glUniformMatrix4fv(scaleMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(scaleMatrix)); | |
glUseProgram(0); | |
} | |
void Clear() { | |
glClearDepth(1.0f); | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
} | |
void Render() { | |
glUseProgram(shaderProgram); | |
glBindVertexArray(vao); | |
glDrawArrays(GL_TRIANGLES, 0, 36); | |
glBindVertexArray(vao); | |
glUseProgram(0); | |
glutSwapBuffers(); | |
glutPostRedisplay(); | |
} | |
void Run() { | |
if (!ifInit) { | |
initObjects(); | |
ifInit = GL_TRUE; | |
} | |
Clear(); | |
Update(); | |
Render(); | |
} | |
void Keyboard(unsigned char key, int x, int y) { | |
switch (key) { | |
case 'w': | |
cameraPosition.z -= 0.1f; | |
break; | |
case 's': | |
cameraPosition.z += 0.1f; | |
break; | |
case 'a': | |
cameraPosition.x -= 0.1f; | |
break; | |
case 'd': | |
cameraPosition.x += 0.1f; | |
break; | |
case 'i': | |
objectPosition.z -= 0.1f; | |
break; | |
case 'k': | |
objectPosition.z += 0.1f; | |
break; | |
case 'j': | |
objectPosition.x -= 0.1f; | |
break; | |
case 'l': | |
objectPosition.x += 0.1f; | |
break; | |
case 'z': | |
LOG("scale z"); | |
scaleCoord = ScalePoint::ZVERTEX; | |
break; | |
case 'x': | |
LOG("scale x"); | |
scaleCoord = ScalePoint::XVERTEX; | |
break; | |
case 'c': | |
LOG("scale y"); | |
scaleCoord = ScalePoint::YVERTEX; | |
break; | |
case 'n': | |
switch (scaleCoord) { | |
case ScalePoint::XVERTEX: | |
scaleValue.x += 0.1f; | |
break; | |
case ScalePoint::YVERTEX: | |
scaleValue.y += 0.1f; | |
break; | |
case ScalePoint::ZVERTEX: | |
scaleValue.z += 0.1f; | |
break; | |
default: | |
break; | |
} | |
break; | |
case 'm': | |
switch (scaleCoord) { | |
case ScalePoint::XVERTEX: | |
scaleValue.x -= 0.1f; | |
break; | |
case ScalePoint::YVERTEX: | |
scaleValue.y -= 0.1f; | |
break; | |
case ScalePoint::ZVERTEX: | |
scaleValue.z -= 0.1f; | |
break; | |
default: | |
break; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
void Resize(int w, int h) { | |
ModelToCameraMatrix[0].x = fovInDegrees(fovDegrees) / (w / (float)h); | |
ModelToCameraMatrix[1].y = fovInDegrees(fovDegrees); | |
glUseProgram(shaderProgram); | |
glUniformMatrix4fv(ModelToCameraMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ModelToCameraMatrix)); | |
glUseProgram(0); | |
glViewport((GLint)0, (GLint)0, (GLint)w, (GLint)h); | |
} | |
void initWindow(int *const p_argn, char **const p_argc) { | |
glutInit(p_argn, p_argc); | |
glutInitContextProfile(GLUT_CORE_PROFILE); | |
glutInitContextVersion(3, 3); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); | |
glutInitWindowPosition(WINDOW_XPOS, WINDOW_YPOS); | |
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | |
glutCreateWindow(windowTitle); | |
if (glewInit()) { | |
std::cerr << "cannot initialized the context" << std::endl; | |
return; | |
} | |
glutDisplayFunc(&Run); | |
glutKeyboardFunc(&Keyboard); | |
glutReshapeFunc(&Resize); | |
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; | |
uniform mat4 translationMatrix; | |
uniform mat4 ModelToCameraMatrix; | |
uniform mat4 cameraTranslationMatrix; | |
uniform mat4 scaleMatrix; | |
smooth out vec4 outputVertexColor; | |
void main () | |
{ | |
vec4 cameraSpace = translationMatrix * (scaleMatrix * inputVertexPosition); | |
mat4 cameraTranslationMatrixNegation = cameraTranslationMatrix; | |
cameraTranslationMatrixNegation[3].x = -cameraTranslationMatrixNegation[3].x; | |
cameraTranslationMatrixNegation[3].y = -cameraTranslationMatrixNegation[3].y; | |
cameraTranslationMatrixNegation[3].z = -cameraTranslationMatrixNegation[3].z; | |
cameraSpace = cameraTranslationMatrixNegation * cameraSpace; | |
gl_Position = ModelToCameraMatrix * cameraSpace; | |
outputVertexColor = inputVertexColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment