Friday, August 24, 2012

OpenGL :: Introduction to OpenGL :: Material 1


OpenGL :: Introduction to OpenGL :: Material 1


                      consists of :
  • What is OpenGL?
  • What should I install?
  • First OpenGL with SDL

What is OpenGL?

User Image
OpenGL (Open Graphics Library) is a comprehensive graphical blibliothèque that allows programmers to develop 2D, 3D quite easily. 
Already have had to use it or hear as many games as Quake III, offering the as OpenGL display mode.



User Image
Quake III Arena


Okay, I must admit after reading this tutorial you will not even make games like that (especially because of the physics engine to handle collisions, and travel shots) but it is nice to dream. OpenGL is mainly used in C + +, so it is advisable to know the language. But do not worry! Even without a base you will perfectly understand all the examples of current applications, however you will have to develop for yourself may be less extensive. Reading the tutorial C / C + + is highly recommended .: P



It is possible to use OpenGL in Python, Delphi, Java, and controls are similar. Here we use the C / C + + to take advantage of being already present on the site.


A host of features



OpenGL has many features that you can "easily" use, including the management of:



User Image
  • the camera;
  • 3D rotation of objects;
  • filling of the faces;
  • textures;
  • light;
  • and much more ...



In this tutorial I intend to show you a maximum of techniques such as the generation of a terrain 3D engine particles , thebump-mapping , the cell-shading , etc.. How then does not drool at all the possibilities open to you? Especially as most are easily accessible from the point of view of programming difficulty.

From a pedagogical point of view, we see that the 3D from 4 th chapter as basic knowledge necessary to launch correctly.


Windowing and Events



OpenGL provides only 3D functions that must be performed in a graphical context already created. So we must choose a system to create windows and handle events to provide interactivity applications. OpenGL is implemented on many platforms, I chose you to use SDL (especially since its installation / use you taught in the course of M @ teo ). The SDL will allow us:


  • create a window;
  • easily load textures with SDL_image ;
  • use the keyboard and mouse;
  • animate our scenes.


So we combine the ease of use of the SDL with the power of OpenGL while keeping the cross-platform side.

And 3D engines?



I will detail later in this tutorial which differs OpenGL 3D engines and how to create a 3D engine based on OpenGL. Anyway what you learn here is often applied elsewhere and provides explanations on the 3D programming. Delve into an engine such as Irrlichtor Ogre will be much easier if you know what they use behind.

What should I install?

You finally decided to get started? ^ ^Perfect!

Files needed to develop



If you downloaded Code :: Block or DevC + + to follow the tutorial for C / C + + does not change anything, you already have the headers and. was necessary. You must have:

OSincludelib
WindowsGL / gl.h
GL / glu.h
* libopengl32.a
libglu32.a
Unix **libopengl.a
libglu.a


* If you are using Visual Studio, you must have the equivalent. Lib. 
** We will see when creating our first program what exactly add Linux.

The files needed to run



  • Under Windows unless very exceptional rare case you already have. dll is needed: opengl32.dll and glu32.dll . You do not have to provide them with your executable, because they are present by default in Windows.
    You will continue to provide. Dll SDL.
  • Under Linux you need them. associated so. has.


Check that the 3D acceleration is enabled



Have the correct files does not necessarily mean that 3D applications will run perfectly. If hardware acceleration is not enabled, the performance will be greatly reduced.
, so make sure you have installed the latest drivers for your graphics card. Windows if you are used to play video games without problems is that it is good! famous Linux using the program glxgears to test. The instructions are too dependent on your distribution and your card, refer to the many resources available on the Internet.: D

First OpenGL with SDL

As I said earlier we use OpenGL in SDL context. The first thing to do is to create a basic SDL project (as explained in the tutorial teo @ M). We will come to replace all the code and complete the compilation options to add OpenGL during the editing phase relationships.

If you use an IDE that provides project templates (like Code :: Blocks), do not choose a project but OpenGL SDL. We will complete a project for SDL / OpenGL.


I will provide also the end of the chapter used the final draft and you can use it as a starting point for your OpenGL applications created by following this tutorial.

Start code



Remember the minimum code to open a window SDL:Code: C + +

Select
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Include <SDL/SDL.h>

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

    SDL_Surface *  screen  =  SDL_SetVideoMode ( 640 ,  480 ,  32 ,  SDL_HWSURFACE );

    SDL_Flip ( screen );

    bool  continue  =  true ; 
    SDL_Event  event ;

    while  ( continue ) 
    { 
        SDL_WaitEvent ( & event ) 
        switch ( event . kind ) 
        { 
            case  SDL_Quit: 
                continue  =  false ; 
        } 
    }

    SDL_Quit ();

    return  0 ; 
}


I do not voluntarily comply with the rules as I compile C + + and C invite you to do the same in the future for this tutorial.Indeed, the concepts that we will see later will be much more easily implementable in C + +. If you only know the C for now, do not worry, we will introduce the concept of "object" in a simple and intuitive.


Initialize SDL OpenGL



The first thing to change is at initializing video mode. We will use SDL_OPENGL instead of SDL_HWSURFACE :Code: C + +
Select
1
SDL_SetVideoMode ( 640 ,  480 ,  32 ,  SDL_OPENGL );


What becomes SDL_DOUBLEBUF normally used with SDL to enable double-buffering (see Working with the double buffer )?


The equivalent OpenGL would call SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1) but it turns out that it is already enabled by default for OpenGL. Nothing special to do so.

The first drawing



Then we will make our first OpenGL calls to draw a simple triangle. 
therefore We add necessary headers:Code: C + +
Select
1
2
<GL/gl.h> # Include 
# include <GL/glu.h>


With Visual Studio (only) must add # include <windows.h> before including headers OpenGL.


Here is the code of the triangle will be explained in the next chapter:Code: C + +
Select
1
2
3
4
5
6
7
glClear ( GL_COLOR_BUFFER_BIT );

    glBegin(GL_TRIANGLES);
        glColor3ub(255,0,0);    glVertex2d(-0.75,-0.75);
        glColor3ub(0,255,0);    glVertex2d(0,0.75);
        glColor3ub(0,0,255);    glVertex2d(0.75,-0.75);
    glEnd();


SDL_Flip (screen) disappears in favor of the following call:Code: C + +
Select
1
2
glFlush (); 
    SDL_GL_SwapBuffers ();


The first command, glFlush , just make sure all OpenGL commands have been executed, and SDL_GL_SwapBuffers is the equivalent of the old SDL_Flip.

The complete code



Code: C + +- Select
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<SDL/SDL.h> # Include 
# include <GL/gl.h> 
# include <GL/glu.h>

int  main ( int  argc ,  char  * argv []) 
{ 
    SDL_Init ( SDL_INIT_VIDEO ) 
    SDL_WM_SetCaption ( "My first OpenGL program" , NULL ); 
    SDL_SetVideoMode ( 640 ,  480 ,  32 ,  SDL_OPENGL );

    bool  continue  =  true ; 
    SDL_Event  event ;

    while  ( continue ) 
    { 
        SDL_WaitEvent ( & event ) 
        switch ( event . kind ) 
        { 
            case  SDL_Quit: 
                continue  =  false ; 
        }

         glClear ( GL_COLOR_BUFFER_BIT );

        glBegin(GL_TRIANGLES);
            glColor3ub(255,0,0);    glVertex2d(-0.75,-0.75);
            glColor3ub(0,255,0);    glVertex2d(0,0.75);
            glColor3ub(0,0,255);    glVertex2d(0.75,-0.75);
        glEnd();

        glFlush (); 
        SDL_GL_SwapBuffers (); 
    }

    SDL_Quit ();

    return  0 ; 
}


Compilation



To compile we need to add the files mentioned above. In our project options so we add:

IDE / Compilerlib
Code :: Blocks WindowsOpenGL32
GLu32
DevC + + Windows-Lopengl32
-lglu32
Visual Studio Windowsopengl32.lib
glu32.lib
Code :: Blocks LinuxGL
GLU
gcc / g+ + Linux-LGL-lGLU


Result ::



User Image

Download The Code and execution file --- >>    click here
program code file --- >>click here


No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin