Showing posts with label GLFW graphics. Show all posts
Showing posts with label GLFW graphics. Show all posts

Tuesday, August 27, 2013

Basic GLFW Tutorials with C++

GLFW (OpenGL FrameWork) is open source library to create OpenGL Window and multi-platform  too. Very easy to manage OpenGL context and event with GLFW. It is specially suitable for game development. GLFW doesn't render 3D scene for that you have to use OpenGL library function. GLFW help to create window , handle keyboard , mouse and joystick events. This tutorials is for those who are new to OpenGL and GLFW. By the end of this tutoirals series understand the basic knowledge about OpenGL and handling events with GLFW.

Tutorials 1: Setup GLFW in Code::Blocks IDE in Windows Machine

Tutorials 2:  Introduction to GLFW

Tutorials 3: Drawing Basic Shapes

Tutorials 4: Transformation: Translation, Rotation and Scalling

Tutorials 5: Texture Mapping 

Saturday, April 28, 2012

GLFW Tutorial 4: Texture mapping sample code in OpenGL Framework

If you are new to the GLFW(OpenGL framework) you can follow beginning tutorial here.
On Contrary with GLUT texture mapping is very easy in GLFW . We want to use pre-generated 2D images for surface textures, light maps, transparency maps etc.These images are stored with a standard image format in a file, which requires the program to decode and load the      image(s) from file(s), which can require much work from the programmer.To make programming easier for OpenGL developers, GLFW has built-in support for loading images from files.
In this tutorial I give example for surface textures only. Surface textures are important because it helps to make object realistic.
To load a texture from a file, you can use the function glfwLoadTexture2D:
int glfwLoadTexture2D( const char *name, int flags )
This function reads a 2D image from a Truevision Targa format file (.TGA) with the name given by name, and uploads it to texture memory. It is similar to the OpenGL function glTexImage2D, except that the image data is read from a file instead of from main memory, and all the pixel format and data storage flags are handled automatically. The flags argument can be used to control how the texture is loaded. If flags is GLFW_ORIGIN_UL_BIT, the origin of the texture will be the upper left corner (otherwise it is the lower left corner). If flags is GLFW_BUILD_MIPMAPS_BIT, all mipmap levels will be generated and uploaded to texture memory (otherwise only one mipmap level is loaded). If flags is GLFW_ALPHA_MAP_BIT, then any gray scale images will be loaded as alpha maps rather than luminance maps. To make combinations of the flags, or them together (e.g. like this: GLFW_ORIGIN_UL_BIT |GLFW_BUILD_MIPMAPS_BIT).
Here is the sample program to illustrate the texture mapping with complete source code written in c++ with gcc compile and code::blocks IDE. You can also download the project file.

Tuesday, April 17, 2012

GLFW(OpenGL FrameWork) tutorial 3:Transformation:Translation,Scaling and Rotation.

If you are beginner to OpenGL FrameWork (GLFW) visit my previous tutorial lesson Here so that you can understand in details.Translation ,Scaling and Rotation of object is so easy because OpenGL provide these command to us we don’t have to write our own  Transformation matrix.

Translation command:

Translation is movement along any of the three axis in a 3D scene, for example, moving something the left is a translation on the X axis if you are looking straight on.
Command:
glTranslatef( xValue, yValue, zValue );
e.g:
glTranslate(5.0, 0.0, 0.0);
This translate the scene five unit along positive x-axis. One things you must remember ,all the scene drawn below this command will be translated.
So,if you want to translate only one object then you have to restore the transformation. Restore can be done by putting drawing scene and translation command between glPushMatrix() and glPopMatrix() .

Monday, April 16, 2012

GLFW (OpenGL Framework) tutorial 2: Drawing Basic Shapes

Lets first discuss how OpenGL does. Describing co-ordinates system of OpenGL, center of co-ordinate is middle of widow, positive x-axis is pointed towards right and negative x-axis toward left ,positive y-axis is pointed towards up and negative y-axis towards bottom , positive z-axis is pointed towards outside of screen and negative z-axis towards inside the screen.
  • coord_system
OpenGL draw object similar to our eye . By default eye is in origin and we are looking toward negative z-axis.

GLFW tutorial 1:Introduction to OpenGL Framework

Welcome to codeincodeblock.blogspot.com I have created this tutorial for 3D programming in OpenGL for beginners. OpenGL has wide range of use some of them is Gamming ,simulation, virtual prototyping. This tutorial provide only basic knowledge of OpenGL in brief with full source code and complete explanation so, that you can move quickly to advanced OpenGL programming. All tutorials is written in c++ so that you must know the basic c++ to understand this tutorial.
We use OpenGL for 3d programming. You may wondering what is OpenGL. OpenGL is software interface for graphics hardware in other word we can say it is language translator so that we can use only one language for different graphics hardware. OpenGL is hardware independent. We can draw both 3D and 2D stuff. For 2D their are better libraries like SDL ,Allegro. We use OpenGL basically for 3D stuff.
I use GLFW library and code::blocks IDE. You may confused reading OpenGL and using GLFW library, what does GLFW means? The creation of a window to render stuff in is not covered in the OpenGL specification. This is handled by platform-specific APIs. GLFW is one of this  APIs. It is a crossplatform windowing and keyboard/mouse/joystick handler. Contrary to GLUT, this is more aimed for creating games. Supports Windows, Mac OS X and Unix-like systems such as Linux and FreeBSD.
You don’t have code::blocks IDE then download it form Here .There are two options of download so, choose codeblocks-10.05mingw-setup.exe file in which MinGW compiler is already install. Then you need to setup the GLFW library in code::blocks. Follow my previous article Setup GLFW Project for setup.
Now you are ready for OpenGL 3d programming..
Goto tutorial 2 Drawing Basic Shape

Sunday, February 27, 2011

Setup GLFW 3 library in Codeblocks IDE

GLFW is a free, open-source, multi-platform library for OpenGL and OpenGL ES especially design for game development. OpenGL itself does not provide features to handle window creation and other input, so GLFW is very useful to create the necessary context, manage windows, timing, read and control user input via keyboard, mouse, joysticks and other purposes. There are many other library alternatives to GLFW such as SDL and freeglut but if you are looking best library for 3D game development then GLFW is best. GLFW community is large and very active, at the time of updating this article latest stable version of GLFW is 3.3.2 which is released on January 20, 2020. It comes with documentation, high standard tutorials and guides and lots of test programs.


Here, I will guide you through a step by step procedure to setup GLFW 3 in Codeblocks IDE on Windows 10.

Visit GLFW download page https://www.glfw.org/download.html and download 32 bit/64 bit windows binary files from the Windows pre-compiled binaries section. You have to make sure that bit of your C++ compiler and downloaded GLFW library files should match. Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010 and above version.

Steps to setup GLFW library and create a project in Code::blocks:
  1. Extract downloaded zip file.
  2. Create a folder in any directory and give a name "GLFW" (eg. c:\GLFW).
  3. Browse extracted files in step 1. Copy "include" folder and paste it inside the directory that you have created in step 2. Copy lib folder of corresponding to your compiler for e.g if you are using minGW then copy lib-mingw folder and paste it inside the directory that you have created in step 2 and rename it to lib. Also copy glfw3.dll file from bin folder to "C:\Windows\System32" if you are using 32-bit library files or copy to "C:\Windows\System" if you are using 64-bit library files.
  4. Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
  6. Go to the menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under the "Compiler" tab add the path of GLFW include folder (e.g. c:\GLFW\include) and under "Linker" tab add the path of GLFW lib folder (e.g. c:\GLFW\lib).
  7. Now go to "Linker Setting" tab and add three linker libglfw3.a, libglfw3dll.a and libopengl32.a. Linker libglfw3.a and libglfw3dll.a are available inside GLFW lib folder and libopengl32.a is available inside minGW compiler lib folder.
Copy and paste below code in main.cpp file and compile it to verify GLFW library setup.

#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>

GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;

int main(int argc, char *argv[]) {

    if(!glfwInit())
    throw std::runtime_error("glfwInit failed");

    window = glfwCreateWindow(800, 600, "test window",NULL, NULL);
    if(!window)
        throw std::runtime_error("glfwOpenWindow failed.");

   // GLFW settings
    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window)){
    // process pending events
       glfwPollEvents();
       glClearColor(1, 0, 0, 1); // black
       glClear(GL_COLOR_BUFFER_BIT);
       glfwSwapBuffers(window);
    }
   // clean up and exit
   glfwTerminate();
    return 0;
}

You have to make sure to define GLFW_DLL  and include glfw3.h header always on top of your source code as shown in the above sample code otherwise the compiler will throw undefine reference error.

Please watch the video below for a detailed explanation.