terça-feira, 25 de junho de 2013

SimpleGL #1 - Hello Window



Hello everyone, today I'm going to create a new series of posts that I will tell about my new project that I'm starting work: Simple OpenGL.
So Why am I doing this? Well, I need to learn OpenGL to use in another (and serious) project, but I hate to learn copying and paste tutorials around the web so I always try to use then to create something different, in this case, an API that simplify the use of OpenGL 3 and above (called Modern OpenGL). 
In this first post I will tell what I want to do at the beginning of this project.


I want to work with OpenGL  like I'm a movie director creating the scene, setting where will be the camera, the light and the objects and I want to work with skeletons to do my animations but it's a advanced topic.






I won't teach how to install OpenGL because there are a lot of sites that show how to do it, this tutorials are to teach (and to I learn either) how to use OpenGL in a simple way using object orientation. So I think that it's enough to chit-chat and let's start and code something.

Until now, I divide my code in three files in different folders, these are  OpenGLControl.h, OpenGLControl.cc and main.cpp. OpenGLControl.h has the class interface, in other words the definition of the class and all of its functions and methods without implementation. The file OpenGLControl.cc has the implementation of all the functions and methods of the class OpenGlControl. And finally the main.cpp that doesn't need explanation. Except the main file I put the others in a folder that I can use in every project that I want to create so in my workspace  I created the folder SimpleGL with 2 sub folders: lib  where I am putting all the .h files and include where  I'm putting the .cc files where is the code of my classes.

The class OpenGLControl has the job to create an environment where we can draw our scene, so in the code bellow, the class OpenGLControl only initialize the OpenGl and create a window using the glfw library.




//**********************************************************************//
// OpenGLControl.h - The class that create and manage an environment //
// to use OpenGL functions.      //
//         //
// Autor: Luiz Augusto Volpi Nascimento.    //
// version: 0.1        //
// data: 17/06/2013       //
//         //
//**********************************************************************//

#ifndef _OPENGLCONTROL_H_
#define _OPENGLCONTROL_H_

//Common includes
#include 
#include 
//OpenGL includes
#include 
#include 
#include 
#include 
using namespace glm;
using namespace std;
namespace vri{
 
 class OpenGLControl{
  private:
  public:
   //Method that initialize the opengl resources.
   void initOpenGL(const char* title, int width, int height, int minorVersion, int majorVersion);
   //Method that is responsible to adjust the window when it is resized.
   void resizeWindow();
   //Method that clean the memory releasing the opengl resources.
   void releaseOpenglControl();


 };
}
#endif

Set namespace isn't necessary, I'm just using to say "this is my code", nothing important.

OpenGLControl.cc
#include "../lib/OpenGLControl.h"

void vri::OpenGLControl::initOpenGL(const char* title, int width, int height, int minorVersion, int majorVersion){
 if( !glfwInit() ){
      fprintf( stderr, "Error: Failed to initialize GLFW\n" );
      exit(-1);
 }
 glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing
 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, majorVersion); // We want OpenGL 3.3
 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, minorVersion);
 if(majorVersion > 2)
  glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
  
 // Open a window and create its OpenGL context
 if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
 {
     fprintf( stderr, "Error: Failed to open GLFW window\n" );
     glfwTerminate();
     exit(-1);
 }
  
 // Initialize GLEW
 glewExperimental=true; // Needed in core profile
 if (glewInit() != GLEW_OK) {
     fprintf(stderr, "Error: Failed to initialize GLEW\n");
     exit(-1);
 }
  
 glfwSetWindowTitle( title );

}
and lastly the main file.
#include 
#include "../SimpleGL/lib/OpenGLControl.h"

using namespace std;

int main(){
 vri::OpenGLControl vrigl;
 vrigl.initOpenGL("My first Window",1024,768,3,3);
 // Ensure we can capture the escape key being pressed below
 glfwEnable( GLFW_STICKY_KEYS );
  
 do{
     // Draw nothing, see you in tutorial 2 !
  
     // Swap buffers
     glfwSwapBuffers();
  
 } // Check if the ESC key was pressed or the window was closed
 while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
 glfwGetWindowParam( GLFW_OPENED ) );
 
}
And the result is:

TADÁÁÁÁ

I hope that you enjoy this tutorial.Thanks and see you!

Nenhum comentário:

Postar um comentário