Creating a 3D World in OpenGL - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Creating a 3D World in OpenGL

Description:

Adding Detail with Textures ... Example of Changing the Texture. Now we're going to change the wall texture from the left bitmap to the right bitmap. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: CSU9151
Category:

less

Transcript and Presenter's Notes

Title: Creating a 3D World in OpenGL


1
Creating a 3D World in OpenGL
  • NeHe Lesson 10
  • Brian Stephens, Manar Deeb, and Ron Burnette
  • COSC 471 Computer Graphics
  • Jeff Schmitt
  • 05/08/07

2
Abstract
  • Our goal is to show how to create an interactive
    3D world using OpenGL and C. We will cover how
    the world is created and drawn, how to give it
    texture, and how the user can move around the
    world.

3
Outline
  • Getting Started
  • Data Structures
  • Loading the World
  • Adding Detail through Textures
  • Moving in the World
  • Bringing it Together
  • Running the Program
  • Example of Changing the Texture
  • Conclusion
  • Questions

4
Getting Started
Getting Started
  • Creating the 3D world requires 4 main elements
  • OpenGL
  • World File
  • Texture File
  • C Code

5
Data Structures
  • Vertex
  • typedef struct tagVERTEX // Build Our Vertex
    Structure
  • float x, y, z // 3D Coordinates
  • float u, v // Texture
    Coordinates
  • VERTEX // Call It VERTEX
  • Triangle
  • typedef struct tagTRIANGLE // Build Our
    Triangle Structure
  • VERTEX vertex3 // Array of 3
    Vertices
  • TRIANGLE // Call It
    TRIANGLE
  • Sector
  • typedef struct tagSECTOR // Build Our Sector
    Structure
  • int numtriangles // Number Of
    Triangles In Sector
  • TRIANGLE triangle // Pointer To
    Array Of Triangles
  • SECTOR // Call It SECTOR

6
Loading the World
Loading the World
  • Data is stored in a text file to allow for the
    flexibility to easily change worlds
  • First line contains the number of triangles in
    the sector
  • NUMPOLIES 36
  • Triangle coordinates are stored as
  • X1 Y1 Z1 U1 V1 Example -3.0 0.0 -3.0 0.0
    6.0
  • X2 Y2 Z2 U2 V2 -3.0 0.0
    3.0 0.0 0.0
  • X3 Y3 Z3 U3 V3 3.0 0.0
    3.0 6.0 0.0
  • Each triangle is loaded into the SECTOR
    structure
  • sector1.triangletriloop.vertexvertloop.x
    x sector1.triangletriloop.vertexvertloop.y
    y sector1.triangletriloop.vertexvertloop.z
    z sector1.triangletriloop.vertexvertloop.u
    u sector1.triangletriloop.vertexvertloop.v
    v

7
Adding Detail with Textures
  • At the first, we load the bitmap image from the
    file and make sure that the file exists.

8
Load Bitmap and convert to Textures
  • We create a status indicator and a storage space
    for the texture

9
If texture Exists
  • We will free the texture image memory.
  • Free the Image structure.
  • Save the status.

10
Moving in the World
  • Rotate the world according to the user commands.
  • Rotate the world around the origin in the
    opposite direction of the camera ( gives the
    illusion that the camera has been rotated)
  • Translate the world in the opposite manner that
    the camera has been translated

11
Rotation of the Camera
  • If we pressed right on the keyboard, the rotation
    variable yrot is decreased 1.0.
  • If we pressed left on the keyboard , the rotation
    variable yrot is increased 1.0.

12
Translation of the Camera
  • When forward or backward button pressed, a new
    location for the camera is calculated using sin()
    and cos()

13

The First Scene and Each Triangle
14
Bringing it Together
  • Without GLUT, we must make our own window
    creation function using the WIN32 API.
  • BOOL CreateGLWindow(char title, int width, int
    height, int bits, bool fullscreenflag)
  • if (!InitGL()) // Initialize Our Newly Created
    GL Window
  • KillGLWindow() // Reset The Display
  • MessageBox(NULL,"Initialization
    Failed.","ERROR",MB_OKMB_ICONEXCLAMATION)
  • return FALSE // Return FALSE
  • return True //Success

15
Bringing it Together Cont.
  • Next we create a main function that will hold an
    instance of our application.
  • int WINAPI WinMain(HINSTANCE hInstance //
    Instance
  • HINSTANCE hPrevInstance, // Previous Instance
  • LPSTR lpCmdLine, // Command Line Parameters
  • int nCmdShow) // Window Show State
  • // Ask The User Which Screen Mode They Prefer
  • if (MessageBox(NULL,"Would You Like To Run In
    Fullscreen Mode?", "Start FullScreen?",MB_YESNOMB
    _ICONQUESTION)IDNO)
  • fullscreenFALSE // Windowed Mode

16
Bringing it Together Cont.
  • Create the window and draw the scene.
  • // Create Our OpenGL Window
  • if (!CreateGLWindow("Lionel Brits NeHe's 3D
    World Tutorial",640,480,16,fullscreen))
  • return 0 // Quit If Window Was Not Created
  • while(!done) // Loop That Runs While doneFALSE
  • // Draw The Scene. Watch For ESC Key And Quit
    Messages From DrawGLScene()
  • if ((active !DrawGLScene())
    keysVK_ESCAPE // Active? Was There A Quit
    Received?
  • doneTRUE // ESC or DrawGLScene Signalled A
    Quit
  • else // Not Time To Quit, Update Screen
  • //Add Keyboard Functionality

17
Running the Program
18
Example of Changing the Texture
  • Now were going to change the wall texture from
    the left bitmap to the right bitmap.

19
Conclusion
  • We have shown how to read coordinates into our
    program and store them in a data structure called
    a Sector.
  • Using various functions (user created, OpenGL,
    and WIN32) we were able to add texture to our
    world, create a window to show it, and add
    interactivity.
  • For more information, please visit
  • http//nehe.gamedev.net

20
Questions?
Write a Comment
User Comments (0)
About PowerShow.com