Created
February 25, 2016 18:44
-
-
Save krysseltillada/eb94a1faea7821d433ea to your computer and use it in GitHub Desktop.
(ex2) ch 2 playing with colors
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 /// needs to define because of glew32s.lib | |
#include <GL/glew.h> /// includes open gl functionality | |
#include <GL/freeglut.h> /// includes window functions | |
#include <cstdlib> /// EXIT_SUCESS, exit (); | |
#include <iostream> /// std::cerr | |
#include "main.hpp" | |
/*acts like the core functionality of the window*/ | |
const int WINDOW_POS_X = 200; | |
const int WINDOW_POS_Y = 200; | |
const int WINDOW_WIDTH = 800; | |
const int WINDOW_HEIGHT = 600; | |
const char *WindowTitle = "playing with colors"; | |
void registerCallbacks() { /// sets some function callbacks | |
glutDisplayFunc(&run); | |
} | |
void initWindow (int *const argv, char **const argc) { /// sets the window properties | |
glutInit(argv, argc); | |
glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y); | |
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); | |
glutInitContextVersion(3, 3); | |
glutInitContextProfile(GLUT_CORE_PROFILE); | |
glutCreateWindow(WindowTitle); | |
glewExperimental = GL_TRUE; /// forces open gl to use CORE_PROFILE | |
if (glewInit() != GLEW_OK) { | |
std::cerr << "cannot initialized open gl context pls update your graphics card" << std::endl; | |
exit(EXIT_FAILURE); | |
} | |
registerCallbacks(); | |
glutMainLoop(); | |
} | |
int main(int argv, char **argc) /// starts the main | |
{ | |
initWindow(&argv, 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 outputVertexColor; | |
out vec4 fragmentColorOutput; | |
void main () { | |
fragmentColorOutput = outputVertexColor; | |
} |
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" | |
/* acts like a main code for the exercise */ | |
bool ifStart = false; /// checks if it starts | |
static GLuint loadShaderFile(const std::string &fileName, GLenum shaderType) { /// loads the shader file | |
std::fstream readFile(fileName, std::ifstream::in, std::ifstream::trunc); | |
std::string buf, sourceCode; | |
const char *strSourceCode; | |
if (!readFile) { | |
std::cerr << "error loading shader file " << fileName << std::endl; | |
return -1; | |
} | |
while (std::getline(readFile, buf)) | |
sourceCode += buf + "\n"; | |
if (sourceCode[sourceCode.length() - 1] != '\0') | |
sourceCode.push_back('\0'); | |
strSourceCode = sourceCode.c_str(); | |
return CompileShader(strSourceCode, shaderType); | |
} | |
static GLuint CompileShader(const char *strSourceCode, GLenum shaderType) { /// compiles the shader | |
GLuint shaderObject; | |
GLint compileStatus; | |
shaderObject = glCreateShader(shaderType); | |
glShaderSource(shaderObject, 1, &strSourceCode, nullptr); | |
glCompileShader(shaderObject); | |
glGetShaderiv(shaderObject, GL_COMPILE_STATUS, &compileStatus); | |
if (!compileStatus) { | |
const std::string type = (shaderType == GL_VERTEX_SHADER) ? "vertex" : | |
(shaderType == GL_FRAGMENT_SHADER) ? "fragment" : "unknown"; | |
GLint logLength; | |
glGetShaderiv(shaderObject, GL_INFO_LOG_LENGTH, &logLength); | |
std::unique_ptr <GLchar[]> u_errorLog(new GLchar[logLength + 1]); | |
glGetShaderInfoLog(shaderObject, logLength + 1, nullptr, u_errorLog.get()); | |
std::cerr << type << " SHADER OBJECT FAILED TO COMPILE:: " << std::endl | |
<< u_errorLog.get() << std::endl; | |
} | |
return shaderObject; | |
} | |
static GLuint BuildShaderProgram(std::initializer_list <GLuint> shaderObjectList) { /// links the shader and builds the program | |
GLuint programObject; | |
GLint LinkStatus; | |
programObject = glCreateProgram(); | |
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[]> u_errorLog(new GLchar[logLength + 1]); | |
glGetProgramInfoLog(programObject, logLength + 1, nullptr, u_errorLog.get()); | |
std::cout << "LINKING ERROR " << std::endl | |
<< u_errorLog.get() << std::endl; | |
} | |
for (std::initializer_list <GLuint>::iterator it = shaderObjectList.begin(); | |
it != shaderObjectList.end(); ++it) { | |
glDetachShader(programObject, *it); | |
glDeleteShader(*it); | |
} | |
return programObject; | |
} | |
void run() { /// runs the window | |
if (!ifStart) { | |
start(); | |
ifStart = true; | |
} | |
display(); | |
} | |
const GLfloat vertexPositions[] { | |
/*vertex positions of a triangle*/ | |
-0.8f, 0.8f, 0.0f, 1.0f, | |
0.8f, 0.8f, 0.0f, 1.0f, | |
0.0f, -0.8f, 0.0f, 1.0f, | |
/*vertex output colors of each vertex of a triangle*/ | |
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; /// buffer object | |
GLuint shaderProgram; /// program object | |
GLuint fragmentShaderObject; /// fragment shader object | |
GLuint vertexShaderObject; /// vertex shader object | |
static void start() { /// sets the required objects to draw the triangle | |
vertexShaderObject = loadShaderFile("vertexShader.vert", GL_VERTEX_SHADER); | |
fragmentShaderObject = loadShaderFile("fragmentShader.frag", GL_FRAGMENT_SHADER); | |
shaderProgram = BuildShaderProgram({ vertexShaderObject, fragmentShaderObject }); | |
glGenBuffers(1, &vertexBufferObject); /// creates a buffer object and stores the handle to vertexBufferObject | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); /// binds that buffer object in the context | |
glBufferData(GL_ARRAY_BUFFER, sizeof (vertexPositions), vertexPositions, GL_STATIC_DRAW); /// sends the vertexPositions data into that buffer object | |
glBindBuffer(GL_ARRAY_BUFFER, 0); /// unbinds that buffer object in the context | |
} | |
static void display() { /// renders the triangle | |
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); /// sets a current clear color | |
glClear(GL_COLOR_BUFFER_BIT); /// clears the color and displays the current clearing color | |
glUseProgram(shaderProgram); /// uses the shader program | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); /// binds the bufferobject to the context | |
glEnableVertexAttribArray(0); /// enables that attribute index that was assigned in the vertex shader in that vertex attribute array | |
glEnableVertexAttribArray(1); /// ^^ same ^^ | |
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); /// refers to that attribute index and specifies what format its gonna send that currently buffer object into the vertex shader | |
/// in this case we prefer to send every 4 values that will be a single vertex (vec4) that is a floating point with no space | |
/// and no byte offset (starting element of the buffer object array) | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, reinterpret_cast <void *> (48)); /// the same the only difference is we set the byte offset to 48 which means | |
/// it starts at 48 byte in the buffer object array its like this (&bufferObject[12]) | |
glDrawArrays(GL_TRIANGLES, 0, 3); /// calls the vertex shader 3 times and every three vertices that glDrawArrays encounters | |
/// it will rasterize a triangle | |
glDisableVertexAttribArray(0); /// disables that attribute index for not to sending buffer object data into that attribute index that was assigned in that vertex shader | |
glDisableVertexAttribArray(1); /// the same | |
glUseProgram(0); /// this will not use the shader program | |
glutSwapBuffers(); /// swaps the buffer (double buffering) | |
glutPostRedisplay(); | |
} |
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
#pragma once /// the same as #ifndef #ifdef mainHPP #endif | |
#include <GL/glew.h> /// open gl functionality | |
#include <GL/freeglut.h> /// window functions | |
#include <initializer_list> /// braced arguments | |
#include <iostream> /// std::cout , std::cerr | |
#include <string> /// std::string | |
#include <fstream> /// file handling | |
#include <memory> /// smart pointers | |
void run (); /// runs the window | |
static void start (); /// sets all required object | |
static void display (); /// renders a triangle | |
GLuint loadShaderFile(const std::string &, GLenum); /// loads the shader file | |
GLuint CompileShader(const char *, GLenum); // compiles the loaded shader file | |
GLuint BuildShaderProgram(std::initializer_list <GLuint> ); /// links and builds the shader program |
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 outputVertexColor; | |
void main () { | |
gl_Position = vertexPosition; | |
outputVertexColor = vertexColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment