Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Shader.h"
- #include "OpenGLWin.h"
- #include "BitmapFile.h"
- #include "miscio.h"
- #include "shape.h"
- #include "scene.h"
- #include "time.h"
- ///////////////////////////////////////////////////////////////////
- // The constant shader, shades each polygon with a constant color
- // The color is defined by the object's "diffuse color"
- ///////////////////////////////////////////////////////////////////
- void constantShader::BeginShading(void)
- {
- glDisable(GL_LIGHTING);
- }
- void constantShader::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- // To Do
- //
- // Replace the following code with code that will draw a polygon that is shaded
- // a single constant color
- glBegin(GL_POLYGON);
- for (int i = 0; i < n; i++)
- glVertex3dv(p[i].AsArray());
- glEnd();
- }
- void constantShader::EndShading(void)
- {
- }
- ///////////////////////////////////////////////////////////////////
- // The wireframe shader, each polygon is drawn as a wireframe
- // The color of each line is defined by the object's "diffuse color"
- ///////////////////////////////////////////////////////////////////
- void wireframeShader::BeginShading(void)
- {
- glDisable(GL_LIGHTING);
- }
- void wireframeShader::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < n; i++)
- glVertex3dv(p[i].AsArray());
- glEnd();
- }
- void wireframeShader::EndShading(void)
- {
- }
- ///////////////////////////////////////////////////////////////////
- // The faceted shader, each polygon is drawn with a single normal
- // The color of each polygon is defined by the object's
- // Lighting properties, ambient, diffuse, specular and shininess
- // Attributes
- ///////////////////////////////////////////////////////////////////
- void facetedShader::BeginShading(void)
- {
- // To Do
- //
- // Enable lighting and set the shading mode to faceted shading
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_FLAT);
- }
- void facetedShader::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- // To Do
- //
- // Replace the following code with code that will render the polygon with
- // faceted shading
- glBegin(GL_POLYGON);
- glNormal3d((GLdouble)pn[0], (GLdouble)pn[1], (GLdouble)pn[2]);
- for (int i = 0; i < n; i++)
- {
- glVertex3dv(p[i].AsArray());
- }
- glEnd();
- }
- void facetedShader::EndShading(void)
- {
- // To Do
- //
- // Any cleanup neccessary
- }
- ///////////////////////////////////////////////////////////////////
- // The gouraud shader, each vertex is given a normal and lighting
- // Calculations are performed per vertex to get a vertex color.
- // Colors are then bilinearly interpolated over the surface of the
- // polygon
- //
- // Lighting properties, ambient, diffuse, specular and shininess
- // Attributes determine the color at each vertex
- ///////////////////////////////////////////////////////////////////
- void gouraudShader::BeginShading(void)
- {
- // To Do
- //
- // Enable lighting and set the shading model to smooth (gouraud) shading
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- }
- void gouraudShader::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- // To Do
- //
- // Replace the following code with the neccessary code for drawing a polygon
- // with smooth (gouraud shading)
- glBegin(GL_POLYGON);
- for (int i = 0; i < n; i++)
- {
- glNormal3d((GLdouble)vn[i][0], (GLdouble)vn[i][1], (GLdouble)vn[i][2]);
- glVertex3dv(p[i].AsArray());
- }
- glEnd();
- }
- void gouraudShader::EndShading(void)
- {
- // To Do
- //
- // Any cleanup neccessary
- }
- ///////////////////////////////////////////////////////////////////
- // The texture shader applies an image texture map to an object and
- // applies gouraud style lighting interpolation. It is much more
- // complex, requiring several parameters:
- //
- // map : The name of the texture map. This name should
- // refer to a file in one of the following formats
- //
- // bmp, tga
- //
- // shape : What shape is used to project the image onto the
- // Possible values are
- //
- // planar, cylindrical, spherical and box
- //
- // axis : What axis is used for the projection. The effect
- // of this parameter affects different shapes in
- // different ways. Possible values are:
- //
- // 0 = x-axis, 1 = y-axis, 2 = z-axis
- //
- // this should be replaced by a more general scheme
- //
- // entity : How are texture coordinates chosen for lookup
- // Possible values are
- //
- // position, centroid, normal, reflection
- //
- // and much much more ...
- //
- ///////////////////////////////////////////////////////////////////
- textureShader::textureShader()
- {
- tObject = 0;
- tTextureUnit = 0;
- tEntity = Position;
- tCoordSys = Object;
- tShape = Planar;
- tAxis = XAxis;
- for (int i = 0; i < 10; i++)
- tName[i][0] = '\0';
- tMinFilter = GL_LINEAR_MIPMAP_LINEAR;
- tMagFilter = GL_LINEAR;
- tHorzWrap = GL_REPEAT;
- tVertWrap = GL_REPEAT;
- tBlend = GL_MODULATE;
- tBlendColor[0] = 1.0; tBlendColor[1] = 1.0; tBlendColor[2] = 1.0; tBlendColor[3] = 1.0;
- tConstColor[0] = 0.0; tConstColor[1] = 0.0; tConstColor[2] = 0.0; tConstColor[3] = 0.0;
- tFilterColor[0] = 1.0; tFilterColor[1] = 1.0; tFilterColor[2] = 1.0; tFilterColor[3] = 1.0;
- tViewTransform = false;
- tAnisotropy = 0;
- }
- textureShader::~textureShader()
- {
- glDeleteTextures(1, &tObject);
- }
- void textureShader::BeginShading(void)
- {
- // To Do
- //
- // Enable lighting and set smooth shading
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_BLEND);
- // To Do
- //
- // Enable the seperate specular color so that highlights can appear
- // on top
- // To Do: Advanced
- //
- // Activate the proper texture unit for multi-texturing
- // To Do
- //
- // Set up the texture by calling "SetupTextureMap" and enable the proper texture
- // target (e.g. GL_TEXTURE2D). Hint: the texture target is set at the top of
- // one of the methods below and is stored in a member of this object
- SetupTextureMap();
- glEnable(this->glTarget);
- // Apply texture transforms
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- // To Do: Advanced
- //
- // Apply texture transforms, and if we are using a viewing transform in the
- // texture (say for reflection mapping) then set up the proper viewing transform
- if (tEntity == Eye) // Project to the screen.
- {
- // To Do: Advanced
- //
- // Apply the necessary transforms to project the 3D texture coordinates
- // (which, if this is active, will be the vertex object coordinates) to
- // the screen (normalized device coordinates)
- }
- // Now we are done with the textrue transforms transforms
- glMatrixMode(GL_MODELVIEW);
- // To Do: Advanced
- //
- // If appropriate, begin automatic coordinate generation for normal mapping or reflection mapping
- }
- void textureShader::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- BeginPolygon(n, p, vn, pn, uv); // Used to prevent texture tearing, and setup for advanced textures
- // To Do
- //
- // Replace the following code with code that will draw the polygon
- // and apply the texture to it by calling the "ApplyTexture" method
- // on each vertex.
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < n; i++)
- {
- glNormal3d((GLdouble)vn[i][0], (GLdouble)vn[i][1], (GLdouble)vn[i][2]);
- ApplyTexture(p[i], vn[i], uv[i]);
- glVertex3dv(p[i].AsArray());
- }
- glEnd();
- }
- void textureShader::EndShading(void)
- {
- // To Do
- //
- // Any cleanup required (hint: whatever you've started up in BeginShading or
- // SetupTextureMap, you need to shut down here!)
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
- }
- void textureShader::SetupTextureMap(void)
- {
- if (tShape == Box)
- glTarget = GL_TEXTURE_CUBE_MAP;
- else
- glTarget = GL_TEXTURE_2D;
- // Note that we only initialize the OpenGL texture objects if we
- // don't yet have an OpenGL texture object
- if (tObject <= 0)
- {
- // Find the number of textures that have been specified
- // Multiple images are used for manually specifiying mipmaps
- // and for box maps
- int nImages = 0;
- while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
- nImages++;
- // If there are none, then do nothing and return
- if (nImages < 1)
- return;
- // Generate the texture object and bind it to the texture target defined above
- glGenTextures(1, &tObject);
- glBindTexture(glTarget, tObject);
- // Now, loop through the images and send them to OpenGL
- for (int nImage = 0; nImage < nImages; nImage++)
- {
- BitmapFile texture;
- if (!texture.Read(tName[nImage]))
- complain("Couldn't read texture %s", tName);
- // Now we will multiply the image by tFilterColor and add tConstColor.
- // We do these with methods in the BitmapFile class.
- if (tFilterColor[0] < .99999 || tFilterColor[1] < .99999 || tFilterColor[2] < .99999 || tFilterColor[3] < .99999)
- texture.Filter(tFilterColor[0], tFilterColor[1], tFilterColor[2], tFilterColor[3]);
- if (tConstColor[0] > .00001 || tConstColor[1] > .00001 || tConstColor[2] > .00001 || tConstColor[3] > .00001)
- texture.Add(tConstColor[0], tConstColor[1], tConstColor[2], tConstColor[3]);
- // To Do
- //
- // Get the width and height from the texture file
- int w, h;
- w = texture.Width();
- h = texture.Height();
- // To Do
- //
- // Set the image data pointer to point do the data in the texture (Hint:
- // is there a method in the texture that
- GLubyte *imageData;
- imageData = texture.ImageData();
- // To Do
- //
- // Set the src and target formats for the texture data. The srcFormat should
- // match the format of the data in the texture file itself. Hint: what order
- // are the colors stored in bitmap and targa files? The targFormat is the
- // format that we want the data stored inside OpenGL. Note that these two
- // may not be the same.
- //
- // We also need to check here for the number of bytes per pixel in the image.
- // For our examples, it will be either 3 or 4. If it is 4, then there is an
- // alpha channel in the data, otherwise it is three. Set the srcFormat and
- // targFormat appropriately, or the data will not be read in correctly!
- GLuint srcFormat = GL_BGRA, targFormat = GL_RGBA;
- if(texture.BytesPerPixel() == 3)
- srcFormat = GL_BGR, targFormat = GL_RGB;
- // Rows in our image are stored contiguously (not aligned on a specific number
- // of bytes, so set the unpack alignment to 1 --> single byte aligned
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- // To Do
- //
- // If it is a box map (which doesn't support manual mipmap specification in RGL)
- // or if only one image is supplied turn on automatic mipmap generation.
- //gluBuild2DMipmaps(glTarget, 3, w, h, srcFormat, GL_UNSIGNED_BYTE, imageData);
- if (nImages == 1 || tShape == Box)
- glTexParameteri(glTarget, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
- // To Do
- //
- // Send the data to OpenGL. Be very careful when sending all the data. For a first
- // shot you may assume that the texture target is GL_TEXTURE2D, but you will need to
- // change this command when we enable box mapping!
- if (tShape != Box)
- glTexImage2D(GL_TEXTURE_2D, nImage, targFormat, (GLsizei)w, (GLsizei) h, (GLint)0, srcFormat, GL_UNSIGNED_BYTE, imageData);
- else
- glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + nImage, 0, targFormat, (GLsizei)w, (GLsizei)h, (GLint)0, srcFormat, GL_UNSIGNED_BYTE, imageData);
- }
- }
- // To Do
- //
- // Now, bind the texture and set the texture parameters. You need to set several here.
- // Set the parameters for how the image wraps and what kind of magnification and minification
- // filter you want to use, and also, set the anisotropy level. Also set the blend mode and
- // blend color. Note, if you do not do this correctly, your textxure will not show up at all,
- // particularly for mipmapping. If you use a mipmapped filter without enabling mipmap generation
- // above, you will get nothing!
- //glBindTexture(glTarget, tObject);
- glTexParameteri(glTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//this->tMinFilter);
- glTexParameteri(glTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//this->tMagFilter);
- glTexParameteri(glTarget, GL_TEXTURE_WRAP_S, GL_REPEAT);//this->tHorzWrap);
- glTexParameteri(glTarget, GL_TEXTURE_WRAP_T, GL_REPEAT);//this->tVertWrap);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->tAnisotropy);
- glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBlendFunc(this->tBlend, GL_ONE_MINUS_SRC_ALPHA);
- //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glBlendColor(this->tBlendColor[0], this->tBlendColor[1], this->tBlendColor[2], this->tBlendColor[3]);
- // glEnable(GL_TEXTURE_2D);
- }
- void textureShader::ApplyTexture(point &pObj, vector &nObj, vector &uvObj)
- {
- // For normal and reflection entities, we do nothing here. These texture coordinates
- // are generated on the graphics card. See the end of the BiginTexture method.
- if (tEntity == Normal || tEntity == Reflection)
- return;
- else if (tEntity == Eye)
- {
- // To Do: Advanced
- //
- // If the entity is "Eye" then send the object coordinates of the vertex.
- // They will be converted into screen coordinates by the texture transform.
- glTexCoord4d(pObj[0], pObj[1], pObj[2], pObj[3]);
- return;
- }
- double u, v, w;
- CalculateTexCoords(pObj, nObj, uvObj, u, v, w);
- // To Do
- //
- // Send the texture coordinates to OpenGL.
- //
- // Note that if the entity is UV then "CalculateTexCoords" will
- // Simply set u = uvObj[0] and v = uvObj[1], i.e. it passes the
- // uv coordinates into u and v in the previous call, so you don't
- // need to implement anything in CalculateTexCoords to render examples
- // that use a uv entity.
- //
- // When you implement box mapping, you will need to change this call
- // to send all three coordinates for the box map.
- //
- // (For multitexturing) Use the multitexturing version of the text coordinate
- // function from OpenGL.
- }
- void textureShader::CalculateTexCoords(point &pObj, vector &nObj, vector &uvObj,
- double &u, double &v, double &w)
- {
- point coord;
- point tmp;
- point sMin, sMax, sCenter;
- // This version of RenderGL can only work with texture coordinates based
- // on object coordinates, not world coordinates. This covers most common
- // texturing situations.
- point p = pObj;
- curShape->MinMax(sMin, sMax);
- sCenter = curShape->Center();
- switch (tEntity)
- {
- case UV:
- // NOTE: for the uv entity, we set the values here and IMMEDIATELY RETURN
- //
- u = uvObj[0]; // Copy uv's from polygon data into output
- v = uvObj[1];
- w = 0.0; // This shape doesn't use "w"
- return; // Have filled the u and v so can return
- case Position:
- // To Do
- //
- // Calculate the position of the vertex relative to the min and max
- // of the object (retrieved above). Store the result in the array "tmp"
- break;
- case Centroid:
- // To Do
- //
- // Calculate the position of the vertex relative to the center of the object
- // and store the result in the point "tmp"
- break;
- }
- // Here we apply the "map axis". We do this by rearranging the coordinates in
- // tmp and placing them in the point "coord". The mapaxis coordinate is always
- // placed in "z" and the other two are placed in x and y, while preserving
- // right-handedness.
- //
- // Note also that the default and only choice for Box shape is the z-axis.
- coord[0] = tmp[0]; coord[1] = tmp[1]; coord[2] = tmp[2];
- if (tShape != Box)
- {
- if (tAxis == XAxis)
- {
- coord[0] = tmp[1]; coord[1] = tmp[2]; coord[2] = tmp[0];
- }
- else if (tAxis == YAxis)
- {
- coord[0] = tmp[2]; coord[1] = tmp[0]; coord[2] = tmp[1];
- }
- }
- // Here we apply the shape of the texture map (remember, for the uv entity,
- // we will never get here ... see above)
- u = v = w = 0;
- switch (tShape)
- {
- case Planar:
- PlanarMap(coord, u, v);
- break;
- case Cylindrical:
- CylindricalMap(coord, sMin[tAxis], sMax[tAxis], u, v);
- break;
- case Spherical:
- SphericalMap(coord, u, v);
- break;
- case Box:
- // Here we just copy the coordinates directly into u, v and w.
- u = coord[0];
- v = coord[1];
- w = coord[2];
- break;
- };
- }
- void textureShader::PlanarMap(const point &p, double &u, double &v)
- {
- if (tEntity != Position)
- complain("The Planar map can only be used with the position entity");
- // Note that the min-max calculation for planar is computed in the entity above
- // so there is nothing to do here. We just copy the coordinates from p directly
- // into u and v.
- u = p[0];
- v = p[1];
- }
- void textureShader::CylindricalMap(const point &p, double minZ, double maxZ, double &u, double &v)
- {
- if (tEntity != Centroid)
- complain("The Cyindrical map can only be used with the centroid entity");
- // To Do
- //
- // Calculate u and v for the texture map using the point p (which will be already
- // calcualted relative to the center of the object ... see above). Use the formula
- // for a cylindrical map. Note, you need to take care of ANY situations where the
- // could be zero!
- // Note: The following piece of code makes sure that there is no seam
- // when the vertices of a polygon span the edge of the texture
- // When the polygon is begun, uBase is reset (see the DrawPolygon method)
- // So the first vertex process set's the uBase. Then all other
- // vertices are compared to this uBase value
- if (uBase == -1)
- uBase = u;
- else
- {
- if (uBase > .75 && u < .25)
- u += 1;
- else if (uBase < .25 && u > .75)
- u -= 1;
- }
- }
- void textureShader::SphericalMap(const point &p, double &u, double &v)
- {
- if (tEntity != Centroid)
- complain("The Spherical map can only be used with the centroid entity");
- // To Do
- //
- // Calculate u and v for the texture map using the point p (which will be already
- // calcualted relative to the center of the object ... see above). Use the formula
- // for a spherical map. Note, you need to take care of ANY situations where the
- // could be zero!
- // As in the cylindrical map, The following piece of code makes sure
- // that there is no seam when the vertices of a polygon span the edge of the texture.
- //
- // To Do
- //
- // Replace the test around this piece of code to make sure that the point isn't
- // exactly at the center (i.e. p is zero)
- if (true)
- {
- if (uBase == -1)
- uBase = u;
- else
- {
- if (uBase > .75 && u < .25)
- u += 1;
- else if (uBase < .25 && u > .75)
- u -= 1;
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////
- // The lightMap shader provides a texturing solution to per-pixel
- // specular lighting.
- ///////////////////////////////////////////////////////////////////////////
- lightMap::lightMap()
- {
- for (int i = 0; i < 3; i++)
- specSurfColor[i] = 1.0;
- shininess = 1.0;
- }
- lightMap::~lightMap()
- {
- glDeleteTextures(1, &tObject);
- }
- void lightMap::BeginShading(void)
- {
- GLfloat fBlack[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
- // To Do
- //
- // Disable specular lighting by setting the secular material to black.
- // You will need to reset the specula material once this is done, so
- // you will need to record the current specualr color to reset it in
- // the EndShading method.
- // Next, set up multi-texturing as before
- SetupTextureMap();
- // Now, enable the cube map target for texturing
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- // Then apply texture transforms for light mapping. This is the inverse
- // of the camera transform.
- glMatrixMode(GL_MODELVIEW);
- // Last, set up texure coordinate generation for reflection coordinates.
- }
- void lightMap::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- // To Do
- //
- // Replace the following with a gouraud shaded polygon
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < n; i++)
- glVertex3dv(p[i].AsArray());
- glEnd();
- }
- void lightMap::EndShading(void)
- {
- glActiveTextureARB(GL_TEXTURE0_ARB + tTextureUnit);
- glBindTexture(glTarget, 0);
- // To Do
- //
- // Disable the cube texture target and the texture coordinates
- // generation
- glEnable(GL_LIGHTING);
- }
- void lightMap::SetupTextureMap(void)
- {
- int i;
- if (tObject > 0)
- {
- glBindTexture(GL_TEXTURE_CUBE_MAP, tObject);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
- return;
- }
- glGenTextures(1, &tObject);
- glBindTexture(GL_TEXTURE_CUBE_MAP, tObject);
- int nLights = curScene->nLights;
- vector lightDir[8];
- point lightCol[8];
- // To Do
- //
- // Loop through all the active lights and record their directions and colors
- // in the lightDir and lightCol arrays. Don't forget to normalize the direction
- // The next loop creates the six faces
- for (i = 0; i < 6; i++)
- cubeMapFaces[i].Create(size, size);
- // a "Pixel" is used to store color values for a bitmap. It is a structure
- // with integer members r, g, b and alpha. These have ranges between 0 and 255.
- Pixel p;
- p.r = 0; p.g = 0; p.b = 0; p.alpha = 255;
- // Map for XPos
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Calculate the vector to the corresponding point on the cube
- // then loop through the lights and calculate the specular color from
- // each of the lights. Store the total luminance in the pixel p
- cubeMapFaces[0].PutPixel(i, j, p);
- }
- }
- // Map for XNeg
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Do the same for this map
- cubeMapFaces[1].PutPixel(i, j, p);
- }
- }
- // Map for YPos
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Do the same for this map
- cubeMapFaces[2].PutPixel(i, j, p);
- }
- }
- // Map for YNeg
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Do the same for this map
- cubeMapFaces[3].PutPixel(i, j, p);
- }
- }
- // Map for ZPos
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Do the same for this map
- cubeMapFaces[4].PutPixel(i, j, p);
- }
- }
- // Map for ZNeg
- for(i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- // To Do
- //
- // Do the same for this map
- cubeMapFaces[5].PutPixel(i, j, p);
- }
- }
- // Now, we loop through each of the maps and store the pixel values in the temporary
- // glIMage array
- GLbyte *glImage = new GLbyte[size * size * 4];
- for (int k = 0; k < 6; k++)
- {
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- Pixel p = cubeMapFaces[k].GetPixel(i, j);
- glImage[4 * (size * j + i) + 0] = p.r;
- glImage[4 * (size * j + i) + 1] = p.g;
- glImage[4 * (size * j + i) + 2] = p.b;
- glImage[4 * (size * j + i) + 3] = char(255);
- }
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glPixelStorei(GL_PACK_ALIGNMENT, 1);
- // To Do
- //
- // Send this array to OpenGL using glTexImage2D. Remember, you need
- // to use the six "Cube map" texture targets.
- }
- delete [] glImage;
- // To Do
- //
- // Set the texture filter and wrap paremeters. And set the blend mode to "add"
- // so that the specular highlight is added on top of any other textures.
- }
- ///////////////////////////////////////////////////////////////////////////
- // The multiTexture shader builds on the texture shader by allowing multiple
- // textures to be active at once. This is very useful for situations like
- // light mapping and phong specular reflection through texturing (a
- // very fast alternative to the phong texturer above.
- ///////////////////////////////////////////////////////////////////////////
- multiTexture::multiTexture()
- {
- glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures);
- textures = new textureShader *[maxTextures];
- for (int i = 0; i < maxTextures; i++)
- textures[i] = NULL;
- disableLight = false;
- disableDiffuse = false;
- disableSpecular = false;
- disableAmbient = false;
- }
- multiTexture::~multiTexture()
- {
- for (int i = 0; i < maxTextures; i++)
- if (textures[i])
- delete textures[i];
- delete [] textures;
- }
- void multiTexture::BeginShading(void)
- {
- // To Do
- //
- // Check to see if multi-texturing is supported!
- //
- // If it is, then loop through the active textures and call the BeginShading
- // method on each of them. An active texture is one whose pointer is not null.
- //
- // Push OpenGL's Lighting Attribute stack
- //
- // Now, if "disableLight" is true, you should disable lighting as the textures
- // contain a diffuse or specular map.
- //
- // If disable specular is true, then you should just disable the
- // specular reflectance of the object. Likewise for disablAmbient and
- // disableDiffuse
- }
- void multiTexture::DrawPolygon(int n, point p[], vector vn[], vector pn, vector uv[])
- {
- // This code loops through and makes sure that each texture will not
- // tear.
- for (int i = 0; i < maxTextures; i++)
- if (textures[i])
- textures[i]->BeginPolygon(n, p, vn, pn, uv);
- // To Do
- //
- // Replace the following code with code that will begin a polygon, and
- // for each vertex, loop through the textures and apply them with
- // their "ApplyTexture" method. Note that the ApplyTexture method will
- // need to have the multi-texturing version written.
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < n; i++)
- glVertex3dv(p[i].AsArray());
- glEnd();
- }
- void multiTexture::EndShading(void)
- {
- // To Do
- //
- // Shut down each of the active textures, and pop the lighting stack
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement