Created
May 7, 2016 08:11
-
-
Save krysseltillada/10fdf43522ffc838400b13f725e2b849 to your computer and use it in GitHub Desktop.
ex 13 (depth testing, depth buffer)
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 CAMERA_HEADER | |
#define CAMERA_HEADER | |
class Camera { | |
public: | |
Camera() : | |
x(0.0f), y(0.0f), z(0.0f), fov(0.0f), zNear(0.0f), zFar(0.0f) { } | |
float x, y, z; | |
float fov, zNear, zFar; | |
}; | |
#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 | |
flat in vec4 outputColor; | |
out vec4 fragmentOutput; | |
void main () | |
{ | |
fragmentOutput = outputColor; | |
} |
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 <stdexcept> | |
#include "Shader.hpp" | |
#include "Camera.hpp" | |
#define ifDEBUG | |
#ifdef ifDEBUG | |
#define LOG(X) {} \ | |
\ { std::clog << #X << std::endl; } | |
#endif | |
namespace myType { | |
typedef unsigned short Byte; | |
} | |
namespace WindowProperties { | |
float width = 800; | |
float height = 600; | |
const int windowInitialPosX = 200; | |
const int windowInitialPosY = 200; | |
std::string windowTitle = "exercise 13"; | |
myType::Byte displayMode = GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE; | |
} | |
namespace Main { | |
bool ifInit = false; | |
const int numberOfVertices = 36; | |
#define RIGHT_EXTENT 0.8f | |
#define LEFT_EXTENT -RIGHT_EXTENT | |
#define TOP_EXTENT 0.20f | |
#define MIDDLE_EXTENT 0.0f | |
#define BOTTOM_EXTENT -TOP_EXTENT | |
#define FRONT_EXTENT -1.25f | |
#define REAR_EXTENT -1.75f | |
#define GREEN_COLOR 0.75f, 0.75f, 1.0f, 1.0f | |
#define BLUE_COLOR 0.0f, 0.5f, 0.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[] = { | |
//Object 1 positions | |
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT, | |
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT, | |
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT, | |
// 0, 2, 1, | |
// 3, 2, 0, | |
//Object 2 positions | |
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT, | |
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT, | |
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT, | |
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT, | |
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT, | |
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT, | |
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT, | |
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT, | |
//Object 1 colors | |
GREEN_COLOR, | |
GREEN_COLOR, | |
GREEN_COLOR, | |
GREEN_COLOR, | |
BLUE_COLOR, | |
BLUE_COLOR, | |
BLUE_COLOR, | |
BLUE_COLOR, | |
RED_COLOR, | |
RED_COLOR, | |
RED_COLOR, | |
GREY_COLOR, | |
GREY_COLOR, | |
GREY_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
//Object 2 colors | |
RED_COLOR, | |
RED_COLOR, | |
RED_COLOR, | |
RED_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
BROWN_COLOR, | |
BLUE_COLOR, | |
BLUE_COLOR, | |
BLUE_COLOR, | |
GREEN_COLOR, | |
GREEN_COLOR, | |
GREEN_COLOR, | |
GREY_COLOR, | |
GREY_COLOR, | |
GREY_COLOR, | |
GREY_COLOR, | |
}; | |
const GLshort indexData[] = | |
{ | |
0, 2, 1, | |
3, 2, 0, | |
4, 5, 6, | |
6, 7, 4, | |
8, 9, 10, | |
11, 13, 12, | |
14, 16, 15, | |
17, 16, 14, | |
}; | |
GLfloat perspectiveMatrix[4 * 4]; | |
GLuint vertexBufferObject; | |
GLuint elementBufferObject; | |
GLuint vertexArrayObject, vertexArrayObject2; | |
GLuint programObject; | |
Camera camera1; | |
GLuint perspectiveMatrixUniformLocation; | |
GLuint offsetUniformLocation; | |
bool ifCull = false, ifDepth = false, ifAspect = false; | |
void Update() { | |
if (ifCull) { | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
glFrontFace(GL_CW); | |
} | |
else { | |
glDisable(GL_CULL_FACE); | |
} | |
if (ifDepth) { | |
glEnable(GL_DEPTH_TEST); | |
glDepthFunc(GL_LEQUAL); | |
glDepthMask(GL_TRUE); | |
glDepthRange(0.0f, 1.0f); | |
} | |
else { | |
glDisable(GL_DEPTH_TEST); | |
} | |
if (ifAspect) { | |
if (WindowProperties::height > 0) { | |
perspectiveMatrix[0] = camera1.fov / (float)(WindowProperties::width / WindowProperties::height); | |
perspectiveMatrix[5] = camera1.fov; | |
} | |
else { | |
WindowProperties::height = 1; | |
perspectiveMatrix[0] = camera1.fov / (float)(WindowProperties::width / WindowProperties::height); | |
perspectiveMatrix[5] = camera1.fov; | |
} | |
} | |
else { | |
perspectiveMatrix[0] = camera1.fov; | |
perspectiveMatrix[5] = camera1.fov; | |
} | |
glUseProgram(programObject); | |
glUniformMatrix4fv(perspectiveMatrixUniformLocation, 1, GL_FALSE, perspectiveMatrix); | |
glUseProgram(0); | |
} | |
void Init() { | |
GLuint vertexShaderObject; | |
GLuint fragmentShaderObject; | |
memset(perspectiveMatrix, 0, sizeof(float)* (4 * 4)); | |
if (ifCull) { | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
glFrontFace(GL_CW); | |
} | |
if (ifDepth) { | |
glEnable(GL_DEPTH_TEST); | |
glDepthMask(GL_TRUE); | |
glDepthFunc(GL_LEQUAL); | |
glDepthRange(0.0f, 1.0f); | |
} | |
camera1.z = -1.0f; | |
camera1.x = 0.0f; | |
camera1.y = 0.0f; | |
camera1.fov = 1.0f; | |
camera1.zNear = 1.0f; | |
camera1.zFar = 10.0f; | |
if (ifAspect) | |
perspectiveMatrix[0] = camera1.fov / (float)(WindowProperties::width / WindowProperties::height); | |
else | |
perspectiveMatrix[0] = camera1.fov; | |
perspectiveMatrix[5] = camera1.fov; | |
perspectiveMatrix[10] = (camera1.zNear + camera1.zFar) / (camera1.zNear - camera1.zFar); | |
perspectiveMatrix[14] = (2 * camera1.zNear * camera1.zFar) / (camera1.zNear - camera1.zFar); | |
perspectiveMatrix[11] = -1.0f; | |
std::string vertexCode = Shader::ShaderUtility::LoadShaderFile("perspective.vert"); | |
std::string fragmentCode = Shader::ShaderUtility::LoadShaderFile("fragmentColor.frag"); | |
vertexShaderObject = Shader::ShaderUtility::CompileShader(vertexCode.c_str(), GL_VERTEX_SHADER); | |
fragmentShaderObject = Shader::ShaderUtility::CompileShader(fragmentCode.c_str(), GL_FRAGMENT_SHADER); | |
programObject = Shader::ShaderUtility::BuildProgram({ vertexShaderObject, fragmentShaderObject }); | |
perspectiveMatrixUniformLocation = glGetUniformLocation(programObject, "perspectiveMatrix"); | |
offsetUniformLocation = glGetUniformLocation(programObject, "offset"); | |
glUseProgram(programObject); | |
glUniformMatrix4fv(perspectiveMatrixUniformLocation, 1, GL_FALSE, perspectiveMatrix); | |
glUseProgram(0); | |
glGenVertexArrays(1, &vertexArrayObject); | |
glGenVertexArrays(1, &vertexArrayObject2); | |
glGenBuffers(1, &vertexBufferObject); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, 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); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); | |
glBindVertexArray(vertexArrayObject); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (0)); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (sizeof(vertexData) / 2)); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject); | |
glBindVertexArray(0); | |
glBindVertexArray(vertexArrayObject2); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (sizeof(float)* 3 * (numberOfVertices / 2))); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (sizeof(float)* 3 * numberOfVertices + sizeof(float)* 4 * (numberOfVertices / 2))); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject); | |
glBindVertexArray(0); | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
} | |
void Clear() { | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
if (ifDepth) { | |
glClearDepth(1.0f); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
} | |
else { | |
glClear(GL_COLOR_BUFFER_BIT); | |
} | |
} | |
void Display() { | |
glUseProgram(programObject); | |
glBindVertexArray(vertexArrayObject); | |
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_SHORT, (void *)0); | |
glBindVertexArray(vertexArrayObject2); | |
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_SHORT, (void *)0); | |
glUseProgram(0); | |
glutSwapBuffers(); | |
glutPostRedisplay(); | |
} | |
void Run() { | |
if (!ifInit) { | |
std::cout << "press c to face culling " << std::endl | |
<< "press d to depth test" << std::endl | |
<< "press a to widescreen aspect ratio" << std::endl; | |
Init(); | |
ifInit = true; | |
} | |
Update(); | |
Display(); | |
Clear(); | |
} | |
void KeyEvents(unsigned char k, int x, int y) { | |
switch (k) { | |
case 'c': | |
if (!ifCull) { | |
std::cout << "-> face culling enabled" << std::endl; | |
ifCull = GL_TRUE; | |
} | |
else { | |
std::cout << "-> face culling disable" << std::endl; | |
ifCull = GL_FALSE; | |
} | |
break; | |
case 'd': | |
if (!ifDepth) { | |
std::cout << "-> depth test enabled" << std::endl; | |
ifDepth = GL_TRUE; | |
} | |
else { | |
std::cout << "-> depth test disabled" << std::endl; | |
ifDepth = GL_FALSE; | |
} | |
break; | |
case 'a': | |
if (!ifAspect) { | |
std::cout << "-> widescreen aspect ratio enabled" << std::endl; | |
ifAspect = GL_TRUE; | |
} | |
else { | |
std::cout << "-> widescreen aspect ratio disabled" << std::endl; | |
ifAspect = GL_FALSE; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
void resize(int w, int h) { | |
glViewport(0, 0, w, h); | |
WindowProperties::width = w; | |
WindowProperties::height = h; | |
} | |
} | |
namespace Window { | |
void registerCallback() { | |
glutDisplayFunc(&Main::Run); | |
glutKeyboardFunc(&Main::KeyEvents); | |
glutReshapeFunc(&Main::resize); | |
} | |
void initWindow(int *const c_argnp, char **const c_argcp) { | |
glutInit(c_argnp, c_argcp); | |
glutInitContextVersion(3, 3); | |
glutInitContextProfile(GLUT_CORE_PROFILE); | |
glutInitDisplayMode(WindowProperties::displayMode); | |
glutInitWindowSize(WindowProperties::width, WindowProperties::height); | |
glutInitWindowPosition(WindowProperties::windowInitialPosX, WindowProperties::windowInitialPosY); | |
if (glutCreateWindow(WindowProperties::windowTitle.c_str())) | |
std::cout << "window initialized--> exercise 13 " << std::endl; | |
glewExperimental = GL_TRUE; | |
try { | |
if (glewInit()) | |
throw std::string("cannot initialized the context"); | |
std::cout << "open gl version: " << glGetString(GL_VERSION) << std::endl; | |
} | |
catch (const std::string &err) { | |
std::cerr << err << " reinitializing... " << std::endl; | |
GLenum error; | |
if ((error = glewInit())) | |
throw std::runtime_error(reinterpret_cast <const char *>(glewGetErrorString(error)) ); | |
} | |
registerCallback(); | |
glutMainLoop(); | |
} | |
} | |
int main(int argn, char **argc) | |
{ | |
Window::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
#version 330 | |
layout (location = 0) in vec4 inputVertex; | |
layout (location = 1) in vec4 inputColor; | |
uniform mat4 perspectiveMatrix; | |
uniform vec3 offset; | |
flat out vec4 outputColor; | |
void main () | |
{ | |
vec4 CameraSpace = inputVertex; | |
CameraSpace.xyz += offset.xyz; | |
gl_Position = perspectiveMatrix * CameraSpace; | |
outputColor = inputColor; | |
} |
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; | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment