Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created March 29, 2016 17:53
Show Gist options
  • Save krysseltillada/56034a5db872d77cda0d56e8d862b9ef to your computer and use it in GitHub Desktop.
Save krysseltillada/56034a5db872d77cda0d56e8d862b9ef to your computer and use it in GitHub Desktop.
exercise 10 orthographic projection, array indexing, fragment positioning
#version 330
layout (location = 0) in vec4 vertexPosition;
layout (location = 1) in vec4 vertexColor;
smooth out vec4 outputVertexColor;
out float heightOutputPosition;
uniform float heightPosition;
void main ()
{
gl_Position = vertexPosition;
outputVertexColor = vertexColor;
heightOutputPosition = heightPosition;
}
#include "core_logic.hpp"
bool ifObjectIsInit = false;
const GLfloat vertexArrayData[] = {
-0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f,
0.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f
};
GLuint vbo_vertexPositions;
GLuint so_clipSpaceVS;
GLuint so_outputColorFS;
GLuint shaderProgram;
GLuint heightUniformLocation;
float windowHeight = 0.0f, windowWidth = 0.0f;
namespace {
void initObjects() {
const std::string vertexShaderStr(Shader::ShaderUtility::loadShaderFile("clipSpaceVS.vert"));
const std::string fragmentShaderStr(Shader::ShaderUtility::loadShaderFile("outputColorFS.frag"));
so_clipSpaceVS = Shader::ShaderUtility::compileShader(GL_VERTEX_SHADER, vertexShaderStr.c_str());
so_outputColorFS = Shader::ShaderUtility::compileShader(GL_FRAGMENT_SHADER, fragmentShaderStr.c_str());
shaderProgram = Shader::ShaderUtility::buildProgram({ so_clipSpaceVS, so_outputColorFS });
glGenBuffers(1, &vbo_vertexPositions);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertexPositions);
glBufferData(GL_ARRAY_BUFFER, sizeof (vertexArrayData), vertexArrayData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
heightUniformLocation = glGetUniformLocation(shaderProgram, "heightPosition");
glUseProgram(shaderProgram);
glUniform1f(heightUniformLocation, windowHeight);
glUseProgram(0);
}
void Update() {
glUseProgram(shaderProgram);
glUniform1f(heightUniformLocation, windowHeight);
glUseProgram(0);
}
void Display() {
glUseProgram(shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertexPositions);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (0));
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (48));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
void Clear() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
}
void setWindowHeight(const int &height) {
windowHeight = height;
}
void setWindowWidth(const int &width) {
windowWidth = width;
}
void Run() {
if (!ifObjectIsInit) {
initObjects();
ifObjectIsInit = true;
}
Update();
Display();
Clear();
}
void Resize(int width, int height) {
windowHeight = height;
windowWidth = width;
glViewport(0, 0, static_cast <GLint> (width), static_cast <GLint> (height));
}
void keyEvent(unsigned char k, int x, int y) {
if (k == 32)
glutLeaveMainLoop();
}
#ifndef CORE_LOGIC_HPP
#define CORE_LOGIC_HPP
#include "shader.hpp"
#include <GL/freeglut.h>
#include <string>
namespace {
void initObjects();
void Update();
void Display();
void Clear();
}
void setWindowHeight(const int &);
void setWindowWidth(const int &);
void Run ();
void Resize(int, int);
void keyEvent(unsigned char, int, int);
#endif
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
#include <cstdlib>
#include "core_logic.hpp"
namespace screen {
const int width = 1366;
const int height = 766;
}
namespace windowProperties {
const int windowWidth = 800;
const int windowHeight = 600;
const int windowPosX = screen::width / 2;
const int windowPosY = screen::height / 2;
const char *windowTitle = "exercise 9";
}
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(windowProperties::windowPosX, windowProperties::windowPosY);
glutCreateWindow(windowProperties::windowTitle);
glewExperimental = GL_TRUE;
if (glewInit())
std::cerr << "cannot initialized the context" << std::endl;
glutDisplayFunc(&Run);
glutKeyboardFunc(&keyEvent);
glutReshapeFunc(&Resize);
glutMainLoop();
}
int main(int argn, char **argc)
{
initWindow(&argn, argc);
return EXIT_SUCCESS;
}
#version 330
smooth in vec4 outputVertexColor;
in float heightOutputPosition;
out vec4 fragmentColor;
void main ()
{
float lerpValue = gl_FragCoord.y / heightOutputPosition;
fragmentColor = mix(outputVertexColor, vec4(0.0f, 0.0f, 0.0f, 1.0f), lerpValue);
}
#ifndef SHADER_HPP
#define SHADER_HPP
#define GLEW_STATIC
#include <GL/glew.h>
#include <fstream>
#include <string>
#include <memory>
#include <iostream>
#include <initializer_list>
namespace Shader {
class ShaderUtility {
public:
static std::string loadShaderFile(const std::string &fileName) {
std::ifstream readFile(fileName.c_str(), std::ifstream::in, std::ifstream::trunc);
std::string line, sourceCode;
if (readFile.is_open()) {
for (; std::getline(readFile, line);)
sourceCode += line + '\n';
if (sourceCode[sourceCode.size() - 1] != '\0')
sourceCode.push_back('\0');
}
else { std::cerr << "cannot open file " << fileName << std::endl; }
std::cout << sourceCode << std::endl;
return sourceCode;
}
static GLuint compileShader(const GLenum &shaderType, const char *sourceCode) {
GLuint shaderObject = glCreateShader(shaderType);
GLint compileStatus;
glShaderSource(shaderObject, 1, &sourceCode, nullptr);
glCompileShader(shaderObject);
glGetShaderiv(shaderObject, GL_COMPILE_STATUS, &compileStatus);
if (!compileStatus) {
GLint loglength;
std::string shaderErrorType;
glGetShaderiv(shaderObject, GL_INFO_LOG_LENGTH, &loglength);
std::unique_ptr <GLchar> errlog(new GLchar[loglength + 1]);
glGetShaderInfoLog(shaderObject, loglength + 1, nullptr, errlog.get());
switch (shaderType) {
case GL_VERTEX_SHADER:
shaderErrorType = "vertex";
break;
case GL_FRAGMENT_SHADER:
shaderErrorType = "fragment";
break;
default:
shaderErrorType = "unknown";
break;
}
std::cerr << shaderErrorType << " shader error " << std::endl
<< errlog.get() << std::endl;
}
return shaderObject;
}
static GLuint buildProgram(std::initializer_list <GLuint> shaderObjectList) {
GLuint programObject = glCreateProgram();
GLint linkStatus;
for (auto it = shaderObjectList.begin(); it != shaderObjectList.end(); ++it)
glAttachShader(programObject, *it);
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::cout << "linker error" << std::endl
<< errlog.get() << std::endl;
}
for (auto it = shaderObjectList.begin(); it != shaderObjectList.end(); ++it) {
glDetachShader(programObject, *it);
glDeleteShader(*it);
}
return programObject;
}
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment