510 likes | 591 Views
COS 397 Computer Graphics. Svetla Boytcheva AUBG, Spring 2013. Lecture 4 Textures & Sphere. Outline. Texture on Cube Sphere Texture on Sphere MultiTexturing. Texture. TGA format
E N D
COS 397 Computer Graphics SvetlaBoytcheva AUBG, Spring 2013
Outline • Texture on Cube • Sphere • Texture on Sphere • MultiTexturing
Texture • TGA format • TGA or TARGA format is a format for describing bitmap images, it is capable of representing bitmaps ranging from black and white, indexed color, and RGB color, the format also supports various compression methods. This note describes the minimal requirements for creating a TGA file for a 24 bit RGB uncompressed color image, this covers most applications where a developer might want to create an image for another package which reads TGA files. • More: http://www.paulbourke.net/dataformats/tga/ • Converter: • http://image.online-convert.com/convert-to-tga
TEXTURE • Patterns should be seamless
Texture Mapping (1,0) (1,1) (a,b) (a,b) (0,0) (0,1)
Load Texture • GLuint LoadTexture(const char* TextureName) • { • GLuint Texture; • glGenTextures(1,&Texture); • glBindTexture(GL_TEXTURE_2D,Texture); • if(glfwLoadTexture2D(TextureName, GLFW_BUILD_MIPMAPS_BIT)) • { • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); • glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); • return Texture; • } else return -1; • }
textureLoading and activating • GLuint text2D; • glEnable(GL_TEXTURE_2D); • glGenTextures(1,&text2D); • glBindTexture(GL_TEXTURE_2D,text2D); • text2D = glfwLoadTexture2D("pattern.tga", • GLFW_BUILD_MIPMAPS_BIT);
Texture mapping on cube (outside) • void drawTexturedCube() • { • float a = 0.5; • glBegin( GL_QUADS ); • glNormal3f(-a, -a, -a); • glTexCoord2f(0, 0); // indicates which part of the texture will be located in the current vertex ... • glVertex3f(-a, -a, -a); • glNormal3f(-a, -a, +a); • glTexCoord2f(0, 2); // … the texture is a square from (0,0) to (1,1), and in this example is mapped to … • glVertex3f(-a, -a, +a); • glNormal3f(-a, +a, +a); • glTexCoord2f(2, 2); // ... the cube’s side by setting one of the side’s vertices to correspond to the point • glVertex3f(-a, +a, +a); • glNormal3f(-a, +a, -a); • glTexCoord2f(2, 0); // ... (0,0) of texture, and the other one – to point (2,2) ... i.e. the texture will be doubled • glVertex3f(-a, +a, -a);
int main() • { • init(); • glClearColor( 0,0,0,0 ); • glEnable( GL_DEPTH_TEST ); • glEnable( GL_LIGHTING ); • glEnable( GL_LIGHT0 ); • glEnable( GL_COLOR_MATERIAL ); • glEnable( GL_NORMALIZE ); • glShadeModel(GL_SMOOTH); • glColor3f(1,1,1); • // Texture Loading and activating • GLuint text2D; • glEnable(GL_TEXTURE_2D); • glGenTextures(1,&text2D); • glBindTexture(GL_TEXTURE_2D,text2D); • text2D = glfwLoadTexture2D("pattern.tga", GLFW_BUILD_MIPMAPS_BIT);
while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT ); • t=t+0.00005; • float q = 80*sin(t/3); • glLoadIdentity(); • float dist=70; • gluLookAt(dist*cos(5*t)*cos(q),dist*sin(5*t)*cos(q),dist*sin(q), • 0,0,0, 0,1,1 ); • glScalef(40,40,40); • drawTexturedCube(); • glfwSwapBuffers(); • } • finit(); • return 0; • }
Look at • glLoadIdentity(); // identity matrix • gluLookAt(Ex,Ey,Ez,Ox,Oy,Oz,Ux,Uy,Uz); • Example: • glLoadIdentity(); • gluLookAt(10,0,5,0,0,0,0,0,1); Eye point Look at point Up vector
ViewingCoordinates WorldCoordinates y lookat point eye point eye point (at origin) lookat point (along –z axis) z y x x z
ViewingCoordinates WorldCoordinates y lookat point eye point lookat point (along –z axis) y x x z
ViewingCoordinates WorldCoordinates y lookat point eye point Why notthese axes? y' x' y x x z
ViewingCoordinates WorldCoordinates • Specify up direction in world coordinates • Transform up vector into viewing coordinates • Rotate about z-axis until up vector in x-y plane. y up lookat point eye point z y x x z up vector up vector
ViewingCoordinates WorldCoordinates • Specify up direction in world coordinates • Transform up vector into viewing coordinates • Rotate about z-axis until up vector in x-y plane. y up lookat point eye point z y x z x up vector up vector
Lookat Transformation upvector eyepoint lookatpoint y x z
Sphere • We use Spherical Coordinates and transform them to Cartesian Coordinates
COS397_POINT spherical( float alpha, float beta, float r ) • { • COS397_POINT p; • p.x = r*cos(alpha)*cos(beta); • p.y = r*sin(alpha)*cos(beta); • p.z = r*sin(beta); • return p; • }
void drawSphere( float x, float y, float z, float r ) • { • float alpha; • float dalpha = 2.0*M_PI/na; • float beta; • float dbeta = 1.0*M_PI/nb; • beta = M_PI/2; • for( int j=0; j<nb; j++, beta-=dbeta) • { • alpha = 0; • for( int i=0; i<na; i++, alpha+=dalpha) • { • glBegin( GL_LINE_LOOP ); • COS397_POINT a = spherical(alpha,beta,r); • COS397_POINT b = spherical(alpha+dalpha,beta,r); • COS397_POINT c = spherical(alpha+dalpha,beta-dbeta,r); • COS397_POINT d = spherical(alpha,beta-dbeta,r); • glVertex3f(x+a.x,y+a.y,z+a.z); • glVertex3f(x+b.x,y+b.y,z+b.z); • glVertex3f(x+c.x,y+c.y,z+c.z); • glVertex3f(x+d.x,y+d.y,z+d.z); • glEnd( ); • } • } • }
Normal vectors • Normal vector to the “central” point of each piece
void drawSolidSphere( float x, float y, float z, float r ) • { • float alpha; • float dalpha = 2.0*M_PI/na; • float beta; • float dbeta = 1.0*M_PI/nb; • beta = M_PI/2; • for( int j=0; j<nb; j++, beta-=dbeta) • { • alpha = 0; • for( int i=0; i<na; i++, alpha+=dalpha) • { • glBegin( GL_POLYGON ); • COS397_POINT a = spherical(alpha,beta,r); • COS397_POINT b = spherical(alpha+dalpha,beta,r); • COS397_POINT c = spherical(alpha+dalpha,beta-dbeta,r); • COS397_POINT d = spherical(alpha,beta-dbeta,r); • COS397_POINT n = spherical(alpha+dalpha/2,beta-dbeta/2,0.8); • glNormal3f(n.x,n.y,n.z); • glVertex3f(x+a.x,y+a.y,z+a.z); • glVertex3f(x+b.x,y+b.y,z+b.z); • glVertex3f(x+c.x,y+c.y,z+c.z); • glVertex3f(x+d.x,y+d.y,z+d.z); • glEnd( ); • } • } • }
void drawSmoothSphere( float x, float y, float z, float r ) • { • float alpha; • float dalpha = 2.0*M_PI/na; • float beta; • float dbeta = 1.0*M_PI/nb; • beta = M_PI/2; • for( int j=0; j<nb; j++, beta-=dbeta) • { • alpha = 0; • for( int i=0; i<na; i++, alpha+=dalpha) • { • COS397_POINT p; • glBegin( GL_POLYGON ); • p = spherical(alpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glVertex3f(x+r*p.x,y+r*p.y,z+r*p.z); • p = spherical(alpha+dalpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glVertex3f(x+r*p.x,y+r*p.y,z+r*p.z); • p = spherical(alpha+dalpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glVertex3f(x+r*p.x,y+r*p.y,z+r*p.z); • p = spherical(alpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glVertex3f(x+r*p.x,y+r*p.y,z+r*p.z); • glEnd( ); • } • } • }
Ellipsoid • Like Sphere but for radius different scaling is used
Ellipsoid patch • Horizontal angle α instead in range is in range Vertical angle is in range
Texture on Sphere Rock.tga
void drawTextureEllipsoidPatch( • float x, float y, float z, // center • float rx, float ry, float rz, // ellipsoid radius • float a1, float a2, // horizontal angle • float b1, float b2 // vertical angle • ) • { • int na = 16; • int nb = 16; • float alpha; • float dalpha = (a2-a1)/na; • float beta; • float dbeta = (b2-b1)/nb;
beta = b2; • for( int j=0; j<nb; j++, beta-=dbeta) • { alpha = a1; • for( int i=0; i<na; i++, alpha+=dalpha) • { COS397_POINT p; • glBegin( GL_POLYGON ); • p = spherical(alpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f(alpha,beta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha+dalpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f(alpha+dalpha,beta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha+dalpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f( alpha+dalpha,beta-dbeta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f( alpha,beta-dbeta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • glEnd( ); • } • } • }
Texture on Sphere – wrapped version earthmap.tga
Texture Mapping • Texture coordinates (X,Y) are in range [0,1] for X and Y • Sphere coordinates are (alpha, beta), where alpha is in the range [0; 2*Pi] and beta is in the range [-Pi/2; Pi/2] • In order to map the texture on sphere (wrapped) we need to find the correspondence between sphere coordinates and texture coordinate • To avoid complex calculations we will move (translate) and scale the sphere to the texture coordinates, i.e. [0,1], thus sphere and texture coordinates will be equivalent
Texture Mapping • Step 1: Scaling form [-Pi/2; Pi/2] to [-1/2, 1/2] for beta (second parameter) and form [0; 2*Pi] to [0, 1] for alpha (first parameter) • Step 2: Translation for beta(second parameter) from [-1/2, 1/2] to [0,1] glScalef(1/(2*M_PI),1/M_PI, 0); 1 0 glTranslatef(0,0.5,0);
void drawPlanetTextureEllipsoidPatch( • float x, float y, float z, // center • float rx, float ry, float rz, // ellipsoid radius • float a1, float a2, // horizontal angle • float b1, float b2 // vertical angle • ) • { • glMatrixMode( GL_TEXTURE); • glLoadIdentity(); • glTranslatef(0,0.5,0); • glScalef(1/(2*M_PI),1/M_PI, 0); • glMatrixMode( GL_MODELVIEW); • int na = 16; • int nb = 16; • float alpha; • float dalpha = (a2-a1)/na; • float beta; • float dbeta = (b2-b1)/nb;
beta = b2; • for( int j=0; j<nb; j++, beta-=dbeta) • { • alpha = a1; • for( int i=0; i<na; i++, alpha+=dalpha) • { • COS397_POINT p; • glBegin( GL_POLYGON ); • p = spherical(alpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f(alpha,beta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha+dalpha,beta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f(alpha+dalpha,beta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha+dalpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f( alpha+dalpha,beta-dbeta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • p = spherical(alpha,beta-dbeta,1); • glNormal3f(p.x,p.y,p.z); • glTexCoord3f( alpha,beta-dbeta,0); • glVertex3f(rx*p.x,ry*p.y,rz*p.z); • glEnd( ); • } • } • }
Multitexturing • // Multi Texture Loading and activating • GLuint textureA, textureB; • glEnable(GL_TEXTURE_2D); • glGenTextures(1,&textureA); • glBindTexture(GL_TEXTURE_2D,textureA); • glfwLoadTexture2D("Pattern.tga", GLFW_BUILD_MIPMAPS_BIT); • glGenTextures(1,&textureB); • glBindTexture(GL_TEXTURE_2D,textureB); • glfwLoadTexture2D("Cloth.tga", GLFW_BUILD_MIPMAPS_BIT);
glScalef(30,30,30); • glTranslatef(0,-0.6,0); // The first cube's center point is translated to (0,-0.6,0) • glBindTexture(GL_TEXTURE_2D,textureA); // the first texture loading and activating • drawTexturedCube(); • glBindTexture(GL_TEXTURE_2D,textureB);// the second texture loading and activating • glTranslatef(0,+1.2,0); // The second cube's center point is translated to (0,+1.2,0) • drawTexturedCube();
Planet and sky • for sky modeling we will use a cube with mapped texture inside • To map the texture inside in the cube we need to change the direction of normal vectors – for each side the sign of the corresponding coordinate should be changed • The scene will be inside in the cube and the planet will rotate in the center of the cube • Cube size should be large enough in order to accommodate the planet • View point (look at) must be inside in the cube
Texture mapping on cube (inside) • glBegin( GL_QUADS ); // Side -X • glNormal3f(+a, -a, -a); // all normal vectors must have +a for X • glTexCoord2f(0, 0); • glVertex3f(-a, -a, -a); // all points have X coordinate -a • glNormal3f(+a, -a, +a); • glTexCoord2f(0, 2); • glVertex3f(-a, -a, +a); • glNormal3f(+a, +a, +a); • glTexCoord2f(2, 2); . • glVertex3f(-a, +a, +a); • glNormal3f(+a, +a, -a); • glTexCoord2f(2, 0); • glVertex3f(-a, +a, -a);
room Door and picture should be drawn in close distance to the walls, but in front of them inside into the cube