Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created March 2, 2016 17:46
Show Gist options
  • Save krysseltillada/62c2e96154386a4ed39d to your computer and use it in GitHub Desktop.
Save krysseltillada/62c2e96154386a4ed39d to your computer and use it in GitHub Desktop.
exercise 3 (moving vertex positions)
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include "main.hpp"
const char *windowTitle = "exercise 3";
const int windowWidth = 800;
const int windowHeight = 600;
void registerCallbacks()
{
glutDisplayFunc(&Render);
glutReshapeFunc(&Resize);
}
void initCoreFunc(int *const argn, char **const args) {
glutInit(argn, args);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitContextVersion(3, 3);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(300, 300);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow(windowTitle);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "error initializing context: " << std::endl;
}
InitObjects();
registerCallbacks();
glutMainLoop();
}
int main(int argn, char **args)
{
initCoreFunc(&argn, args);
return EXIT_SUCCESS;
}
#version 330
smooth in vec4 outputVertexColor;
out vec4 fragmentColor;
void main ()
{
fragmentColor = outputVertexColor;
}
#include "main.hpp"
std::string loadShaderFile(const std::string &fileName) {
std::fstream readFile(fileName, std::fstream::in);
std::string line, sourceCode;
if (!readFile)
std::cerr << "error loading shader file: " << fileName << std::endl;
while (std::getline(readFile, line))
sourceCode += line + "\n";
if (sourceCode[sourceCode.length() - 1] != '\0')
sourceCode += '\0';
return sourceCode;
}
GLuint BuildShaderFile(std::initializer_list <std::string> fileNames) {
std::vector < std::pair <std::string, GLenum> > shaderCodes;
std::vector <GLuint> shaderObjectList;
GLuint programShader;
for (int i = 0; i != fileNames.size(); ++i) {
std::string fileName = *(fileNames.begin() + i);
int countedChar = 0;
for (int b = 0; b != fileName.length(); ++b) {
if (fileName[b] == '.') {
if (fileName.substr(countedChar + 1) == "vert")
shaderCodes.push_back({ loadShaderFile(fileName), GL_VERTEX_SHADER });
else if (fileName.substr(countedChar + 1) == "frag")
shaderCodes.push_back({ loadShaderFile(fileName), GL_FRAGMENT_SHADER });
break;
}
else { ++countedChar; }
}
}
for (std::pair <std::string, GLenum> p : shaderCodes) {
std::cout << ((p.second == GL_VERTEX_SHADER) ? "vertex shader:" :
(p.second == GL_FRAGMENT_SHADER) ? "fragment shader:" : "null") << std::endl
<< p.first << std::endl;
shaderObjectList.push_back(CompileShader(p.second, p.first.c_str()));
}
programShader = BuildProgram(shaderObjectList);
for (int a = 0; a != shaderObjectList.size(); ++a)
glDeleteShader(shaderObjectList[a]);
return programShader;
}
GLuint CompileShader(GLenum shaderType, const char *shaderCode) {
GLuint shaderObject;
GLint compileStatus;
shaderObject = glCreateShader(shaderType);
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> errorInfo(new GLchar[logLength + 1]);
glGetShaderInfoLog(shaderObject, logLength + 1, nullptr, errorInfo.get());
std::cerr << errorInfo.get() << std::endl;
}
return shaderObject;
}
GLuint BuildProgram(std::vector <GLuint> shaderObjectList) {
GLuint programObject = glCreateProgram();
GLint linkStatus;
for (int i = 0; i != shaderObjectList.size(); ++i)
glAttachShader(programObject, shaderObjectList[i]);
glLinkProgram(programObject);
glGetProgramiv(programObject, GL_LINK_STATUS, &linkStatus);
if (!linkStatus) {
GLint logLength;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &logLength);
std::unique_ptr <GLchar> errorLog(new GLchar[logLength + 1]);
glGetProgramInfoLog(programObject, logLength + 1, nullptr, errorLog.get());
std::cerr << errorLog.get() << std::endl;
}
for (int i = 0; i != shaderObjectList.size(); ++i)
glDetachShader(programObject, shaderObjectList[i]);
return programObject;
}
const GLfloat vertexArray[] = {
0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.0f, 1.0f,
0.0f, 0.1f, 0.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 vertexBufferObject;
GLuint shaderProgram;
void circularOffset(float &xOffset, float &yOffset) {
const float circularDuration = 5.0f;
const float angleDistance = 3.14159f * 2.0f / circularDuration;
float deltaTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
float range = fmodf(deltaTime, circularDuration);
yOffset = sinf(range * angleDistance) * 0.5f;
xOffset = cosf(range * angleDistance) * 0.5f;
}
void UpdateVertexPosition(float xPosition, float yPosition) {
std::vector <GLfloat> newVertexArray ( sizeof (vertexArray) / sizeof(vertexArray[0]));
memcpy(&newVertexArray[0], vertexArray, sizeof(vertexArray));
for (int c = 0; c != newVertexArray.size() / 2; c += 4) {
newVertexArray[c] += xPosition;
newVertexArray[c + 1] += yPosition;
}
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertexArray), &newVertexArray[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void InitObjects() {
shaderProgram = BuildShaderFile({ "vertexShader.vert", "fragmentShader.frag"});
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof (vertexArray), vertexArray, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Render() {
float xPos = 0.0f, yPos = 0.0f;
circularOffset(xPos, yPos);
UpdateVertexPosition(xPos, yPos);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
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);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
void Resize(int w, int h) {
glViewport(0, 0, static_cast <GLint> (w), static_cast <GLint>(h) );
}
#ifndef MAIN_HPP
#define MAIN_HPP
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <initializer_list>
#include <utility>
#include <memory>
std::string loadShaderFile(const std::string &);
GLuint BuildShaderFile(std::initializer_list <std::string>);
GLuint CompileShader(GLenum, const char *);
GLuint BuildProgram(std::vector <GLuint>);
void circularOffset(float &, float &);
void InitObjects();
void Resize (int, int);
void Render ();
#endif
#version 330
layout (location = 0) in vec4 inputVertexPosition;
layout (location = 1) in vec4 inputVertexColor;
smooth out vec4 outputVertexColor;
void main ()
{
gl_Position = inputVertexPosition;
outputVertexColor = inputVertexColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment