Last active
July 25, 2016 06:51
-
-
Save mpkuse/d4fe65f744264fe41a81a569f01ed823 to your computer and use it in GitHub Desktop.
Setting up OpenGL with GLFW, GLEW
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
cmake_minimum_required (VERSION 2.8) | |
project (glfw_test) | |
find_package( glfw3 REQUIRED ) | |
find_package( GLEW REQUIRED ) | |
find_package( OpenGL REQUIRED ) | |
set(CMAKE_CXX_FLAGS "-std=c++11") | |
add_executable( glDemo gl_test.cpp ) | |
target_link_libraries( helloDemo glfw GLEW GL ) |
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 <iostream> | |
#include <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
#include <ctime> | |
#include <sys/time.h> | |
// Refer : http://www.learnopengl.com/ | |
using namespace std; | |
void key_callback( GLFWwindow* window, int key, int scancode, int action, int mode ) | |
{ | |
if( action == GLFW_PRESS ) | |
cout << "you pressed something..\n"; | |
if( action == GLFW_RELEASE ) | |
cout << "you released the key\n"; | |
if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS ) | |
glfwSetWindowShouldClose(window, GL_TRUE); | |
} | |
long get_time_micro() | |
{ | |
timeval curTime; | |
gettimeofday(&curTime, NULL); | |
long micro = curTime.tv_usec; | |
return micro; | |
} | |
void flicker( bool& flag ) | |
{ | |
if( flag ) | |
{ | |
glClearColor( 0.9f, 0.2f, 0.4f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
flag = false; | |
} | |
else | |
{ | |
glClearColor( 0.2f, 0.9f, 0.4f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
flag = true; | |
} | |
} | |
int main( int argc, char ** argv ) | |
{ | |
// glfw-init | |
glfwInit(); | |
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); | |
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 ); | |
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); | |
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE ); | |
// glfw-window make | |
GLFWwindow * window = glfwCreateWindow(800,600, "LearnOpenGL", nullptr, nullptr); | |
if( window == nullptr ) | |
{ | |
cout << "Failed to create glfw window\n"; | |
return 1; | |
} | |
glfwMakeContextCurrent(window); | |
// glew-init | |
glewExperimental = GL_TRUE; | |
if( glewInit() != GLEW_OK ) | |
{ | |
cout << "Failed to init GLEW\n"; | |
return 1; | |
} | |
// set view-port | |
int width, height; | |
glfwGetFramebufferSize( window, &width, &height ); | |
glViewport(0,0, width, height ); | |
// setup call backs | |
glfwSetKeyCallback( window, key_callback ); | |
// game-loop | |
long prev = get_time_micro(); | |
bool flag = true; | |
while( !glfwWindowShouldClose(window)) | |
{ | |
long curr = get_time_micro(); | |
printf( "%-2ld msec\n", (curr - prev)/1000 ); | |
prev = curr; | |
// pool-events | |
glfwPollEvents(); | |
long renderStart = get_time_micro(); | |
// render | |
flicker( flag ); | |
// end rendering | |
printf( "%-2ld micro-sec (render)\n", (get_time_micro() - renderStart) ); | |
// swap-buffers | |
glfwSwapBuffers(window); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment