INDE 599 Lecture - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

INDE 599 Lecture

Description:

Add #include 'DialogGLDlg.h' to your view.cpp file. ... In the view.cpp constructor, initialize the CDialogGLDlg class with the line ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 19
Provided by: jeffbe1
Category:
Tags: inde | lecture | the | view

less

Transcript and Presenter's Notes

Title: INDE 599 Lecture


1
INDE 599 - Lecture 8
  • Jeffrey J. Berkley, PhD
  • Office 209 Sieg Hall
  • Office Hours 330-420PM Tuesday Thursday
  • e-mail jberkley_at_u.washington.edu

2
Last Time
  • What OpenGL is
  • Initializing an OpenGL window.
  • Drawing points, lines and polygons.
  • Color
  • Display Lists

3
Adding the Graphics Window
  • Under the project menu, choose Add To Project and
    the Components and Controls.
  • Choose DialogGLDlg.ogx and press OK.
  • You should now see that a dialog called
    IDD_DIALOGGL_DIALOG has been added to your
    project.
  • You should also see a class called CDialogGLDlg.

4
Using the Graphics Window as a Modeless Dialog
  • Add include "OpenGLExampleView.h (or whatever
    your view file is named) to the DialogGLDlg.cpp
    file.
  • Add include DialogGLDlg.h to your view.cpp
    file.
  • Add a pointer to this new class in the view.h
    file as a public attribute (add CDialogGLDlg
    m_pOpenGLDlg) and forward declare CDialogGLDlg.
  • In the view.cpp constructor, initialize the
    CDialogGLDlg class with the line m_pOpenGLDlgnew
    CDialogGLDlg(this)

5
Using the Graphics Window as a Modeless Dialog
  • In the view.cpp destructor, delete the
    CDialogGLDlg class with the line delete
    m_pOpenGLDlg
  • Create a button or menu item to activate the
    window and map a function to it.
  • In that function add the code
  • if(m_pOpenGLDlg-GetSafeHwnd()0)
  • m_pOpenGLDlg-Create()
  • else m_pOpenGLDlg-DestroyWindow()

6
Lighting Properties
  • Set your lighting properties with commands such
    as these
  • float fAmbientLight 0.7f, 0.7f, 0.7f,
    1.0f
  • float fDiffuseLight 0.8f, 0.8f, 0.8f,
    1.0f
  • float fSpecularLight 1.0f, 1.0f, 1.0f,
    1.0f
  • glLightfv( GL_LIGHT0, GL_AMBIENT,
    fAmbientLight)
  • glLightfv( GL_LIGHT0, GL_DIFFUSE,
    fDiffuseLight)
  • glLightfv( GL_LIGHT0, GL_SPECULAR,
    fSpecularLight)

7
Material Properties for Lighting
  • While not essential, you can also vary material
    properties for each object by declaring different
    properties in the code before rendering each
    object
  • float fAmbientMat 0.7f, 0.7f, 0.7f,
    1.0f
  • float fDiffuseMat 0.8f, 0.8f, 0.8f,
    1.0f
  • float fSpecularMat 1.0f, 1.0f, 1.0f,
    1.0f
  • glMaterialfv(GL_FRONT, GL_AMBIENT, fAmbientMat)
  • glMaterialfv(GL_FRONT, GL_DIFFUSE, fDiffuseMat)
  • glMaterialfv(GL_FRONT, GL_SPECULAR,
    fSpecularMat)

8
Setting the Position of Your Light
  • The position can be set with code like the
    following
  • float fPosition1.0,1.0,0.0,0.0
  • glLightfv(GL_LIGHT0, GL_POSITION, fPosition)

9
Activate Your Light
  • Now a few commands to enable the light
  • glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0)
  • glPolygonMode(GL_FRONT_AND_BACK,GL_FILL)
  • glEnable(GL_LIGHT0)
  • glEnable(GL_LIGHTING)
  • Dont forget to set the normal when rendering
    each of your polygons!

10
Texture Maps
  • Texture maps allow you to add pictures to your
    polygons. This can be MUCH more efficient than
    trying to define individual pixels on a polygon.
  • The hardest part of adding a texture map is
    loading it, so Ill give you code for this.

11
Texture Maps
tx1, ty1
  • In a nut shell, you pull apiece out of a picture
    andpaste it to a polygon.
  • Here is some example code
  • glEnable( GL_TEXTURE_2D )
  • glColor3f(1.0,1.0,1.0)
  • glBegin(GL_TRIANGLES)
  • glNormal3f(nx1,ny1,nz1)
  • glTexCoord2f(tx1,ty1)
  • glVertex3f(x1,y1,z1)
  • glNormal3f(nx2,ny2,nz2)
  • glTexCoord2f(tx2,ty2)
  • glVertex3f(x2,y2,z2)
  • glNormal3f(nx3,ny3,nz3)
  • glTexCoord2f(tx3,ty3)
  • glVertex3f(x3,y3,z3)
  • glEnd()

tx2, ty2
tx3, ty3
12
The Long Way of Using Textures
  • 1) Turn on texture mapping by saying
    glEnable(GL_TEXTURE_2D) This is usually done in
    the beginning of the program. BUT, you will want
    to turn it back off before you draw primatives
    using color instead. So, say glDisable(GL_TEXTURE
    _2D) - Then draw your colored shapes, then turn
    it back on by saying glEnable(GL_TEXTURE_2D)
  • 2) Load a texture. Once you have the data of the
    texture you loaded, you need to register it with
    OpenGL. Ill give you code for this, but in case
    you really want to know.the first step to do
    this is
  • glGenTextures(1, textureArraytextureID) This
    generates a unique texture reference to the
    desired index of our texture array. We then pass
    this data into glBindTexture() to set the current
    texture being mapped.
  • glBindTexture(GL_TEXTURE_2D, textureArraytexture
    ID) We NEED to call glBindTexture() after we
    generate the texture using glGenTextures,
    otherwise it won't be assigned the data from the
    next function, gluBuild2DMipmaps()

13
The Long Way of Adding Textures
  • gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
    pBitmap-sizeX, pBitmap-sizeY, GL_RGB,
    GL_UNSIGNED_BYTE, pBitmap-data) What is a mip
    map you ask? Mip maps are a bunch of different
    scaled sizes of the texture. The reason for this
    is because it makes the texture look nicer if we
    are farther away or up close to the texture. It
    picks the best size depending on the distance we
    are from it. It is a good idea to make textures
    that are a power of 2, like 2, 4, 8, 16, 32, 64,
    128, 256, etc... This makes it so it can get a
    smoother representation when it scales, otherwise
    it could look worse when scaled. After we build
    the mip maps, we then need to set the type of
    filter we want to use.
  • glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILT
    ER,GL_LINEAR)

14
The Long Way of Adding Textures
  • glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILT
    ER,GL_LINEAR) If we use GL_LINEAR it will look
    smoother, but takes a little speed hit. If we use
    GL_NEAREST it will be a bit faster, but will look
    pixelated and blotchy. The MIN filter is used
    whenever the pixel being textured maps to an area
    greater than one texture element. The MAX filter
    is used when the pixel being textured maps to an
    area less than or equal to one texture element
  • 3) After we load the texture, we just need to
    call glBindTexture() to set that as the current
    texture being mapped. Not necessary if you are
    only loading one texture map.

15
The Long Way of Adding Textures
  • 4) Next, we need to give each vertex a texture
    coordinate, also called (U, V) coordinates. We do
    that by calling glTexCoord2f(U, V) This assigns
    a part of the bitmap to the next vertice passed
    in. It's like putting a sticker on paper, you
    just match the correct corner with the correct
    paper corner. You have to be carefull when you do
    this or else the image will be flipped or upside
    down. It depends on the direction of your camera
    though. The order is VERY important.
  • 5) Lastly, you need to delete the textures after
    you are finished. You do this by calling
    glDeleteTextures(1, textureArraytextureIndex)

16
Animation
  • A RenderScene() function can be inserted in
    Windows OnPaint() function. OnPaint() is called
    whenever the window is moved or resized. You can
    insert this by mapping to the message WM_PAINT
    using the ClassWizard.
  • You might want the window to update when events
    happen that change the view (i.e. moving the
    mouse might rotate your view). Dont call
    OnPaint() yourself. Call InvalidateRect(NULL,FALS
    E)

17
Animation
  • Using the RenderScene() scene function you
    create, you would do the following
  • Clear the buffers with glClear(GL_COLOR_BUFFER_BIT
    GL_DEPTH_BUFFER_BIT)
  • Load the identity matrix into the buffer with
    glLoadIdentity()to put your viewpoint at the
    origin.
  • Translate your viewpoint away from the origin.
  • Translate, rotate and scale your scene.

18
Next Time
  • Matrix Transformation
  • Splines, Curves, Patches
Write a Comment
User Comments (0)
About PowerShow.com