Skip to content

Instantly share code, notes, and snippets.

@athanase
Last active August 29, 2015 14:02
Show Gist options
  • Save athanase/64633a8d2ec61079b4b0 to your computer and use it in GitHub Desktop.
Save athanase/64633a8d2ec61079b4b0 to your computer and use it in GitHub Desktop.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
//
static void error_callback( int error, const char* description )
{
fputs( description, stderr );
}
//-----------------------------------------------------------------------------
//
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 );
}
}
glfwWindowHint( GLFW_SAMPLES, 4 ); // 4x antialiasing
//We don't want the old OpenGL
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwSetErrorCallback( error_callback );
if( !glfwInit() )
{
exit( EXIT_FAILURE );
}
GLFWwindow* window = glfwCreateWindow( 640, 480, "Simple example", NULL, NULL );
if( !window )
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent( window );
glfwSetKeyCallback( window, key_callback );
while( !glfwWindowShouldClose(window) )
{
float ratio;
int width, height;
glfwGetFramebufferSize( window, &width, &height );
ratio = width / (float) height;
glViewport( 0, 0, width, height );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -ratio, ratio, -1.f, 1.f, 1.f, -1.f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glRotatef( (float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f );
//glBegin( GL_TRIANGLES );
//glColor3f( 1.f, 0.f, 0.f );
//glVertex3f( -0.6f, -0.4f, 0.f );
//glColor3f( 0.f, 1.f, 0.f );
//glVertex3f( 0.6f, -0.4f, 0.f );
//glColor3f( 0.f, 0.f, 1.f );
//glVertex3f( 0.f, 0.6f, 0.f );
//glEnd();
// this code will draw a point located at [100, 100, -25]
glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd( );
// next code will draw a line at starting and ending coordinates specified by glVertex3f
glBegin(GL_LINES);
glVertex3f(100.0f, 100.0f, 0.0f); // origin of the line
glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the line
glEnd( );
// the following code draws a triangle
glBegin(GL_TRIANGLES);
glVertex3f(100.0f, 100.0f, 0.0f);
glVertex3f(150.0f, 100.0f, 0.0f);
glVertex3f(125.0f, 50.0f, 0.0f);
glEnd( );
// this code will draw two lines "at a time" to save
// the time it takes to call glBegin and glEnd.
glBegin(GL_LINES);
glVertex3f(100.0f, 100.0f, 0.0f); // origin of the FIRST line
glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the FIRST line
glVertex3f(120.0f, 170.0f, 10.0f); // origin of the SECOND line
glVertex3f(240.0f, 120.0f, 5.0f); // ending point of the SECOND line
glEnd( );
glfwSwapBuffers( window );
glfwPollEvents();
}
glfwDestroyWindow( window );
glfwTerminate();
exit( EXIT_SUCCESS );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment