Created
March 11, 2016 18:12
-
-
Save krysseltillada/3872a1950c69c386e70b to your computer and use it in GitHub Desktop.
exercise 6 (perspective projection)
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 <iostream> | |
#include <string> | |
#include "main.hpp" | |
static const int windowWidth = 800; | |
static const int windowHeight = 600; | |
static const char *windowTitle = "exercise 5"; | |
void InitCoreFunc(int *const argn, char **cargs) { | |
glutInit(argn, cargs); | |
glutInitContextProfile(GLUT_CORE_PROFILE); | |
glutInitContextVersion(3, 3); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); | |
glutInitWindowSize(windowWidth, windowHeight); | |
glutInitWindowPosition(200, 200); | |
glutCreateWindow(windowTitle); | |
if (glewInit()) | |
std::cerr << "cannot init open gl context " << std::endl; | |
glutDisplayFunc(&Run); | |
glutReshapeFunc(&Resize); | |
glutMainLoop(); | |
} | |
int main(int argn, char **argc) | |
{ | |
InitCoreFunc(&argn, argc); | |
return 0; | |
} |
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
#include "main.hpp" | |
bool ifInit = false; | |
const GLfloat 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, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.8f, 0.8f, 0.8f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
1.0f, 0.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
0.0f, 1.0f, 1.0f, 1.0f, | |
}; | |
GLuint vertexBufferObject; | |
GLuint vertexShaderObject; | |
GLuint fragmentShaderObject; | |
GLuint programObject; | |
GLint offsetUniformLocation; | |
GLint zNearUniformLocation; | |
GLint zFarUniformLocation; | |
GLint frustumScaleUniformLocation; | |
static void InitObject() { | |
std::string vertexStrCode; | |
std::string fragmentStrCode; | |
const char *vertexCode; | |
const char *fragmentCode; | |
glGenBuffers(1, &vertexBufferObject); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(Cube), Cube, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
vertexStrCode = Shader::LoadShaderFile("PerspectiveProjection.vert"); | |
fragmentStrCode = Shader::LoadShaderFile("verticesColor.frag"); | |
vertexCode = vertexStrCode.c_str(); | |
fragmentCode = fragmentStrCode.c_str(); | |
vertexShaderObject = Shader::CompileShader(GL_VERTEX_SHADER, vertexCode); | |
fragmentShaderObject = Shader::CompileShader(GL_FRAGMENT_SHADER, fragmentCode); | |
programObject = Shader::BuildProgram({ vertexShaderObject, fragmentShaderObject }); | |
offsetUniformLocation = glGetUniformLocation(programObject, "offset"); | |
zNearUniformLocation = glGetUniformLocation(programObject, "zNear"); | |
zFarUniformLocation = glGetUniformLocation(programObject, "zFar"); | |
frustumScaleUniformLocation = glGetUniformLocation(programObject, "frustumScale"); | |
glUseProgram(programObject); | |
glUniform1f(zNearUniformLocation, 1.0f); | |
glUniform1f(zFarUniformLocation, 3.0f); | |
glUniform1f(frustumScaleUniformLocation, 1.0f); | |
glUseProgram(0); | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
glFrontFace(GL_CW); | |
} | |
static void Render() { | |
glUseProgram(programObject); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glUniform2f(offsetUniformLocation, -0.3f, 0.3f); | |
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (0)); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (sizeof (Cube) / 2)); | |
glDrawArrays(GL_TRIANGLES, 0, 36); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
glUseProgram(0); | |
glutSwapBuffers(); | |
glutPostRedisplay(); | |
} | |
static void Clear() { | |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
} | |
static void Update() { | |
} | |
void Run() { | |
if (!ifInit) { | |
InitObject(); | |
ifInit = true; | |
} | |
Render(); | |
Update(); | |
Clear(); | |
} | |
void Resize(int w, int h) { | |
glViewport(0, 0, w, h); | |
} |
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 MAIN_HEADER | |
#define MAIN_HEADER | |
#include "GL/glew.h" | |
#include "GL/freeglut.h" | |
#include "Shader.hpp" | |
static void InitObject(); | |
static void Render(); | |
static void Clear(); | |
static void Update(); | |
void Run(); | |
void Resize(int, int); | |
#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 position; | |
layout(location = 1) in vec4 color; | |
smooth out vec4 theColor; | |
uniform float frustumScale; /// how big is the field of view | |
uniform float zNear; /// how near is the view from the eye | |
uniform float zFar; /// how far that this camera can view | |
uniform vec2 offset; | |
void main() | |
{ | |
vec4 CameraSpacePosition = position + vec4(offset.x, offset.y, 0.0f, 0.0f); /// adds an offset to each vertex of the triangles and stores it into the camera space | |
vec4 CameraClipSpacePosition; /// this stores the calculated camera clip space | |
CameraClipSpacePosition.x = CameraSpacePosition.x * frustumScale; /// scales the plane of projection in x and y | |
CameraClipSpacePosition.y = CameraSpacePosition.y * frustumScale; | |
CameraClipSpacePosition.z = CameraSpacePosition.z * (zNear + zFar) / (zNear - zFar); /// gets the depth or the z | |
CameraClipSpacePosition.z += 2 * zNear * zFar / (zNear - zFar); | |
CameraClipSpacePosition.w = -CameraSpacePosition.z; /// needs the negate the z component of Camera Position because the -z value of the camera space must be flipped into +z in order to maintain the image | |
gl_Position = CameraClipSpacePosition; /// transform into ndc (by perspective division) | |
theColor = color; /// sends the color of each vertex in the next shader | |
} |
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_HEADER | |
#define SHADER_HEADER | |
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#include <initializer_list> | |
#include <string> | |
#include <memory> | |
#include <iostream> | |
#include <fstream> | |
#include <algorithm> | |
class Shader { | |
public: | |
static GLuint BuildProgram(std::initializer_list <GLuint> shaderObjects) { | |
GLuint programObject = glCreateProgram(); | |
GLint linkStatus; | |
for (std::initializer_list <GLuint>::iterator Shaderit = shaderObjects.begin(); | |
Shaderit != shaderObjects.end(); ++Shaderit) | |
glAttachShader(programObject, *Shaderit); | |
glLinkProgram(programObject); | |
glGetProgramiv(programObject, GL_LINK_STATUS, &linkStatus); | |
if (!linkStatus) { | |
GLint logLength; | |
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &logLength); | |
std::unique_ptr <GLchar> errLog(new GLchar[logLength + 1]); | |
glGetProgramInfoLog(programObject, logLength + 1, nullptr, errLog.get()); | |
std::cerr << "program linking error: " << std::endl | |
<< errLog.get() << std::endl; | |
} | |
for (std::initializer_list <GLuint>::iterator Shaderit2 = shaderObjects.begin(); | |
Shaderit2 != shaderObjects.end(); ++Shaderit2) | |
glDetachShader(programObject, *Shaderit2); | |
return programObject; | |
} | |
static std::string LoadShaderFile(const std::string &fileName) { | |
std::ifstream readFile(fileName, std::ifstream::in, std::ifstream::trunc); | |
std::string srcCode, line; | |
if (!readFile) | |
std::cout << "cannot open shader file " << fileName << std::endl; | |
for (; std::getline(readFile, line);) | |
srcCode += line + "\n"; | |
if (srcCode[srcCode.length() - 1] != '\0') | |
srcCode.push_back('\0'); | |
std::cout << srcCode << std::endl; | |
return srcCode; | |
} | |
static GLuint CompileShader(const GLenum &shaderType, const char *shaderCode) { | |
GLuint shaderObject = glCreateShader(shaderType); | |
GLint compileStatus; | |
glShaderSource(shaderObject, 1, &shaderCode, nullptr); | |
glCompileShader(shaderObject); | |
glGetShaderiv(shaderObject, GL_COMPILE_STATUS, &compileStatus); | |
if (!compileStatus) { | |
GLint logLength; | |
glGetShaderiv(shaderObject, GL_INFO_LOG_LENGTH, &logLength); | |
std::unique_ptr <GLchar> errLog(new GLchar[logLength + 1]); | |
glGetShaderInfoLog(shaderObject, logLength + 1, nullptr, errLog.get()); | |
std::cerr << ((shaderType == GL_VERTEX_SHADER) ? "vertex" : | |
(shaderType == GL_FRAGMENT_SHADER) ? "fragment" : "none") << "shader error" << std::endl | |
<< errLog.get() << std::endl; | |
} | |
return shaderObject; | |
} | |
}; | |
#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 | |
smooth in vec4 theColor; | |
out vec4 fragmentOutput; | |
void main () | |
{ | |
fragmentOutput = theColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment