Phong

This is a discussion on Phong within the Game Programming forums, part of the General Programming Boards category; How come phong shading (D3DSHADE_PHONG) is not supported in DX? It's slow and perhaps not so useful, but still......

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Phong

    How come phong shading (D3DSHADE_PHONG) is not supported in DX? It's slow and perhaps not so useful, but still...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    Because by DirectX8 where the symbol was introduced, you could accomplish the same using vertex and pixel shaders. The fixed function pipeline is too slow to do phong shading in real time.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Phong shading can be done manually. You just calculate the phong lighting per pixel with normals interpolated accross the face of each polygon. (of course this very expensive and isnt the best choice for real time rendering as Bubba pointed out)

    Here's an excerpt from my raytracer, ive tried to clean up the code a bit for clarity and generalization (hopefully i didnt mess it up too much!). This function is called to calculate the phong lighting for every pixel in the resulting frame (ie. Phong Shading)

    ray is the current vector from the eye to the point on the polygon.
    face The vertices of the current polygon.
    u and v are the interpolation coefficients of the current polygon.
    t is the scalar factor of the ray representing the distance from the eye to the point on the polygon.
    c is where the resulting color of the pixel will be stored.

    information about lights and objects in the scene is found in the scene member variable.

    Code:
    void RayTracer::calcPhongLighting(const Ray& ray, VertexList* face, float u, float v, float t, Color& c) {
    
      /* the coefficients for each type of lighting */
      float kambient = 0.8f;
      float kdiffuse = 0.75f;
      float kspecular = 0.25f;
      float shine = 50.0f;
    
      Point3f point = ray.start + (ray.dir * t);
    
      Vector3f norm = scene->currentObject.normals[(face->vertex) - 1] * u +
        scene->currentObject.normals[(face->next->vertex) - 1] * (1.0f - (u + v)) +
        scene->currentObject.normals[(face->next->next->vertex) - 1] * v;
      
      norm.normailze();
    
      Ray reflect;
      reflect.start = point;
      reflect.dir = norm * (2 * norm.dot(ray.dir * -1)) + ray.dir;
      reflect.dir.normalize();
      // recursive ray trace call for reflection
      castRay(scene->objects, reflect, c);
    
      /* after the recursive 'castRay' function returns, 'c' will have the ambient term of the lighting */  
      c.r = (unsigned char)(c.r * kambient); 
      c.g = (unsigned char)(c.g * kambient); 
      c.b = (unsigned char)(c.b * kambient);
      
      for (int i = 0; i < scene->numLights; i++) {
    
        Vector3f lightDir = -(scene->lights[i].pos - point);
        Vector3f reflect = norm * (2 * norm.dot(lightDir)) - lightDir;
        lightDir.normalize();
        reflect.normalize();
        float l = norm.dot(lightDir * -1) * kdiffuse + pow((-ray.dir).dot(reflect), shine) * kspecular;
        Color light = scene->lights[i].color;
        c.r += (int)((float)light.r * l); c.g += (int)((float)light.g * l); c.b += (int)((float)light.b * l);
      }
    }
    (Magos, i know your post was more related to DX support so this may not be of interest to you. I posted this more for the sake of search bait in case anyone else comes wandering in curious about Phong Shading )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phong Pixel Shader for PS1.3
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 08-27-2005, 11:05 PM
  2. Shot of some detail textures in engine
    By VirtualAce in forum Game Programming
    Replies: 19
    Last Post: 05-29-2004, 01:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21