Выбрать главу

* DOWNLOAD Visual C++ Code For This Lesson. 

Lesson 38

Welcome to the 38th NeHe Productions Tutorial. It's been awhile since my last tutorial, so my writing may be a little rusty. That and the fact that I've been up for almost 24 hours working on the code :)

So you know how to texture map a quad, and you know how to load bitmap images, tga's, etc. So how the heck do you texture map a Triangle? And what if you want to hide your textures in the .EXE file?

The two questions I'm asked on a daily basis will soon be answered, and once you see how easy it is, you'll wonder why you never thought of the solution :)

Rather than trying to explain everything in great detail I'm going to include a few screenshots, so you know exactly what it is I'm talking about. I will be using the latest basecode. You can download the code from the main page under the heading "NeHeGL I Basecode" or you can download the code at the end of this tutorial.

The first thing we need to do is add the images to the resource file. Alot of you have already figured out how to do this, unfortunately, you miss a few steps along the way and end up with a useless resource file filled with bitmaps that you can't use.

Remember, this tutorial was written in Visual C++ 6.0. If you're using anything other than Visual C++, the resource portion of this tutorial wont make sense (especially the screenshots).

* Currently you can only use 24 bit BMP images. There is alot of extra code to load 8 bit BMP files. I'd love to hear from anyone that has a tiny / optimized BMP loader. The code I have right now to load 8 and 24 bit BMP's is a mess. Something that uses LoadImage would be nice.

Open the project and click INSERT on the main menu. Once the INSERT menu has opened, select RESOURCE.

You are now asked what type of resource you wish to import. Select BITMAP and click the IMPORT button.

A file selection box will open. Browse to the DATA directory, and highlight all three images (Hold down the CTRL key while selecting each image). Once you have all three selected click the IMPORT button. If You do not see the bitmap files, make sure FILES OF TYPE at the bottom says ALL FILE (*.*).

A warning will pop up three times (once for each image you imported). All it's telling you is that the image was imported fine, but the picture can't be viewed or edited because it has more than 256 colors. Nothing to worry about!

Once all three images have been imported, a list will be displayed. Each bitmap has been assigned an ID. Each ID starts with IDB_BITMAP and then a number from 1–3. If you were lazy, you could leave the ID's and jump to the code. Lucky we're not lazy!

Right click each ID, and select PROPERTIES. Rename each ID so that it matches the name of the original bitmap file. See the picture if you're not sure what I mean.

Once you are done, select FILE from the main menu and then SAVE ALL because you have just created a new resource file, windows will ask you what you want to call the file. You can save the file with the default filename or you can rename it to lesson38.rc. Once you have decided on a name click SAVE.

This is the point that most people make it to. You have a resource file. It's full of Bitmap images and it's been saved to the Hard Drive. To use the images, you need to complete a few more steps. The next thing you need to do is add the resource file to your current project. Select PROJECT from the main menu, ADD TO PROJECT, and then FILES. Select the resource.h file, and the resource file (Lesson38.rc). Hold down control to select more than one file, or add each file individually. The last thing to do is make sure the resource file (Lesson38.rc) was put in the RESOURCE FILES folder. As you can see in the picture above, it was put in the SOURCE FILES folder. Click it with your mouse and drag it down to the RESOURCE FILES folder.

Once the resource file has been moved select FILE from the main menu and SAVE ALL. The hard part has been done! …Way too many pictures :)

So now we start on the code! The most important line in the section of code below is #include "resource.h". Without this line, you will get a bunch of undeclared identifier errors when you try to compile the code. The resource.h file declares the objects inside the resource file. So if you want to grab data from IDB_BUTTERFLY1 you had better remember to include the header file!

#include <windows.h> // Header File For Windows

#include <gl\gl.h> // Header File For The OpenGL32 Library

#include <gl\glu.h> // Header File For The GLu32 Library

#include <gl\glaux.h> // Header File For The GLaux Library #include "NeHeGL.h" // Header File For NeHeGL

#include "resource.h" // Header File For Resource (*IMPORTANT*)

#pragma comment( lib, "opengl32.lib" ) // Search For OpenGL32.lib While Linking

#pragma comment( lib, "glu32.lib" ) // Search For GLu32.lib While Linking

#pragma comment( lib, "glaux.lib" ) // Search For GLaux.lib While Linking

#ifndef CDS_FULLSCREEN // CDS_FULLSCREEN Is Not Defined By Some

#define CDS_FULLSCREEN 4 // Compilers. By Defining It This Way,

#endif // We Can Avoid Errors

GL_Window* g_window; Keys* g_keys;

The first line below sets aside space for the three textures we're going to make.

The structure will be used to hold information about 50 different objects that we'll have moving around the screen.

tex will keep track of which texture to use for the object. x is the xposition of the object, y is the y position of the object, z is the objects position on the z-axis, yi will be a random number used to control how fast the object falls. spinz will be used to rotate the object on it's z-axis as it falls, spinzi is another random number used to control how fast the object spins. flap will be used to control the objects wings (more on this later) and fi is a random value that controls how fast the wings flap.

We create 50 instances of obj[ ] based on the object structure.

// User Defined Variables GLuint texture[3]; // Storage For 3 Textures

struct object // Create A Structure Called Object

{

 int tex; // Integer Used To Select Our Texture

 float x; // X Position

 float y; // Y Position

 float z; // Z Position

 float yi; // Y Increase Speed (Fall Speed)

 float spinz; // Z Axis Spin

 float spinzi; // Z Axis Spin Speed

 float flap; // Flapping Triangles :)

 float fi; // Flap Direction (Increase Value)

};

object obj[50]; // Create 50 Objects Using The Object Structure

The bit of code below assigns random startup values to object (obj[ ]) loop. loop can be any value from 0–49 (any one of the 50 objects).

We start off with a random texture from 0 to 2. This will select a random colored butterfly.

We assign a random x position from –17.0f to +17.0f. The starting y position will be 18.0f, which will put the object just above the screen so we can't see it right off the start.

The z position is also a random value from –10.0f to –40.0f. The spinzi value is a random value from –1.0f to 1.0f. flap is set to 0.0f (which will be the center position for the wings).