Skip to content

Instantly share code, notes, and snippets.

@gamingrobot
Last active December 25, 2015 02:49
Show Gist options
  • Save gamingrobot/6905769 to your computer and use it in GitHub Desktop.
Save gamingrobot/6905769 to your computer and use it in GitHub Desktop.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
// Shader sources
const char* vertexSource =
"#version 150\n"
"in vec2 position;"
"in vec3 color;"
"in vec2 texcoord;"
"out vec3 Color;"
"out vec2 Texcoord;"
"void main() {"
" Color = color;"
" Texcoord = texcoord;"
" gl_Position = vec4( position, 0.0, 1.0 );"
"}";
const char* fragmentSource =
"#version 150\n"
"in vec3 Color;"
"in vec2 Texcoord;"
"out vec4 outColor;"
"uniform sampler2D texKitten;"
"uniform sampler2D texPuppy;"
"void main() {"
" outColor = mix( texture( texKitten, Texcoord ), texture( texPuppy, Texcoord ), 0.5 );"
"}";
static void error_callback( int error, const char* description ) {
printf( "%s", description );
}
static void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods ) {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
glfwSetWindowShouldClose( window, GL_TRUE );
}
int main(int argc, char **argv) {
// Inits
glfwSetErrorCallback( error_callback );
if ( !glfwInit() ) {
printf( "Could not init glfw" );
return 1;
}
// Make 3.2 OpenGL version
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
GLFWwindow* window;
window = glfwCreateWindow( 800, 600, "Pretty OpenGL", NULL, NULL );
if ( !window ) {
printf( "Could not create window" );
glfwTerminate();
return 1;
}
glfwMakeContextCurrent( window );
glfwSetKeyCallback( window, key_callback );
glewExperimental = GL_TRUE;
glewInit();
// OpenGL Stuff
// Create Vertex Array Object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers( 1, &vbo );
float vertices[] = {
// Position Color Texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
// Create an element array
GLuint ebo;
glGenBuffers( 1, &ebo );
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ebo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( elements ), elements, GL_STATIC_DRAW );
// Create and compile the vertex shader
GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexSource, NULL );
glCompileShader( vertexShader );
// Check shader compiled
GLint status;
glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &status );
if( status == GL_FALSE ) {
char buffer[512];
glGetShaderInfoLog( vertexShader, 512, NULL, buffer );
printf( "%s", buffer );
}
// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentSource, NULL );
glCompileShader( fragmentShader );
// Check shader compiled
glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &status );
if( status == GL_FALSE ) {
char buffer[512];
glGetShaderInfoLog( fragmentShader, 512, NULL, buffer );
printf("%s", buffer);
}
// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glBindFragDataLocation( shaderProgram, 0, "outColor" );
glLinkProgram( shaderProgram );
glUseProgram( shaderProgram );
// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation( shaderProgram, "position" );
glEnableVertexAttribArray( posAttrib );
glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof( float ), 0 );
GLint colAttrib = glGetAttribLocation( shaderProgram, "color" );
glEnableVertexAttribArray( colAttrib );
glVertexAttribPointer( colAttrib, 3, GL_FLOAT, GL_FALSE, 7*sizeof( float ), ( void* )( 2*sizeof( float ) ) );
GLint texAttrib = glGetAttribLocation( shaderProgram, "texcoord" );
glEnableVertexAttribArray( texAttrib );
glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof( float ), ( void* )( 5*sizeof( float ) ) );
// Main Loop
while ( !glfwWindowShouldClose( window ) ) {
// Clear the screen to black
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0 );
// Swapbuffers
glfwSwapBuffers( window );
glfwPollEvents();
}
//OpenGL Cleanup
glDeleteProgram( shaderProgram );
glDeleteShader( fragmentShader );
glDeleteShader( vertexShader );
glDeleteBuffers( 1, &ebo );
glDeleteBuffers( 1, &vbo );
glDeleteVertexArrays( 1, &vao );
// Cleanup
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment