Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created May 21, 2016 17:17
Show Gist options
  • Save krysseltillada/91f0396de358d3c8ff852215a0095046 to your computer and use it in GitHub Desktop.
Save krysseltillada/91f0396de358d3c8ff852215a0095046 to your computer and use it in GitHub Desktop.
ex 15 translation, model space, transformation, identity matrix
#ifndef CAMERA_HEADER
#define CAMERA_HEADER
#include <glm.hpp>
#include <stdexcept>
class Camera {
public:
Camera() try :
position(glm::vec3(0.0f, 0.0f, 0.0f)), fov(0.0f), zNear(1.0f), zFar(0.0f) {
if (zNear <= 0)
throw std::runtime_error("zNear cannot be less than or equal to zero");
}
catch (std::runtime_error a) {
if (zNear <= 0)
throw std::runtime_error("zNear cannot be less than or equal to zero");
}
float getAngleFov() {
float fovToRad = glm::radians(this->fov);
return 1.0f / (glm::tan(fovToRad) / 2.0f);
}
glm::vec3 position;
float fov, zNear, zFar;
};
#endif
#version 330
in vec4 outputVertexColor;
out vec4 outputFragmentColor;
void main ()
{
outputFragmentColor = outputVertexColor;
}
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdexcept>
#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>
#include "Shader.hpp"
#include "Camera.hpp"
#include <glm.hpp>
#include <gtc/type_ptr.hpp>
#define DEBUG_MODE
#ifdef DEBUG_MODE
template <typename T>
inline void LOG(const T &log) {
std::clog << log << std::endl;
}
#endif
namespace WindowProperties {
std::string windowTitle = "exercise 15 (translation)";
const int Width = 800;
const int Height = 600;
glm::vec2 windowPosition;
unsigned short int displayMode = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH;
}
const int numberOfVertices = 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 vertexData[] =
{
+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 indexData[] =
{
0, 1, 2,
1, 0, 3,
2, 3, 0,
3, 2, 1,
5, 4, 6,
4, 5, 7,
7, 6, 4,
6, 7, 5,
};
const float vertexData2[] = {
//** 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, 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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,
};
bool ifInit = GL_FALSE;
float height = 0.0f, width = 0.0f;
glm::mat4 cameraSpaceMatrix(1.0f);
glm::mat4 modelSpaceMatrix(1.0f);
glm::mat4 scaleMatrix(1.0f);
glm::vec3 objectPosition;
GLuint vertexArrayObject;
GLuint vertexBufferObject;
GLuint elementBufferObject;
Camera camera;
GLuint shaderProgramObject;
GLuint cameraSpaceMatrixUniformLocation;
GLuint modelSpaceMatrixUniformLocation;
GLuint cameraOffsetUniformLocation;
GLuint inputVertexPositionAttributeLocation;
GLuint inputVertexColorAttributeLocation;
void InitObjects() {
GLuint vertexShaderObject;
GLuint fragmentShaderObject;
LOG("initObjects()");
camera.position.x = 0.3f;
camera.position.y = 0.5f;
camera.position.z = 0.0f;
camera.fov = 45.0f;
camera.zNear = 1.0f;
camera.zFar = 90.0f;
objectPosition.x = 0.0f;
objectPosition.y = 0.0f;
objectPosition.z = 0.0f;
modelSpaceMatrix[3].x = objectPosition.x;
modelSpaceMatrix[3].y = objectPosition.y;
modelSpaceMatrix[3].z = objectPosition.z;
cameraSpaceMatrix[0].x = camera.getAngleFov() / (WindowProperties::Width / GLfloat(WindowProperties::Height));
cameraSpaceMatrix[1].y = camera.getAngleFov();
cameraSpaceMatrix[2].z = (camera.zNear + camera.zFar) / (camera.zNear - camera.zFar);
cameraSpaceMatrix[3].z = (2 * camera.zNear * camera.zFar) / (camera.zNear - camera.zFar);
cameraSpaceMatrix[2].w = -1.0f;
const std::string vertexSource = Shader::ShaderUtility::LoadShaderFile("transformation.vert");
const std::string fragmentSource = Shader::ShaderUtility::LoadShaderFile("fragmentColor.frag");
vertexShaderObject = Shader::ShaderUtility::CompileShader(vertexSource.c_str(), GL_VERTEX_SHADER);
fragmentShaderObject = Shader::ShaderUtility::CompileShader(fragmentSource.c_str(), GL_FRAGMENT_SHADER);
shaderProgramObject = Shader::ShaderUtility::BuildProgram({ vertexShaderObject, fragmentShaderObject });
cameraSpaceMatrixUniformLocation = glGetUniformLocation(shaderProgramObject, "cameraSpaceMatrix");
modelSpaceMatrixUniformLocation = glGetUniformLocation(shaderProgramObject, "modelSpaceMatrix");
cameraOffsetUniformLocation = glGetUniformLocation(shaderProgramObject, "cameraOffset");
glUseProgram(shaderProgramObject);
glUniformMatrix4fv(cameraSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraSpaceMatrix));
glUniformMatrix4fv(modelSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(modelSpaceMatrix));
glUniform3f(cameraOffsetUniformLocation, camera.position.x, camera.position.y, camera.position.z);
glUseProgram(0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthRange(0.0f, 1.0f);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_CLAMP);
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData2), vertexData2, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &elementBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof (indexData), indexData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
inputVertexPositionAttributeLocation = glGetAttribLocation(shaderProgramObject, "inputVertexPosition");
inputVertexColorAttributeLocation = glGetAttribLocation(shaderProgramObject, "inputVertexColor");
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
glEnableVertexAttribArray(inputVertexPositionAttributeLocation);
glEnableVertexAttribArray(inputVertexColorAttributeLocation);
glVertexAttribPointer(inputVertexPositionAttributeLocation, 4, GL_FLOAT, GL_FALSE, 0, (void *)0);
glVertexAttribPointer(inputVertexColorAttributeLocation, 4, GL_FLOAT, GL_FALSE, 0, (void *) ( sizeof(vertexData2) / 2) );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBindVertexArray(0);
glDisableVertexAttribArray(inputVertexPositionAttributeLocation);
glDisableVertexAttribArray(inputVertexColorAttributeLocation);
}
void KeyEvents(unsigned char key, int x, int y) {
switch (key) {
case 'e':
glutExit();
break;
case 'w':
objectPosition.z += 0.1;
break;
case 's':
objectPosition.z -= 0.1;
break;
case 'a':
objectPosition.x -= 0.1;
break;
case 'd':
objectPosition.x += 0.1;
break;
case 'i':
camera.position.y += 0.1;
break;
case 'k':
camera.position.y -= 0.1;
break;
case 'j':
camera.position.x -= 0.1;
break;
case 'l':
camera.position.x += 0.1;
break;
default:
break;
}
}
void Resize(int w, int h) {
width = w;
height = h;
cameraSpaceMatrix[0].x = camera.getAngleFov() / (width / height);
cameraSpaceMatrix[1].y = camera.getAngleFov();
glUseProgram(shaderProgramObject);
glUniformMatrix4fv(cameraSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(cameraSpaceMatrix));
glUseProgram(0);
glViewport(GLint(0), GLint(0), GLint(w), GLint(h));
}
void Render() {
glUseProgram(shaderProgramObject);
glBindVertexArray(vertexArrayObject);
modelSpaceMatrix[3].x = objectPosition.x;
modelSpaceMatrix[3].y = objectPosition.y;
modelSpaceMatrix[3].z = objectPosition.z;
glUniformMatrix4fv(modelSpaceMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(modelSpaceMatrix));
glUniform3f(cameraOffsetUniformLocation, camera.position.x, camera.position.y, camera.position.z);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
void Clear() {
glClearDepth(1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Run() {
if (!ifInit) {
ifInit = GL_TRUE;
InitObjects();
}
Clear();
Render();
}
void initWindow(int *const cp_argn, char **const cp_argc) {
LOG("initializing window properties...");
glutInit(cp_argn, cp_argc);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitContextVersion(3, 3);
glutInitDisplayMode(WindowProperties::displayMode);
glutInitWindowPosition(WindowProperties::windowPosition.x, WindowProperties::windowPosition.y);
glutInitWindowSize(WindowProperties::Width, WindowProperties::Height);
glutCreateWindow(WindowProperties::windowTitle.c_str());
glewExperimental = GL_TRUE;
try {
if (glewInit()) {
LOG("error initializing the context");
throw std::runtime_error("error initializing the context");
}
else {
LOG(glGetString(GL_VERSION));
}
}
catch (std::runtime_error errLog) {
LOG("reinitializing the context");
if (glewInit())
throw std::runtime_error("cant initialize the context check your video card drivers");
}
glutDisplayFunc(&Run);
glutKeyboardFunc(&KeyEvents);
glutReshapeFunc(&Resize);
glutMainLoop();
}
int main(int argn, char **argc)
{
initWindow(&argn, argc);
return EXIT_SUCCESS;
}
#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;
}
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;
}
for (auto shaderListIt = shaderList.begin(); shaderListIt != shaderList.end(); ++shaderListIt) {
glDetachShader(programObject, *shaderListIt);
glDeleteShader(*shaderListIt);
}
return programObject;
}
};
}
#endif
#version 330
in vec4 inputVertexPosition;
in vec4 inputVertexColor;
smooth out vec4 outputVertexColor;
uniform mat4 cameraSpaceMatrix;
uniform mat4 modelSpaceMatrix;
uniform vec3 cameraOffset;
void main ()
{
vec4 cameraSpace = modelSpaceMatrix * inputVertexPosition;
cameraSpace.xyz -= cameraOffset.xyz;
gl_Position = cameraSpaceMatrix * cameraSpace;
outputVertexColor = inputVertexColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment