OpenGL Getting Started Tutorial 1

Source: Internet
Author: User

Speaking of programming, there are probably a lot of people think of TC's # include <graphics.h>?
But have you ever wondered how those fancy PC games were written? By the TC, the poor 640*480 resolution, 16
Color to do it? Obviously it's not going to work.
The purpose of this post is to let everyone give up the old interface of the TC, let everyone touch some new things.
OpenGL, one of the most popular graphics APIs, has features that are superior to DirectX in some situations.
1. Close integration with C language.
OpenGL commands are initially described in C-language functions, and for those who have studied C, OpenGL is easy to understand and
To learn. If you have ever contacted TC Graphics.h, you will find that using OpenGL is even simpler than TC.
2, the powerful portability.
Microsoft's Direct3D is also a very good graphics API, but it's only for Windows systems (now plus an XBOX tour
Play machine). OpenGL is not only used for Windows, but also for other systems such as Unix/linux, even in mainframe computers, various
Applications are available on professional computers (e.g. medical display devices). Also, OpenGL's basic commands are hardware-independent, even
Platform-Independent.
3, high-performance graphics rendering.
OpenGL is an industry standard, its technology keep up with the times, today, all the video card manufacturers do not have a strong support for OpenGL, fierce
's competition has led to OpenGL performance.
In short, OpenGL is a very NB graphics software interface. As for the number of NB, take a look at professional tours such as DOOM3 and QUAKE4
The play will know.
OpenGL Official website (US site)
http://www.opengl.org
I'll take a brief introduction to OpenGL programming under Windows.
Preparation before learning OpenGL
The first step is to select a compilation environment
Now the main Windows System compiler environment has visual Studio, Broland C + + Builder, dev-c++, and so on, they are all support OpenGL
Of But here we choose Visual Studio 2005 as the environment for learning OpenGL.
Step two, install the GLUT Toolkit
GLUT is not necessary for OpenGL, but it will give us a certain convenience in learning, recommended installation.
GLUT in the Windows environment: (approximately 150k in size)
Http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
If you cannot download from the above address, please use the following connection:
Http://www.sundablog.com/1.zip
Steps to install GLUT in a Windows environment:
1, will download the compressed package to untie, will get 5 files
2. Search for "gl.h" in "My Computer" and locate its folder (if VisualStudio2013, it should be under its installation directory
C:\Program Files (x86) \ Windows Kits\8.1\include\um\gl folder). Put the extracted glut.h into this folder.
3. Put the extracted glut.lib and glut32.lib into the folder where the static function library is located (if it is VisualStudio2013, it should be its security
"C:\Program Files (x86) \ Windows Kits\8.1\lib" folder below the directory).
4. Put the extracted glut.dll and Glut32.dll in the System32 folder under the operating system directory. (The typical location is:
C:\WINDOWS\SYSTEM32)
Step three, build an OpenGL project
Here take VisualStudio2013 as an example.
Select File->new->project, then select Win32 Console program, click OK
First OpenGL Program

A simple OpenGL program is as follows: (Note that if you need to compile and run, you need to install GLUT correctly, installation method as described above)

#include <gl/glut.h>void mydisplay (void) {glclear (gl_color_buffer_bit); GLRECTF ( -0.5f, -0.5f, 0.5f, 0.5f);< Span style= "White-space:pre" ></span>glflush ();} int _tmain (int argc, char* argv[]) {<span style= "White-space:pre" ></span>glutinit (&ARGC, argv);< Span style= "White-space:pre" ></span>glutinitdisplaymode (Glut_rgb | <span style= "White-space:pre" ></span>glutinitwindowposition (glut_single); <span style= " White-space:pre "></span>glutinitwindowsize", <span style= "White-space:pre" ></span> Glutcreatewindow ("First OpenGL program"), <span style= "White-space:pre" ></span>glutdisplayfunc (&myDisplay ); <span style= "White-space:pre" ></span>glutmainloop (); <span style= "White-space:pre" ></span >return 0;}
The function of the program is to draw a white rectangle in the center of a black window. Each line statement is described below.
What do you think? The code isn't long, is it?
First, you need to include the header file # include <gl/glut.h> this is the glut header file.
Originally OpenGL program will also include <GL/gl.h> and <gl/glu.h&gt, but GLUT's header file has automatically included these two files
, it does not have to be included again.
Then look at the main function.
int main (int argc, char *argv[]), this is the main function with command line arguments, you should have seen it? The comrades who have not met have asked for more
Turn over the book, and then look down.
Note that the statements in the main function, except for the last return, all begin with glut. This function, which starts with glut, is
The functions provided by the GLUT Toolkit are described below for several functions that are used.
1, Glutinit, initialize the GLUT, this function must be called before the other GLUT use. Its format is rather rigid,
Generally copy this sentence glutinit (&AMP;ARGC, argv) on the can.
2, Glutinitdisplaymode, set the display mode, wherein GLUT_RGB is used RGB color, and the corresponding Glut_index
(indicates the use of indexed colors). The glut_single represents the use of a single buffer, which corresponds to a glut_double (using double buffering).
For more information, please Google yourself. Of course, later tutorials will also have some explanation.
3, Glutinitwindowposition, this simple, set the window in the position of the screen.
4, Glutinitwindowsize, this is also simple, set the size of the window.
5, Glutcreatewindow, create a window based on the information set earlier. The parameter is used as the caption of the window. Note: After the window is created,
Does not immediately appear on the screen. You need to call Glutmainloop to see the window.
6, Glutdisplayfunc, set a function that will be called when drawing is required. (This statement is not accurate enough, but
The exact saying may not be very good for beginners to understand, for the time being.
7, Glutmainloop, a message loop. (This may not be easy for beginners to understand, now just need to know that this function can show
window, and wait for the window to close before returning, which is enough. )
In the Glutdisplayfunc function, we set the "Call Mydisplay function when drawing is required." So the Mydisplay function is
Used for drawing. Observe the three function calls in Mydisplay and find that they all start with GL. This function, starting with GL, is OpenGL.
Standard functions, the functions used are described below.
1, Glclear, clear. Gl_color_buffer_bit means clear color, glclear function can also clear other things, but here
Not introduced.
2, GLRECTF, draw a rectangle. The four parameters represent the horizontal and vertical coordinates of two points located on the diagonal.
3, Glflush, ensure that the previous OpenGL command executes immediately (instead of letting them wait in the buffer). Its effect with fflush (stdout)
Similar.



OpenGL Getting Started Tutorial 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.