NEHE OpenGL Tutorial Delphi Edition (6) ---- Texture Mapping (Picture)

zhaozj2021-02-16  44

{

The map can greatly save the CPU time. Oh, but this festive time is much more time: (

Because of the use of OpenGL's auxiliary library, the function of this library is now rarely used, but I still found it, thank ZDCNOW, he gives me this auxiliary library Delphi version. Before studying this section, please download Glaux.dll, Glaux.PAS files online, and add it to the project.

Ok, let's continue the road of OpenGL.

First we need to add the sysutils unit because we have to use the file operation in this section, we also add the GLAUX unit.

Then we plus several variables, XROT, YROT, and ZROT on the basis of the first lesson. These variables are used to rotate the cube around X, Y, and Z axis. TEXTURE [] is allocated to a texture. If you need more than one texture, you should change the number 1 to the numbers you need.

}

VAR

H_rc: hglrc; // rendering context (Color Description Table). H_dc: hdc; // device context h_wnd: hwnd; // window handle h_instance: hinst; // program instance (instance). Keys: array [0..255] of boolean; // An array of keyboard routines

XROT, // X Rotation (New) YROT, // Y Rotation (New) Zrot: GLFLOAT; / / Z Rotate (New)

TEXTURE: Array [0..1] of gluint; // Store a texture (new)

{Then take the two processes in OpenGL32.DLL, we want to use them}

Procedure Glgentextures (N: Glsizei; Var Textures: gluint); stdcall; External OpenGL32; Procedure GlbindTexture (Target: glenum; texTure: gluint); stdcall; External OpenGL32;

{Next we need to add a new function to re-enter the image, the return type of the function is defined in GLAUX.PAS as follows:

TAUX_RGBIMAGEREC = Record Sizex, Sizey: Glint; Data: Pointer; End; PTAUX_RGBIMAGEREC = ^ Taux_rgbimageRec

The specific meaning will be introduced later}

Function loadBMP (FileName: Pchar): PTAUX_RGBIMAGEREC; VAR BITMAPFILE: THANDLE; / / File Handle Begin // Next Check if filename = '' THEN / / Make sure the file name is provided. Result: = nil; // If not provided, return null // then check if the file exists. BitmapFile: = fileopen (filename, fmopenwrite); // Try Open file // If we can open the file, it is obvious that the file exists. If BitmapFile> 0 THEN / / File exists? Begin // Close the file. FileClose (BitmapFile); // Close the handle // auxdibimageLoad (filename) read the image data and returns it. Result: = AUXDIMAGELOADA (FileName); / / Load bitmap and returns the pointer ELSE // If we can't open the file, we will return nil. Result: = nil; // If load fails, returns NIL. END;

// Next to create a new function, use to load texture map

Function loadTexture: Boolean;

// status variable. We use it to track whether you can load bitmaps and whether you can create textures. // Status is default to false (indicating that there is no load or creation any stuff). // TextureImage variable PTAUX_RGBImageRec type storage bitmap image record. / / The number of widths, heights, and data containing the bitmap are recorded.

Var Status: boolean; TextureImage: Array [0..1] Of PTAUX_RGBImageRec; Begin Status: = false; ZeroMemory (@TextureImage, sizeof (TextureImage)); // pointer to NULL TextureImage [0]: = LoadBMP ( ' Texture.bmp '); if TextureImage [0] <> nil dam status: = true; // Setting Status to True // Now use the data creation texture of TextureImage [0]. // GlGentextures (1, Texture [0]) Tell OpenGL We want to generate a texture name / / (if you want to load multiple textures, increase the number). // GlbindTexture (GL_Texture_2D, Texture [0]) tells OpenGL to bind the texture name Texture [0] to the texture target. // 2D texture is only height (on the Y-axis) and the width (on the x-axis). // The main function assigns the texture name to the texture data. // In this example we inform OpenGL, the memory at & texture [0] is available. // We created the texture that will store the pointer to the point to the point of the & texture [0]. Glgentextures (1, Texture [0]); // Create texture GlbindTexture (GL_Texture_2D, Texture [0]); // Use typical textures generated from bitmap data // to create a real texture. // The following line tells OpenGL this texture is a 2D texture (GL_Texture_2D). // Digital zero represents the image of the image, usually it is zero. // Digital three is the number of data. Because the image consists of three components of red data, green data, blue data. //Textureimage[0].sizex is the width of the texture. // If you know the width, you can fill it here, but your computer can easily point this value for you. // TextureImage [0] .sizey is the height of the texture. // Number zero is the value of the border, usually zero. // GL_RGB tells OpenGL image data consisting of red, green, and blue three-color data. // GL_unsigned_byte means that the data that makes up the image is an unsigned byte type. // Finally ... TextureImage [0] .data tells OpenGL texture sources. // In this case, the data stores in the TextureImage [0] record is stored. // Generate texture GLTexImage2D (GL_Texture_2D, 0, 3, TextureImage [0] .Sizex, TextureImage [0] .Sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, TEXTUREIMAGE [0] .data);

// The following two lines tell OpenGL when displaying the image, / / ​​When it is larger than the original texture (GL_TEXTURE_MAG_FILTER) // or reduces the filtering method used when OpenGL is smaller than the original texture (GL_TEXTURE_MIN_FILTER). // Usually I use GL_LINEAR in both cases. This makes the texture from far away from the screen very close to the screen. // Use GL_LINEAR to need CPUs and graphics cards to do more operations. // If your machine is very slow, you may use GL_Nearest. // Filtering texture When it is magnified, it looks high (mosaic). / / You can also combine these two filtering methods. Use GL_LINEAR when you are close to GL_NEAREST. Gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Line-shaped filter gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Line-shaped filter END; // Now we release the memory of the presence to place diagram data. // Let's first check if you are stored at your bitmap data. // If so, check whether the data has been stored. // If you have already stored, delete it. / / Then release the TextureImage [0] image structure to ensure that all memory can be released. If Assigned (TextureImage [0]) THEN / / Whether IF Assigned (TextureImage [0] .data) THEN // Texture Image There is a TextureImage [0] .data: = nil; // Release Texture Image Used Memory TextureImage [0]: = NIL; // Release the image structure // finally returns the state variable. If everything is OK, the value of the variable status is True. Otherwise, false results: = status; // returns statusend; // then add a few lines of code in Glinit

Procedure Glinit (); begin if (not loadtexture) THEN // Call texture load subroutine (new) exit; // If it is not loaded, exit (new) glenable (GL_Texture_2D); // Enable texture mapping (Add) GlclearColor (0.0, 0.0, 0.0); // Black background Glshademodel (GL_SMOOTH); // Enable Shadow Smoothing GLCLEDEPTH (1.0); // Set Depth Cache Glenable (GL_Depth_test); // Enable Deep Test Gldepthfunc (GL_SSS); // The type of depth test is GLHINT (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); / / True fine perspective correction

END;

{Now we draw a mapped cube. This code was released by arsenic, it should be very well understood. Starting two lines of code Glclear () and gLloadIdentity () are code in the first lesson. GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) Clears the screen and set it to our selected color in Initgl (), this example is black. The depth cache is also cleared. The model observation matrix also uses GLLoadIdentity () reset. } Procedure gldraw (); begin glclear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_bit); // Clear the screen and depth cache GLLoadIdentity (); // Reset the current model observation matrix GLTranslateF (0.0, 0.0, -6.0); // Move into the screen 6 unit

// The following three exercises the cube around X, Y, and Z axis. // How much is dependent on the value of the variables XROT, YROT, and ZROT. Glrotatef (xROT, 1.0, 0.0); // Rotate GLROTATEF around the X-axis; // Rotate GLROTATEF (Zrot, 0.0, 0.0, 1.0); // around the Z-axis Rotate

// The next line of code selects the texture we use. / / If multiple textures are used in the scene, you should use // GlbindTexture (GL_TEXTURE_2D, TEXTURE [Number of Texture to use]) to select the texture you want to bind. // When you want to change the texture, you should bind a new texture. // There is a little worth pointing out that // cannot bind texture between glbegin () and glend (), // must be bound before glbegin () before or after Glend (). / (Note that we will use GlbindTexture to specify and bind textures later.

GlbindTexture (GL_Texture_2D, Texture [0]); // Select Texture

/ / In order to map the texture to the quadrilateral, / / ​​must map the upper right corner of the texture to the upper right corner of the quadrilateral, // The left upper corner of the texture maps to the upper left corner of the quadrilateral, // Texture, the lower right corner of the texture maps to the quadrilateral The lower right corner, // The left corner of the texture maps to the lower left corner of the quadrilateral. / / If the image is mapped, the image is displayed upside down, and the side or nothing is not.

// The first parameter of GLTEXCOORD2F is X coordinate. // 0.0 is the left side of the texture. 0.5 is the midpoint of the texture, 1.0 is the right side of the texture. The second parameter of // GLTEXCOORD2F is Y coordinate. //0.0 is the bottom of the texture. 0.5 is the midpoint of the texture, 1.0 is the top of the texture. // So the top left coordinates of the texture are x: 0.0f, Y: 1.0F, // quad-shaped top vertices are x: -1.0f, y: 1.0F. // The remaining three points are pushed.

// Try to play the X, Y coordinate parameters of Gltexcoord2f. // Replace 1.0 to 0.5 will display only the left half of the texture, // Turn 0.0 to 0.5 will display only the right half of the texture.

Glbegin (GL_QUADS); // Front GLTEXCOORD2F (0.0, 0.0); GlvertEX3F (-1.0, -1.0, 1.0); // Texture and quadrilateral left lower GltexcoRD2F (1.0, 0.0); GlvertEX3F (1.0, -1.0, 1.0); // Texture and quadrilateral upper right gltexcoord2f (1.0, 1.0); GlvertEX3F (1.0, 1.0, 1.0); // Texture and quadrilateral upper right gltexcoord2f (0.0, 1.0); GlvertEX3F (-1.0, 1.0, 1.0); // Texture and quadrilateral left // back GLTEXCOORD2F (1.0, 0.0); GlvertEX3F (-1.0, -1.0, -1.0); // Texture and quadrilateral upper right gltexcoord2f (1.0, 1.0); GlvertEX3F (-1.0, 1.0, - 1.0); // Texture and quadrilateral upper right gltexcoord2f (0.0, 1.0); GlvertEX3F (1.0, 1.0, -1.0); // Texture and quadrilateral left gltexcoord2f (0.0, 0.0); Glvertex3f (1.0, -1.0, -1.0 ); // texture and quadrilateral left // top GLTEXCOORD2F (0.0, 1.0); GlvertEX3F (-1.0, 1.0, -1.0); // Texture and quadrilateral left GltexcoD2F (0.0, 0.0); GlvertEx3f (-1.0, 1.0, 1.0); // Texture and quadrilateral top left gltexcoord2f (1.0, 0.0); GLVERTEX3F (1.0, 1.0, 1.0); // Texture and quadrilateral upper right gltexcoord2f (1.0, 1.0); GlvertEX3F (1.0, 1.0, - 1.0); // Texture and quadrilateral right // bottom GLTEXCOORD2 f (1.0, 1.0); Glvertex3f (-1.0, -1.0, -1.0); // Texture and quadrilateral upper right gltexcoord2f (0.0, 1.0); GlvertEX3F (1.0, -1.0, -1.0); // Texture and quadrilateral Gltexcoord2f (0.0, 0.0); GlvertEX3F (1.0, -1.0, 1.0); // texture and quadrilateral top left gltexcoord2f (1.0, 0.0); Glvertex3f (-1.0, -1.0, 1.0); // Texture and quadrilateral right The lower // right gltexcoord2f (1.0, 0.0); GlvertEX3F (1.0, -1.0, -1.0); // Texture and quadrilateral upper right gltexcoord2f (1.0, 1.0); GlvertEX3F (1.0, 1.0, -1.0); // Texture And quadrilateral upper right gltexcoord2f (0.0, 1.0); Glvertex3f (1.0, 1.0, 1.0); // texture and quadrilateral left GLTEXCOORD2F (0.0, 0.0); GlvertEX3F (1.0, -1.0, 1.0); // Texture and quadrilateral The lower left // left Gltexcoord2f (0.0, 0.0); GlvertEX3F (-1.0, -1.0, -1.0); // Texture and quadrilateral top left GltexcoRD2F (1.0, 0.0);

Glvertex3f (-1.0, -1.0, 1.0); // Texture and quadrilateral upper right gltexco 3f (1.0, 1.0); Glvertex3f (-1.0, 1.0, 1.0); // Texture and quadrilateral upper right gltexcoord2f (0.0, 1.0); GlvertEX3F (-1.0, 1.0, -1.0); // Texture and quadrilateral glend (); xrot: = xrot 0.3; // x axis rotation YROT: = YROT 0.2; // Y axis rotation Zrot: = Zrot 0.4; // Z axis rotation END;

{Finally, I want to be very important about the image used as texture, and you must understand. The width and high of this image must be 2 N times; the width and height must be 64 pixels; and for compatibility, the width and height of the image should not exceed 256 pixels. If the width and height of your original material is not 64, 128, 256 pixels, use the image processing software to re-change the size of the image. It is certain that there is a way to bypass these restrictions, but now we only need to use standard texture sizes. }

// ok! Run a look

转载请注明原文地址:https://www.9cbs.com/read-25039.html

New Post(0)