Created
March 31, 2016 14:22
-
-
Save krysseltillada/abf978a9ddc1c771d0c0ab1c4797da8d to your computer and use it in GitHub Desktop.
ex 11 indexed drawing
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 <string> | |
#include <stdexcept> | |
#include <iostream> | |
#include <cstdlib> | |
#include "main.hpp" | |
class Color { | |
public: | |
Color() = default; | |
Color(const float &r, const float &g, const float &b, const float &a) try : | |
red(r), green(g), blue(b), alpha(a) { } | |
catch (std::string err) { | |
} | |
static Color normalizeRGBA(const int &r, const int &g, const int &b, const int &a) { | |
return Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); | |
} | |
float red, green, blue, alpha; | |
}; | |
struct Window { | |
static const int width = 800; | |
static const int height = 600; | |
static const int yPos = 200; | |
static const int xPos = 300; | |
static char * title; | |
}; | |
char * Window::title = "exercise 10"; | |
void InitWindow(int *const cp_argn, char **const cp_argc) { | |
glutInit(cp_argn, cp_argc); | |
glutInitContextProfile(GLUT_CORE_PROFILE); | |
glutInitContextVersion(3, 3); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); | |
glutInitWindowSize(Window::width, Window::height); | |
glutInitWindowPosition(Window::xPos, Window::yPos); | |
glutCreateWindow(Window::title); | |
glewExperimental = GL_TRUE; | |
if (glewInit()) { | |
std::cerr << "error cannot initialized the context check your video card drivers" << std::endl; | |
throw std::runtime_error("context::error"); | |
} | |
glutDisplayFunc(&Run); | |
glutKeyboardFunc(&KeyEvent); | |
glutMainLoop(); | |
} | |
int main(int argn, char **argc) | |
{ | |
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 | |
smooth in vec4 vertexOutputColor; | |
out vec4 fragmentColor; | |
void main () | |
{ | |
fragmentColor = vertexOutputColor; | |
} |
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 = GL_FALSE; | |
const GLfloat triangle[] = { | |
-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, 1.0f, 0.0f, 1.0f, | |
0.0f, 1.0f, 0.0f, 1.0f, | |
0.0f, 0.0f, 1.0f, 1.0f | |
}; | |
const GLushort elementArray[] = { | |
0, 1, 2 | |
}; | |
GLuint bufferObject; | |
GLuint elementBufferObject; | |
GLuint programObject; | |
namespace { | |
void InitializeObjects() { | |
std::string vertexShaderCode = Shader::ShaderUtility::loadShaderFile("vertexShader.vert"); | |
std::string fragmentShaderCode = Shader::ShaderUtility::loadShaderFile("fragmentShader.frag"); | |
GLuint vertexShaderObject = Shader::ShaderUtility::compileShader(GL_VERTEX_SHADER, vertexShaderCode.c_str()); | |
GLuint fragmentShaderObject = Shader::ShaderUtility::compileShader(GL_FRAGMENT_SHADER, fragmentShaderCode.c_str()); | |
programObject = Shader::ShaderUtility::buildProgram({ vertexShaderObject, fragmentShaderObject }); | |
glGenBuffers(1, &bufferObject); | |
glBindBuffer(GL_ARRAY_BUFFER, bufferObject); | |
glBufferData(GL_ARRAY_BUFFER, sizeof (triangle), triangle, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glGenBuffers(1, &elementBufferObject); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangle), elementArray, GL_STATIC_DRAW); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
} | |
void Update() { | |
} | |
void Display() { | |
glUseProgram(programObject); | |
glBindBuffer(GL_ARRAY_BUFFER, bufferObject); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void *)0); | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *)(sizeof (triangle) / 2)); | |
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, (void *) 0); | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
glUseProgram(0); | |
glutSwapBuffers(); | |
glutPostRedisplay(); | |
} | |
void Clear() { | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
} | |
} | |
void Run() { | |
if (!ifInit) { | |
InitializeObjects(); | |
ifInit = GL_TRUE; | |
} | |
Update(); | |
Display(); | |
Clear(); | |
} | |
void KeyEvent(unsigned char key, int x, int y) { | |
} | |
void WindowEvent(int width, int height) { | |
glViewport(0, 0, width, height); | |
} |
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_LOGIC_HPP | |
#define MAIN_LOGIC_HPP | |
#include <GL/glew.h> | |
#include <GL/freeglut.h> | |
#include "shader.hpp" | |
namespace { | |
void InitializeObjects(); | |
void Update(); | |
void Display(); | |
void Clear(); | |
} | |
void Run(); | |
void KeyEvent(unsigned char, int, int); | |
void WindowEvent(int, int); | |
#endif MAIN_LOGIC_HPP |
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 <memory> | |
#include <initializer_list> | |
#include <stdexcept> | |
#include <string> | |
#include <iostream> | |
#include <fstream> | |
namespace Shader { | |
class ShaderUtility { | |
public: | |
static std::string loadShaderFile(const std::string &fileName) { | |
std::ifstream readFile; | |
std::string line, sourceCode; | |
readFile.open(fileName, std::ifstream::in, std::ifstream::trunc); | |
if (readFile.is_open()) { | |
for (; std::getline(readFile, line) ;) | |
sourceCode += line + '\n'; | |
} | |
else {} | |
if (sourceCode[sourceCode.length() - 1] != '\0') | |
sourceCode.push_back('\0'); | |
return sourceCode; | |
} | |
static GLuint compileShader(const GLuint &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 shaderTypeStr; | |
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: | |
shaderTypeStr = "vertex"; | |
break; | |
case GL_FRAGMENT_SHADER: | |
shaderTypeStr = "fragment"; | |
break; | |
default: | |
break; | |
} | |
std::cerr << "error " << shaderTypeStr << " shader " << std::endl | |
<< errLog.get() << std::endl; | |
} | |
return shaderObject; | |
} | |
static GLuint buildProgram(std::initializer_list <GLuint> shaderObjectList) { | |
GLuint programObject = glCreateProgram(); | |
GLint linkStatus; | |
for (std::initializer_list <GLuint>::iterator 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::cerr << "LINKING ERROR" << std::endl | |
<< errLog.get() << std::endl; | |
} | |
for (std::initializer_list <GLuint>::iterator it2 = shaderObjectList.begin(); | |
it2 != shaderObjectList.end(); ++it2) { | |
glDetachShader(programObject, *it2); | |
glDeleteShader(*it2); | |
} | |
return programObject; | |
} | |
}; | |
} | |
#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 vertexPosition; | |
layout (location = 1) in vec4 vertexColor; | |
smooth out vec4 vertexOutputColor; | |
void main () | |
{ | |
gl_Position = vertexPosition; | |
vertexOutputColor = vertexColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment