OpenGL :: Introduction to OpenGL :: Material 1
consists of :
- What is OpenGL?
- What should I install?
- First OpenGL with SDL
What is OpenGL?
Already have had to use it or hear as many games as Quake III, offering the as OpenGL display mode.
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 .
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:
- 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!
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:
* If you are using Visual Studio, you must have the equivalent. Lib.
** We will see when creating our first program what exactly add Linux.
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.
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:
OS | include | lib |
---|---|---|
Windows | GL / 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.
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 / Compiler | lib |
---|---|
Code :: Blocks Windows | OpenGL32 GLu32 |
DevC + + Windows | -Lopengl32 -lglu32 |
Visual Studio Windows | opengl32.lib glu32.lib |
Code :: Blocks Linux | GL GLU |
gcc / g+ + Linux | -LGL-lGLU |
Result ::
Download The Code and execution file --- >> click here
program code file --- >>click here
No comments:
Post a Comment