December 23rd, 2007
OpenGL Super Bible! Page 451 basically because texture mapping on a standard PC is too slow for animation. When the user isn t flying around (or drawing, for that matter), we want to draw with the textures. We will take care of this with a little conditional code and a few lighting parameters. Also, because drawing the textured scene will be slower than the fly-through scene, we need to provide some feedback to the user that our program is doing something. For simplicity, we ll just draw to the front buffer (the visible one) when texturing, and to the back buffer (the invisible one for animation) when flying or drawing. This way, when the program updates the textured scene, the user will see the image being drawn. You ll learn more about buffers in Chapter 15. The RepaintWindow function handles redrawing the terrain for the user. It starts off by selecting the front or back buffer (as described just above). Then it clears the color and depth bits, as follows: glViewport(0, 0, rect->right, rect->bottom); glClearColor(0.5, 0.5, 1.0, 1.0); glEnable(GL_DEPTH_TEST); if (Moving || Drawing) { glDisable(GL_TEXTURE_2D); glDrawBuffer(GL_BACK); } else { glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glDrawBuffer(GL_FRONT); }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); After this, RepaintWindow draws in the sky. For performance reasons, the sky is only drawn when the user is not flying over or drawing the terrain. Since the background is cleared to a light blue, this isn t really a problem. The sky is shaped like a pyramid and has the SKY.BMP texture image mapped to it for a nice, cloudy blue sky. Once the sky is drawn, RepaintWindow starts drawing the terrain. The algorithm used is quite simple and basically generates strips of quadrilaterals (squares) along the terrain points. Each strip uses a different texture or lighting material color, so we have to issue glBegin/glEnd calls for each one. See Figure 12-6 for a graphical depiction of the algorithm.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.
Posted in Coldfusion | No Comments »
December 22nd, 2007
Page 450 OpenGL Super Bible! /* * Set the height of the terrain ‘cell . For water, the * height is constant at WATER_HEIGHT. Other other types, * we add a random pertubation to make the terrain more * interesting/realistic. */ switch (TerrainCurrent) { case IDC_WATER : TerrainHeight[y][x] = WATER_HEIGHT; break; case IDC_GRASS : TerrainHeight[y][x] = GRASS_HEIGHT + 0.1 * (rand() % 5); break; case IDC_TREES : TerrainHeight[y][x] = TREES_HEIGHT + 0.1 * (rand() % 5); break; case IDC_ROCKS : TerrainHeight[y][x] = ROCKS_HEIGHT + 0.1 * (rand() % 5); break; case IDC_MOUNTAINS : TerrainHeight[y][x] = MOUNTAINS_HEIGHT + 0.15 * (rand() % 5); break; }; } For the IDC_WATER terrain type, the point height is just set to WATER_HEIGHT (0.0). For other types, we add a small amount of random jitter to make the terrain look more realistic. Once the selected cell is drawn, we recompute the lighting normals using the new height values in UpdateNormals. Each lighting normal is calculated using the points above and to the right of the current point with the following formula: N = lighting normal H = height of current point Hu = height of point above Hr = height of point to the right Nx = (Hr - H) / |N| Ny = 1 / |N| Nz = (Hu - H) / |N| This is just a simplification of the cross product of adjacent terrain grid- cells. Once all the normals are recalculated, the scene is redrawn. Drawing the Scene Now that we ve taken care of the drudge work, we can concentrate on displaying the terrain. You ll remember that besides displaying a pretty textured image, we also want to fly through this terrain. To accomplish this, we need to draw the terrain without textures
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.
Posted in Coldfusion | No Comments »
December 22nd, 2007
OpenGL Super Bible! Page 449 The heart of the drawing interface is in the DrawTerrain function. It uses the OpenGL selection mechanism to determine which terrain points are under the mouse pointer. Instead of drawing the terrain to the screen, selection rendering records hits inside the selection area (in this case, the mouse pointer) to a buffer you provide. In DrawTerrain, we record the (x,y) location of the terrain in the selection buffer, as in a paint-by-numbers book (see Figure 12-5). OpenGL selection is covered in more detail in Chapter 19. Figure 12-5 Picking a terrain cell Once we have the (x,y) terrain locations, we then reset the height and type of these points in the draw_cell function (Listing 12-4). Listing 12-4 The draw_cell function void draw_cell(int x, /* I - Terrain X location */ int y) /* I - Terrain Y location */ { /* * Range check the terrain location */ if (x < 0 || x >= TERRAIN_SIZE || y < 0 || y >= TERRAIN_SIZE) return; if (TerrainType[y][x] == TerrainCurrent) return; /* Already the right type */ TerrainType[y][x] = TerrainCurrent; /* * Force a redraw */ InvalidateRect(SceneWindow, NULL, TRUE);
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.
Posted in Coldfusion | No Comments »
December 21st, 2007
Page 448 OpenGL Super Bible! Defining the Terrain To keep things simple, we ll define our terrain as a grid of elevation points with a texture attribute such as this is water or this is a mountain. Each point in the grid will also have an associated lighting normal to add realism. #define TERRAIN_SIZE 21 int TerrainType[TERRAIN_SIZE][TERRAIN_SIZE]; GLfloat TerrainHeight[TERRAIN_SIZE][TERRAIN_SIZE]; GLfloat TerrainNormal[TERRAIN_SIZE][TERRAIN_SIZE][3]; Here the TerrainType array contains the type of terrain at each point and is assigned one of the following control IDs from our user-interface resource file: IDC_GRASS Grasslands IDC_WATER Water IDC_TREES Trees/woodland IDC_ROCKS Rocks/cliffs IDC_MOUNTAINS Mountains Drawing Terrain Our terrain drawing controls consist of a toolbar dialog window with five buttons that select the current type of terrain. To draw the terrain, you just click and drag in the main window (see Figure 12-4). Figure 12-4 Textured terrain editing window
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.
Posted in Coldfusion | No Comments »
December 20th, 2007
OpenGL Super Bible! Page 447 To make your life a bit easier, the OpenGL utility library (GLU32.LIB) provides two functions that automatically generate mipmapped images based on a single, high-resolution texture. In the following code, the gluBuild1DMipmaps and gluBuild2DMipmaps functions take the place of glTexImage1D and glTexImage2D: /* 1D texture */ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); gluBuild1DMipmaps(GL_TEXTURE_1D, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image); /* 2D texture */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, info->bmiHeader.biWidth, info- >bmiHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb); Because the gluBuild1DMipmaps and gluBuild2DMipmaps functions create images from one image, the appearance of some textured images may not be accurate. It s like drawing text characters at different sizes scaling the bitmaps doesn t always generate good-looking results! When you run into this sort of problem, generate your mipmap images manually. A Terrain Viewing Program Our project for this chapter is a terrain viewing program that takes advantage of some of the texture-mapping features we have discussed. With this program, we ll want to accomplish the following: View textured terrain scenes Edit the terrain interactively in 3D Fly through the terrain Print the current scene Save the current scene to a .BMP file The entire terrain program is listed at the end of this chapter, just before the Reference Section. A copy of the program is in the CH12 source directory on your CD-ROM. Double- click on the TEXSCENE.EXE program icon to try it out!
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.
Posted in Coldfusion | No Comments »
December 19th, 2007
Page 446 OpenGL Super Bible! glTexImage1D(GL_TEXTURE_1D, 0, 3, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image0); glTexImage1D(GL_TEXTURE_1D, 1, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image1); glTexImage1D(GL_TEXTURE_1D, 2, 3, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image2); glTexImage1D(GL_TEXTURE_1D, 3, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image3); glTexImage1D(GL_TEXTURE_1D, 4, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv_image4); glEndList(); The image levels are specified in the first parameter to glTexImage1D(). The level 0 image is your primary, highest-resolution image for the texture. The level 1 image is half the size of the primary image, and so forth. When drawing polygons with a mipmapped texture, you need to use one of the minification filters (GL_TEXTURE_MIN_FILTER) in Table 12-3. Table 12-3 Minification Filters Filter Description GL_NEAREST_MIPMAP_NEAREST Use the image nearest to the screen (polygon) resolution. Use the GL_NEAREST filter when texturing with this image. GL_NEAREST_MIPMAP_LINEAR Use the image nearest to the screen (polygon) resolution. Use the GL_LINEAR filter when texturing with this image. GL_LINEAR_MIPMAP_NEAREST Linearly interpolate between the two images nearest to the screen (polygon) resolution. Use the GL_NEAREST filter when texturing with this image. GL_LINEAR_MIPMAP_LINEAR Linearly interpolate between the two images nearest to the screen (polygon) resolution. Use the GL_LINEAR filter when texturing with this image. The GL_LINEAR_MIPMAP_NEAREST and GL_LINEAR_MIPMAP_LINEAR filters can be very expensive in terms of display performance. GL_NEAREST_MIPMAP_NEAREST is roughly equivalent to GL_NEAREST in performance, but generally produces much better results. Mipmap images are chosen by comparing the size of the polygon as it will be drawn on the screen, to the sizes of the mipmap images.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.
Posted in Coldfusion | No Comments »
December 18th, 2007
OpenGL Super Bible! Page 445 If you use GL_REPEAT instead, the texture image is tiled over the polygon. Texture coordinates are used modulo 1.0 that is, the texture image repeats at regular intervals. GL_REPEAT texturing can be used to reduce the size of texture images on repetitive surfaces. The challenge with these kinds of textures is to make the edges of each tile blend into the next. Automatically Generating Texture Coordinates: Generating texture coordinates can be a tedious task. Later in this chapter you ll learn about the glTexGen functions that can generate these coordinates automatically for you. Mipmapped Textures So far, we ve dealt exclusively with single-texture images. That is, whenever we draw a textured polygon, the polygon is painted with a single 1D or 2D image. This is fine for some scenes, but animated displays often need various levels of detail depending on the distance from the viewer. For example, when walking through a virtual room, you might want a high- resolution image of a picture close up, but only the outline at a distance. OpenGL supports textures with multiple images, called mipmapped textures. Mipmapping selects the texture image closest to the screen resolution for a polygon. Loading mipmapped textures takes slightly longer than standard textures, but the visual results are impressive. In addition, mipmapped textures can improve display performance by reducing the need for GL_LINEAR image filters. What Does the ‘Mip in ‘Mipmapped Mean?: ‘mip is latin for ‘many . ‘Mipmapping means ‘many images . Mipmapped textures are defined by providing a specific level parameter for each image. For the ROY-G-BIV texture in the previous example, you would use the following: static unsigned char roygbiv_image0[16][3]; static unsigned char roygbiv_image1[8][3]; static unsigned char roygbiv_image2[4][3]; static unsigned char roygbiv_image3[2][3]; static unsigned char roygbiv_image4[1][3]; glNewList(RainbowTexture = glGenLists(1), GL_COMPILE); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
Posted in Coldfusion | No Comments »
December 17th, 2007
Page 444 OpenGL Super Bible! Listing 12-3 Drawing a 1D textured rainbow glEnable(GL_TEXTURE_1D); glCallList(RainbowTexture); glBegin(GL_QUAD_STRIP); for (th = 0.0; th <= M_PI; th += (0.03125 * M_PI)) { /* * Bottom edge of rainbow */ x = cos(th) * 50.0; y= sin(th) * 50.0; z = -50.0; glTexCoord1f(0.0); glVertex3f(x, y, z); /* * Top edge of rainbow */ x = cos(th) * 55.0; y = sin(th) * 55.0; z = -50.0; glTexCoord1f(1.0); glVertex3f(x, y, z); }; glEnd(); To position the ROY-G-BIV texture on the rainbow, you call glTexCoord. For 1D textures, you call one of the glTexCoord1f, glTexCoord1d, glTexCoord1s, or glTexCoord1i functions. A value of 0.0 represents the leftmost pixel in the image, and 1.0 represents the rightmost pixel. Values outside this range are handled differently depending on the value of the GL_TEXTURE_WRAP_S parameter. If GL_TEXTURE_WRAP_S is set to GL_CLAMP (the default), then texture coordinates are restricted to a range of 0.0 to 1.0, inclusive. When a polygon strays from the texture image, it is drawn using the color(s) along the texture image s edges (see Figure 12-3) or the texture image border colors, if defined. Texture coordinates are traditionally referred to as S and T, or (s,t) instead of X and Y. Figure 12-3 GL_CLAMP textures
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.
Posted in Coldfusion | No Comments »
December 16th, 2007
OpenGL Super Bible! Page 443 Drawing Textured Polygons Once you have defined a texture, you still have to enable texturing. To enable 1D texturing, you d use the following: glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_1D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); The glEnable call enables 1D texturing. If you forget to enable texturing, none of your polygons will be textured! The glTexEnvi function sets texturing to decal mode, meaning that images are overlaid directly upon the polygons. Other texturing modes are listed in Table 12-2. Table 12-2 Texture Modes for GL_TEXTURE_ENV_MODE Mode Description GL_MODULATE Texture pixels filter existing pixel colors on the screen. GL_DECAL Texture pixels replace existing pixels on the screen. GL_BLEND Texture pixels filter existing pixels colors and are combined with a constant color. The GL_MODULATE texture mode multiplies the current texture color (or luminance) by the color on the screen. For one-component (luminance) textures, this translates into a brightness filter that will vary the brightness of the screen image based upon the texture image. For three-component (RGB) textures, you can generate colored lens filter effects. Unlike GL_MODULATE texturing, GL_BLEND texturing allows you to blend a constant color into the scene based upon the texture image. You d use GL_BLEND texturing for things like clouds; the constant color would be off-white, and the texture image would be of a cloud. Once you have defined the texturing mode to use, you can then proceed with the drawing of your polygons. Listing 12-3 shows how to draw the rainbow in Figure 12-1.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.
Posted in Coldfusion | No Comments »
December 15th, 2007
Page 442 OpenGL Super Bible! bits = LoadDIBitmap(’textures/sky.bmp , &info); if (bits == NULL) return; rgb = ConvertRGB(info, bits); if (rgb == NULL) { free(info); free(bits); return; }; glNewList(SkyTexture = glGenLists(1), GL_COMPILE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINE AR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /* * Define the 2D texture image. */ glPixelStorei(GL_UNPACK_ALIGNMENT, 4); /* Force 4-byte alignment */ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glTexImage2D(GL_TEXTURE_2D, 0, 3, info->bmiHeader.biWidth, info- >bmiHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb); glEndList(); /* * Free the bitmap and RGB images, then return 0 (no errors). */ free(rgb); free(info); free(bits); } A Note About Textures: You ll notice that all the examples presented in this chapter use display lists to store texture images. Display lists generally speed up the drawing of static graphics commands, and texture images are no exception. In addition, the forthcoming OpenGL 1.1 API includes texture object support that optimizes texture images stored in display lists by keeping them loaded in the graphics hardware texture memory if available.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.
Posted in Coldfusion | No Comments »