Microsoft DirectX 8.1 (Visual Basic) |
Before rendering, the Texture sample project calls an application-defined function, InitGeometry, to create a texture and set up the geometry for a cylinder.
Textures are created from file-based images. The following code fragment uses D3DX8.CreateTextureFromFile to create a texture from Banana.bmp that will be used to cover the surface of the cylinder.
Set g_Texture = g_D3DX.CreateTextureFromFile(g_D3DDevice, App.Path + "\banana.bmp") If g_Texture Is Nothing Then Exit Function
The first parameter that CreateTextureFromFile accepts is the Microsoft® Direct3D® device that will be used to render the texture. The second parameter is a string that specifies the filename from which to create the texture. This sample specifies Banana.bmp to load the image from that file.
The banana texture is loaded and ready to use. The next step is to create the cylinder. The following code fills the vertex buffer with a cylinder. Note that each point has the texture coordinate (tu, tv).
For i = 0 To 49 theta = (2 * g_pi * i) / (50 - 1) Vertices(2 * i + 0).position = vec3(Sin(theta), -1, Cos(theta)) Vertices(2 * i + 0).color = &HFFFFFFFF 'White. Vertices(2 * i + 0).tu = i / (50 - 1) Vertices(2 * i + 0).tv = 1 Vertices(2 * i + 1).position = vec3(Sin(theta), 1, Cos(theta)) Vertices(2 * i + 1).color = &HFF808080 'Grey. Vertices(2 * i + 1).tu = i / (50 - 1) Vertices(2 * i + 1).tv = 0 Next
Each vertex includes a position, color, and texture coordinates. The Texture sample project sets the texture coordinates for each point so that the texture will wrap smoothly around the cylinder.
Now that the texture is loaded and the vertex buffer is ready for rendering, it is time to render the display, as described in Step 3: Rendering the Scene.