Esempio n. 1
0
//=================================================== mouse2plane
//===============================================================
void TFWidgetRen::mouse2plane(int x, int y)
{
  // assume we are always aligned with z axis
  // should be based on up vector and (at-eye)xup vector!!!!!
  float fx = 1 - (float)x/(float)gluvv.win.width;
  float fy = (float)(gluvv.win.height - y)/(float)gluvv.win.height;
  float vdir[3];
  subV3(vdir, gluvv.env.at, gluvv.env.eye);
  normalizeV3(vdir);
  scaleV3(gluvv.env.clip[0], vdir);
  float fcp[3]; //position of front clipping plane
  addV3(fcp, gluvv.env.eye, vdir);
  float cpnt[3];//clip plane point (projecton of screen point on clip plane)
  cpnt[0] = ((fcp[0] + gluvv.env.frustum[0]) + 
	     (gluvv.env.frustum[1] - gluvv.env.frustum[0]) * fx);
  cpnt[1] = ((fcp[1] + gluvv.env.frustum[2]) + 
	     (gluvv.env.frustum[3] - gluvv.env.frustum[2]) * fy);
  cpnt[2] = fcp[2]; //remember, assume z aligned view direction

  float ep[3];  //eye - pos
  subV3(ep, gluvv.env.eye, pos);
  float me[3];  //eye - clipplane point
  subV3(me, gluvv.env.eye, cpnt);
  float dir[3] = {0,0,-1};
  float t = dotV3(dir, ep)/dotV3(dir, me);
  negateV3(me);
  scaleV3(t,me);
  addV3(mousepnt, me, gluvv.env.eye);
}
Esempio n. 2
0
void uniqueMap(obj* object) {
   int i, j, k;
   int vIndices[3], tIndices[3];
   for(i = 0; i < object->faceCount; ++i) {
      for(j = 0; j < 3; ++j) {
         vIndices[j] = object->faces[i][j][0] - 1;
         tIndices[j] = object->faces[i][j][1] - 1;
      }
      
      for(j = 0; j < 3; ++j) {
         vec3 V[3];
         vec2 T[3];
         for(k = 0; k < 3; ++k) {
            V[k] = object->vertices[vIndices[(j+k)%3]];
            T[k] = object->textures[tIndices[(j+k)%3]];
         }
         vec3 edge1 = subV3(V[1], V[0]);
         vec3 edge2 = subV3(V[2], V[0]);
         vec2 deltaUV1 = subV2(T[1], T[0]);
         vec2 deltaUV2 = subV2(T[2], T[0]);
         GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);

         vec3 tangent;
         tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
         tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
         tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
         tangent = unit(tangent);

         vec3 bitangent;
         bitangent.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
         bitangent.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
         bitangent.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
         bitangent = unit(bitangent);

         object->tangents[vIndices[j]] = addV3(object->tangents[vIndices[j]], tangent);
         object->bitangents[vIndices[j]] = addV3(object->bitangents[vIndices[j]], bitangent);
      }

      for(j = 0; j < 3; ++j) ++object->shared[vIndices[j]];
   }
   for(i = 0; i < object->vertexCount; ++i) {
      object->tangents[i] = unit(scale(object->tangents[i], 1.0f / object->shared[i])); 
      object->bitangents[i] = unit(scale(object->bitangents[i], 1.0f / object->shared[i])); 
   }
}