Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created February 23, 2016 16:59
Show Gist options
  • Save krysseltillada/74f1f7f708141c19510e to your computer and use it in GitHub Desktop.
Save krysseltillada/74f1f7f708141c19510e to your computer and use it in GitHub Desktop.
open gl (hello triangle)
#version 330
out vec4 outputColor;
void main ()
{
outputColor = vec4(1.0f, 0.50f, 0.50f, 0.0f);
}
#define GLEW_STATIC
#include <GL/glew.h> /// includes open gl functions
#include <GL/freeglut.h> // includes freeglut functions
#include <stdio.h> /// some printf's
#include <fstream> /// for loading fragment file and vertex file
#include <string> /// strings
#include <memory> /// smart pointers
#include <initializer_list> /// for braced arguments
#include <iostream> /// some std::cout, std::cerr
std::string loadShaderFile(const std::string &fileName) { /// loads the shader file
std::ifstream readFile(fileName);
std::string source, line;
while (std::getline(readFile, line))
source += line + "\n";
source += '\0';
return source;
}
const GLfloat vertexPositions[] = { /// vertex data's each of set of 3 lines represents a single triangle in the screen
//** triangle data 1 **// /// each of 4 values represents a single vertex or a point in a 4d space
0.0f, 0.80f, 0.0f, 1.0f, /// including the 4th value which specifies the extents of each vertex in clip space
0.80f, -0.80f, 0.0f, 1.0f,
-0.80f, -0.80f, 0.0f, 1.0f,
//** triangle data 2 **//
-0.40f, 0.70f, 0.0f, 1.0f,
0.20f, 0.20f, 0.0f, 1.0f,
0.50f, -0.40f, 0.0f, 1.0f
//** triangle data 3 **//
-0.50f, 0.20f, 0.0f, 1.0f,
-0.50f, -0.50f, 0.0f, 1.0f,
0.50f, 0.80f, 0.0f, 1.0f
};
GLuint positionBufferObject; /// for storing a handle for buffer object (handles the buffer object created and hidden by the open gl)
GLuint vertexShaderObj; /// a vertex shader object that is use to store the vertex shader code for to be compiled and link with the programObj
GLuint fragmentShaderObj; /// a fragment shader object that is use to store the vertex shader code for to be compiled and link with the programObj
GLuint programObj; /// a program Object that contains all the shader objects that is compiled and linked in and form into a one shader program
void renderScene() { /// rendering scene
glClearColor(0.90f, 0.25f, 0.90f, 0.0f); /// sets the reference color to r g b a
glClear(GL_COLOR_BUFFER_BIT); /// clears the buffer color in the screen (clears the color in the screen)
glUseProgram(programObj); /// uses the shader program that is linked all the shader objects
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); /// this binds the positionBufferObject handle that handles the buffer object
/// that is created and hidden by the open gl to a location in a context by the target GL_ARRAY_BUFFER
glEnableVertexAttribArray(0); /// this enables the attribute index in that vertex shader program
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); /// this refers to that attribute index and specifies a format of data
/// of how to send the data from the buffer object to that attribute index
glDrawArrays(GL_TRIANGLES, 0, 9); /// this draws a list of vertices in starting from zero index to 9
/// GL_TRIANGLES tells open gl that for each 3 vertex there is a one triangle that is rendered in the screen
glutSwapBuffers(); /// this swaps the buffer
}
GLuint BuildProgram(std::initializer_list <GLuint> shaderObjectList) { /// this links the program
GLuint ShaderProgramObject;
GLint linkingStatus;
ShaderProgramObject = glCreateProgram(); /// creates an empty program object
for (auto shaderObject : shaderObjectList)
glAttachShader(ShaderProgramObject, shaderObject); /// attaches the shader object in the program object
glLinkProgram(ShaderProgramObject); /// this links all the attached shader object in the program object
glGetProgramiv(ShaderProgramObject, GL_LINK_STATUS, &linkingStatus); /// this gets a status if the linking was successful
if (linkingStatus == GL_FALSE) { /// if it has errors then print some error log
GLint errorLength;
glGetProgramiv(ShaderProgramObject, GL_INFO_LOG_LENGTH, &errorLength);
std::shared_ptr <GLchar> errorLog = std::make_shared <GLchar>(errorLength + 1);
glGetProgramInfoLog(ShaderProgramObject, errorLength + 1, NULL, errorLog.get());
std::cout << "LINKING ERROR:: BuildProgram () --> " << std::endl
<< errorLog.get() << std::endl;
}
for (int a = 0; a != shaderObjectList.size(); ++a) /// this detaches the previous attached shader object in the program object
glDetachShader(ShaderProgramObject, *(shaderObjectList.begin() + a) );
for (int j = 0; j != shaderObjectList.size(); ++j) /// this deletes a list of the shader objects
glDeleteShader( *(shaderObjectList.begin () + j ) ) ;
return ShaderProgramObject;
}
GLuint CompileShader(GLenum ShaderType, const char *sourceCode) { /// compiles the shader code
GLuint shaderObject;
GLint compileStatus;
shaderObject = glCreateShader(ShaderType); /// creates a shader object
glShaderSource(shaderObject, 1, &sourceCode, NULL); /// puts the shader code in the shader object
glCompileShader(shaderObject); /// compiles the set shader code in the shader object
glGetShaderiv(shaderObject, GL_COMPILE_STATUS, &compileStatus); /// checks if the compilation was successful
if (compileStatus == GL_FALSE) { /// if it has errors then display some error log
GLint errorLogLength;
glGetShaderiv(shaderObject, GL_INFO_LOG_LENGTH, &errorLogLength);
std::shared_ptr <GLchar> errorInfo = std::make_shared <GLchar>(errorLogLength + 1);
glGetShaderInfoLog(shaderObject, errorLogLength + 1, NULL, errorInfo.get());
std::cout << errorInfo.get() << std::endl;
}
return shaderObject;
}
void resize(int w, int h) { /// this changes the renderable view everytime the user resizes the window
glViewport(0, 0, static_cast <GLsizei> (w), static_cast <GLsizei> (h));
}
int main(int argc, char **argv)
{
/// sets all window properties
glutInit(&argc, argv);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello Triangle");
/// initiates open gl
glewInit();
std::string str1, str2;
const char *src = nullptr;
const char *src2 = nullptr;
str1 = loadShaderFile("vertexCode.vert");
str2 = loadShaderFile("fragmentCode.frag");
src = str1.c_str();
src2 = str2.c_str();
vertexShaderObj = CompileShader(GL_VERTEX_SHADER, src);
fragmentShaderObj = CompileShader(GL_FRAGMENT_SHADER, src2);
programObj = BuildProgram({ vertexShaderObj, fragmentShaderObj });
glGenBuffers(1, &positionBufferObject); /// creates a buffer object and stores the handle in the positionBufferObject
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); /// binds the positionBufferObject in some location of the context by GL_ARRAY_BUFFER target
glBufferData(GL_ARRAY_BUFFER, sizeof (vertexPositions), vertexPositions, GL_STATIC_DRAW); /// sends some vertex data in the buffer object
glBindBuffer(GL_ARRAY_BUFFER, 0); /// unbinds the currently buffer object that was bound in GL_ARRAY_BUFFER target
glutDisplayFunc(renderScene);
glutReshapeFunc(resize);
glutMainLoop(); /// creates a main loop
return 0;
}
#version 330
layout (location = 0) in vec4 inputPosition;
void main ()
{
gl_Position = inputPosition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment