OpenGL 3D Objects - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

OpenGL 3D Objects

Description:

In the tutorials so far you have been creating 2D polygons (triangles and rectangles) ... Half-Life 2 Pre-Textured Level. Texturing ... – PowerPoint PPT presentation

Number of Views:222
Avg rating:3.0/5.0
Slides: 19
Provided by: yvancar
Category:
Tags: opengl | half | life | objects

less

Transcript and Presenter's Notes

Title: OpenGL 3D Objects


1
OpenGL 3D Objects Texturing
  • Yvan Cartwright
  • y.j.f.cartwright_at_staffs.ac.uk

2
Creating 3D objects
  • In the tutorials so far you have been creating 2D
    polygons (triangles and rectangles).
  • These polygons have then been added to a drawing
    context (an OpenGL-aware window).
  • All 3D objects are created by stitching multiple
    polygons together.
  • Sounds simple?
  • Lets look at creating a pyramid out of our
    triangle.

3
Making the Pyramid
  • Things to remember
  • We are using the GL_TRIANGLES OpenGL primitive.
  • This primitive allows us to specify a series of
    vertices.
  • For every three vertices we include in the list,
    a new triangle will be formed.
  • We may want to do extra things with the pyramid
    once it is created (e.g. rotation).
  • The most useful way to do this is to ensure that
    when we specify the coordinates of each vertex,
    the centre of the pyramid is at (0,0,0).

4
Front Face
  • glBegin(GL_TRIANGLES) // Start Drawing The
    Pyramid
  • glColor3f(1.0f,0.0f,0.0f) // Red
  • glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of
    Triangle (Front)
  • glColor3f(0.0f,1.0f,0.0f) // Green
  • glVertex3f(-1.0f,-1.0f, 1.0f) // Left Of
    Triangle (Front)
  • glColor3f(0.0f,0.0f,1.0f) // Blue
  • glVertex3f( 1.0f,-1.0f, 1.0f) // Right Of
    Triangle (Front)
  • Notice that the top vertex is one unit above the
    origin and the bottom two vertices are one unit
    below.
  • Also, the bottom-left vertex is one unit left of
    the origin and the bottom-right vertex is one
    unit to the right of the origin.
  • Both are one unit closer to the viewpoint.
  • Finally, we have put the points in the list in a
    counter-clockwise direction. More on this in
    another lecture.

5
Additional Faces
  • The other faces are added in a similar way.
  • If you look carefully at the commands to draw the
    right face, youll notice that the colour used
    for the top vertex is the same as for the front
    face (red).
  • glColor3f(1.0f,0.0f,0.0f) // Red
  • glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of
    Triangle (Right)
  • glColor3f(0.0f,0.0f,1.0f) // Blue
  • glVertex3f( 1.0f,-1.0f, 1.0f) // Left Of
    Triangle (Right)
  • glColor3f(0.0f,1.0f,0.0f) // Green
  • glVertex3f( 1.0f,-1.0f, -1.0f) // Right Of
    Triangle (Right)
  • All other colours are chosen to match their
    counterpart vertices.
  • This is to ensure consistent colouring of all
    vertices on each face.

6
The Finished Pyramid
  • We havent drawn the bottom face (floor) of the
    pyramid because are viewpoint is fixed so it
    cannot be seen.
  • If we need to add this, we could use a GL_QUAD.
  • We have defined the pyramid to have its centre
    at (0,0,0).
  • This was done to allow us to rotate the 3D object
    more easily.
  • We can add a simple command before the call to
    GL_TRIANGLES to rotate the object by a small
    amount.
  • glRotatef(rtri,0.0f,1.0f,0.0f) // Rotate The
    Pyramid On It's Y Axis
  • The value of the float variable rtri can be
    increased each time through the display loop
    which will cause the pyramid to rotate
    continuously.

7
The Finished Pyramid
8
3D Objects in Games
  • Clearly, it is a time-consuming (and error-prone)
    task to create a complex object (plane, human
    figure etc.) from a list of vertices.
  • The 3D objects used in games are created by
    digital artists.
  • The tools they use are object modellers e.g. 3D
    Studio Max.
  • A model is created by drawing a series of plan
    views.
  • The model is tweaked and a basic flat coloured
    model is issued as a resource to the programmers.

9
Half-Life 2 Pre-Textured Level
10
Texturing
  • The programmers use an import utility in their
    codes to load the objects.
  • A flat colour is used to provide a standard
    reference for seeing how lighting for the scene
    affects the objects.
  • Texture maps are then created which can be
    applied to the objects.
  • Before the graphics hardware was particularly
    capable, there would just be a single texture
    applied to the object primitives.
  • Now, multiple textures can be blended together
    for a more realistic effect (particularly true
    for landscapes).
  • This takes time but when ready, the
    texture-mapped version replaces the flat coloured
    one in the game.

11
Texture Mapping
  • Texture mapping means applying a graphical image
    (usually 2D) onto a graphical primitive.
  • This is done because real objects have a lot of
    detail.
  • Adding this detail using additional primitives
    can massively increase rendering times for your
    scenes.
  • This extra detail can be more easily generated by
    using a bitmapped image.

12
Simple Example
13
Texturing Basics
  • So, if we have a suitable bitmap, how do we apply
    it to our 3D objects?
  • We have to map the vertex coordinates of our
    primitives to the coordinates of our image.
  • The coordinates of our image are known as texels
    (texture elements think of pixels).
  • Every image must be a square (usually) and the
    texels range from 0-1 in the (u,v) coordinate
    system.
  • Once we have specified these boundary points, the
    rest of the primitive will be filled with the
    image.
  • There is a bit more to this process to get a good
    effect but this is the basic principle.

14
Texel Coordinates
15
Texel Coordinates
16
Texturing in OpenGL
  • To texture map an object using OpenGL, we first
    have to load in an image (obviously, real games
    use many textures for a whole scene).
  • To do this we must have the image as a file
    (common formats are BMP, PNG, JPEG etc.).
  • We then use a utility method to load the texture
    into memory and give it a name.
  • We turn the texture mapping capabilities of
    OpenGL on.
  • We can then specify a texel coordinate for each
    vertex in the primitive we are drawing.

17
Texturing in OpenGL
  • gl.glBindTexture(GL.GL_TEXTURE_2D, texture)
  • gl.glBegin(GL.GL_QUADS) // Front Face
  • gl.glTexCoord2f(0.0f, 0.0f)
  • gl.glVertex3f(-1.0f, -1.0f, 1.0f)
  • gl.glTexCoord2f(1.0f, 0.0f)
  • gl.glVertex3f( 1.0f, -1.0f, 1.0f)
  • gl.glTexCoord2f(1.0f, 1.0f)
  • gl.glVertex3f( 1.0f, 1.0f, 1.0f)
  • gl.glTexCoord2f(0.0f, 1.0f)
  • gl.glVertex3f(-1.0f, 1.0f, 1.0f)

18
Textured Crate
Write a Comment
User Comments (0)
About PowerShow.com