Document directory
- Display pixel data in simple memory
-
- Display of Bitmap files in OpenGL
- References
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie
Discuss newsgroups and documents
Technorati label: OpenGL
, Bitmap
I personally think that OpenGL programming guide
Chapter 1 of the book is the most dizzy one. It tells a lot about the content, but many things are too biased towards pure theoretical concepts and detailed descriptions of many function parameters, it may be difficult for the author to know the content, so I configured the most intensive illustration in the book for this chapter. However, I personally feel that those pictures are not helpful -_-!.
We can't learn too many things and theories. This section only deals with one very important thing, but this book has not touched on for a long time. It reads data from a BMP file and then shows it. Of course, this does not blame the author. After all, reading data is no longer part of the OpenGL API, but because of this, I always use a bunch of disgusting black and white images generated through brutal Array Operations for demonstration and teaching. Is it too much?
In addition, the nehe tutorial contains the relevant sections on loading graphics. It is a bit out of date to use the Glaux library, which is no longer recommended. I don't want to use this library, the best way is to understand the format of the bitmap file in bytes, read the relevant data directly, and then load it into its own structure. This can be used as a full cross-platform (irrlicht approach ), I have no determination to do this. Since it is OpenGL programming learning under Win32, Win32 API is always my first consideration, and it is also concise... Just don't discuss cross-platform issues with me. In fact, this statement is worth exploring .... I don't care about cross-platform use OpenGL. Why? -_-!
Display pixel data in simple memory
GLubyte rasters[24] = { 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xff, 0x00, 0xff, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xff, 0xc0, 0xff, 0xc0};
// Perform all the drawing operations here void sceneshow (glvoid) {glclear (gl_color_buffer_bit); // clear the color buffer glcolor3f (1.0, 0.0, 0.0); glpixelstorei (gl_unpack_alignment, 1 ); // pixel storage mode (Word Alignment/1 bytes) glrasterpos2f (0, 0); glbitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters); glbitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters); glwindowpos2i (0, 0); glbitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters ); glbitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters); glbitmap (10, 12, 0.0, 0.0, 11.0, 0.0, rasters); glflush ();}
The displayed results are as follows:
Because it is too simple, a brief description of the Code:
In this example, there are mainly four OpenGL APIs, but they are very simple, because there are few related concepts involved.
Glpixelstore * is used to specify the pixel storage format. In this example, it represents an element of 1 byte.
OpenGL Reference Manual
":
Name
Glpixelstore-set pixel storage modes
C Specification
Void glpixelstoref (glenum pname,
Glfloat PARAM );
Void glpixelstorei (glenum pname,
Glint PARAM );
This function has many parameters, but it is relatively simple to use. You can check the specific explanations by yourself.
Glrasterpos * And glwindowpos * are used to specify the position where the pixel is to be drawn. The preceding example also shows the difference between the two functions. After glrasterpos * is determined, two F functions are drawn, and all are in the middle of the window, (0, 0). The position of this function is based on the normal OpenGL coordinate system. Therefore, it is also affected by OpenGL coordinate transformation. Glwindowpos2i is a special function, because it does not take into account OpenGL coordinate transformation at all and always locates Based on the window coordinate system (this is useful when drawing the interface) although the actual coordinate positioning method is a little different from the conventional windows method, the origin is not in the upper left corner but in the lower left corner. In this case, glwindowpos * is a | _ coordinate system, the orientation of the coordinate system follows the direction of the OpenGL coordinate system (that is, the direction of the normal Cartesian coordinate system ).
OpenGL Reference Manual
":
Glrasterpos-specify the raster position for Pixel operations
Glwindowpos-specify the raster position in window coordinates for Pixel operations
To better understand the certainty of glwindowpos * and the variability of glrasterpos *, here I add the offset introduced by gltranslate (gltranslatef (0.5, 0.0, 0.0 );), you can see that the location determined by glrasterpos * has changed, and the glwindowpos * is still the old location.
In addition, it should be noted that the function call for the above plot F is the same, but the painting does not overlap, because the API itself contains the offset parameter of the painting position.
In order to save space, only key clips are posted. For the complete source code, see the 2009-12-28/glpixeldraw directory of the source code of my blog. For more information about how to obtain the complete source code of the blog, see the article.
Display of Bitmap files in OpenGL
Take the film from the classic book Windows graphic programming.
Reading the data of a bitmap file is a problem. In fact, the bitmap file itself is very simple. It is not too difficult to completely parse and read the data through a fixed structure, lamothe (Windows game programming master skills, 3D game programming master skills) is introduced in the book, but I will not do this, it is not the same as learning OpenGL.
As described at the beginning, Windows API is used to complete the process. The following is the Win32 bitmap structure:
/* Bitmap Header Definition */typedef struct tagBITMAP { LONG bmType; LONG bmWidth; LONG bmHeight; LONG bmWidthBytes; WORD bmPlanes; WORD bmBitsPixel; LPVOID bmBits; } BITMAP, *PBITMAP, NEAR *NPBITMAP, FAR *LPBITMAP;
All we need are available, long, high, and actual pixels.
BITMAP gBmp;
// OpenGL initialization starts void sceneinit (int w, int h) {glenum err = glewinit (); If (Err! = Glew_ OK) {MessageBox (null, _ T ("error"), _ T ("glew init failed. "), mb_ OK); exit (-1);} hbitmap hbmp = (hbitmap) LoadImage (null," tiger.bmp ", image_bitmap, 0, 0, lr_createdibsection | lr_loadfromfile ); if (! Hbmp) {exit (3);} GetObject (hbmp, sizeof (gbmp), & gbmp); glclearcolor (0.0, 0.0, 0.0, 0.0 );}
Msdn function prototype:
Handle LoadImage (
HinstanceHinst
, LpctstrLpszname
, UintUtype
, IntCxdesired
, IntCydesired
, UintFuload
);
The image is read during initialization. The above two Win32 APIs, one LoadImage is used to load the image and the GetObject is used to obtain information. Note that, do not see a type that specifies the image type. The LoadImage is very powerful ....
Utype
[In] specifies the type of image to be loaded. This parameter can be one of the following values.
-
Image_bitmap
-
Loads a bitmap.
-
Image_cursor
-
Loads a cursor.
-
Image_icon
-
Loads an icon.
Illusion .............. After all, it is a Win32 API. You think d3d API .......... Haha, even the cimage in MFC is weak. I still expect this function .............
With the data, let's talk about it.
// Perform all the drawing operations here void sceneshow (glvoid) {glclear (gl_color_buffer_bit); // clear the color buffer glpixelstorei (gl_unpack_alignment, 4 ); // pixel storage mode (Word Alignment/4 bytes) glwindowpos2d (width-gbmp. bmwidth)/2, (height-gbmp. bmheight)/2); gldrawpixels (gbmp. bmwidth, gbmp. bmheight, gl_bgr, gl_unsigned_byte, gbmp. bmbits); glflush ();}
Here, the glwindowpos2d function is used and the image length and width are calculated to display the image in the middle of the window.
Ha .......... After learning OpenGL for so long, I used the API for the first time to draw images (indeed a little late). I used to understand the box and solid body, let's look at the different pictures .......... (In fact, this rendering process is actually much easier to implement through Win32 API, But who told us to learn OpenGL)
Here there is only one more function: gldrawpixels
OpenGL Reference Manual
":
Gldrawpixels-write a block of pixels to the frame buffer
C Specification
Void gldrawpixels (glsizei width,
Glsizei height,
Glenum format,
Glenum type,
Const glvoid * data );
Parameters
Width, height: Specify the dimensions of the pixel rectangle to be written into the frame buffer.
Format: Specifies the format of the pixel data.
Type: Specifies the data type for data.
Data: Specifies a pointer to the pixel data.
There is nothing strange here. The only strange thing I have is that for images in windows, OpenGL specifies that the type should be gl_bgr rather than gl_rgb... Let's explain it to someone else.
In addition, we can use the glpixelzoom function to scale the image. For example, the following code expands the image horizontally by two times and vertically by 1.5 times.
// Perform all the drawing operations here void sceneshow (glvoid) {glclear (gl_color_buffer_bit); // clear the color buffer glpixelzoom (2.0, 1.5); glpixelstorei (gl_unpack_alignment, 4 ); // pixel storage mode (Word Alignment/4 bytes) glwindowpos2d (width-gbmp. bmwidth)/2, (height-gbmp. bmheight)/2); gldrawpixels (gbmp. bmwidth, gbmp. bmheight, gl_bgr, gl_unsigned_byte, gbmp. bmbits); glflush ();}
Display Effect:
To save space, only key clips are posted. For the complete source code, see the 2009-12-28/glbitmaptest directory of the source code of my blog. For more information about obtaining the complete source code of my blog, see the article.
For other articles in this series, see the OpenGL topic "Win32 OpenGL series topics ".
"
References
1. OpenGL Reference Manual
, OpenGL Reference Manual
2. OpenGL
OpenGL programming guide
), Dave shreiner, Mason Woo, Jackie neider, Tom Davis
Xu Bo, Mechanical Industry Press
3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.
4. [Game Development] Three Methods of texture Paster using BMP images in OpenGL
"
Complete source code retrieval instructions
Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:
Https://blog-sample-code.jtianling.googlecode.com/hg/
For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"
If you only want to browse all the code, you can go to Google Code to view it. the following URL:
Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code
The author of the original article retains the copyright reprinted. Please indicate the original author and give a link
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie