texturing in opengl lab #7. creating textures glenable ( gl_texture_2d ); gluint mytex;...

7
Texturing in OpenGL Lab #7

Upload: kathryn-carroll

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Texturing in OpenGL

Lab #7

Page 2: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Creating Textures

glEnable ( GL_TEXTURE_2D );

GLuint myTex;

glGenTextures ( 1, (GLuint*) &myTex );

glBindTexture ( GL_TEXTURE_2D, myTex );

glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );

int xres = 256;int yres = 256;char* data; glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, xres, yres, 0, GL_RGB, GL_UNSIGNED_BYTE, data );

Page 3: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

In IGX, these steps are already done for you:

ImageX img;

void setup (){

img.Load ( “abc.jpg” ); // does glGenTextureimg.Update (0); // does glTexImage2D

int id = img.getGLID (); // Give the OpenGL id}

Page 4: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Binding and Using Textures

glEnable ( GL_TEXTURE_2D );glBindTexture ( GL_TEXTURE_2D, id );

Texture Coordinates

Similar to glColor and glNormal3f, goes before glVertex.Sets the UV texture coordinate for the next vertex.

glTexCoord2f ( u ,v ); glVertex3f ( 1, 0, 0 );glTexCoord2f ( u ,v ); glVertex3f ( 0, 1, 0 );

Page 5: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Lab #7

You are given a rendered cube like this.An image which contains 6 sides is already loaded: abc.jpg

Page 6: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Modify the code to bind the texture and apply UV coordinatesso that the cube is properly textured to look like a Toy block.

Lab #7

Notice that the side letters are B, C, D, E in order, upright. The bottom is F.

Page 7: Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,

Advanced:

* Create and animate a texture without using the ImageX class.

You need to:

1) Create an array of bytes to hold the data

2) Call glGenTexture and glTexImage2D to load the data

3) Change the texture over time. This could be done by painting it using the mouse, or modifying by a function over time.

4) If you animate the texture, you must call glTexImage2D in the draw3D portion (not setup). This makes sure the image is continuously updated on the GPU.

5) Call glBindTexture and glTexCoords to render the texture on object.