Ejemplo n.º 1
0
void SetMeshes()
{
	Cube cube;
	cube.CalculateNormals();
	cube.CalculateTangents();
	cube.CalculateBitangents();
	cube.Build(XYZ_UV_TBN);
	cube.position = Vector3(-10, 20, 0);

	cubie = RenderableObject(cube, Diffuse, mtlBrickD);
	scene.AddObject(&cubie);

	Sphere sphere(1, 24, 16);
	sphere.CalculateNormals();
	sphere.CalculateTangents();
	sphere.CalculateBitangents();
	sphere.Build(XYZ_UV_TBN);
	sphere.position = Vector3(-14, 20, 0);

	sphereRO = RenderableObject(sphere, BumpPOMSpecular, mtlBrickPOM);
	scene.AddObject(&sphereRO);

	/*Sphere sphere(1, 24, 16);
	sphere.CalculateNormals();
	sphere.Build(XYZ_N);
	sphere.position = Vector3(4, 0, 0);

	sphereRO = RenderableObject(sphere, CubeEM, mtlCubeMap);
	scene.AddObject(&sphereRO);*/
	
	Mesh teapot;
	teapot.LoadDataFromFile("./Models/teapot.obj", OBJ);
	teapot.CalculateUVs(SphereUV);
	teapot.CalculateNormals();
	teapot.Build(XYZ_N);
	teapot.scale = Vector3(0.01f, 0.01f, 0.01f);
	teapot.position = Vector3(-18, 20, 0);

	teapotRO = RenderableObject(teapot, CubeEM, mtlCubeMap);
	scene.AddObject(&teapotRO);
}
Ejemplo n.º 2
0
// Load a file to the mesh
void GLViewer::LoadFile(const char * aFileName, const char * aOverrideTexture)
{
  // Get the file size
  ifstream f(aFileName, ios::in | ios::binary);
  if(f.fail())
    throw runtime_error("Unable to open the file.");
  f.seekg(0, ios_base::end);
  long tmpFileSize = (long) f.tellg();
  f.close();

  // Load the mesh
  cout << "Loading " << aFileName << "..." << flush;
  mTimer.Push();
  Mesh * newMesh = new Mesh();
  try
  {
    ImportMesh(aFileName, newMesh);
  }
  catch(exception &e)
  {
    delete newMesh;
    throw;
  }
  if(mMesh)
    delete mMesh;
  mMesh = newMesh;
  cout << "done (" << int(mTimer.PopDelta() * 1000.0 + 0.5) << " ms)" << endl;

  // Get the file name (excluding the path), and the path (excluding the file name)
  mFileName = ExtractFileName(string(aFileName));
  mFilePath = ExtractFilePath(string(aFileName));

  // The temporary file size is now the official file size...
  mFileSize = tmpFileSize;

  // Set window title
  string windowCaption = string("OpenCTM viewer - ") + mFileName;
  glutSetWindowTitle(windowCaption.c_str());

  // If the file did not contain any normals, calculate them now...
  if(mMesh->mNormals.size() != mMesh->mVertices.size())
  {
    cout << "Calculating normals..." << flush;
    mTimer.Push();
    mMesh->CalculateNormals();
    cout << "done (" << int(mTimer.PopDelta() * 1000.0 + 0.5) << " ms)" << endl;
  }

  // Load the texture
  if(mTexHandle)
    glDeleteTextures(1, &mTexHandle);
  mTexHandle = 0;
  if(mMesh->mTexCoords.size() == mMesh->mVertices.size())
  {
    string texFileName = mMesh->mTexFileName;
    if(aOverrideTexture)
      texFileName = string(aOverrideTexture);
    if(texFileName.size() > 0)
      InitTexture(texFileName.c_str());
    else
      InitTexture(0);
  }

  // Setup texture parameters for the shader
  if(mUseShader)
  {
    glUseProgram(mShaderProgram);

    // Set the uUseTexture uniform
    GLint useTexLoc = glGetUniformLocation(mShaderProgram, "uUseTexture");
    if(useTexLoc >= 0)
      glUniform1i(useTexLoc, glIsTexture(mTexHandle));

    // Set the uTex uniform
    GLint texLoc = glGetUniformLocation(mShaderProgram, "uTex");
    if(texLoc >= 0)
      glUniform1i(texLoc, 0);

    glUseProgram(0);
  }

  // Load the mesh into a displaylist
  if(mDisplayList)
    glDeleteLists(mDisplayList, 1);
  mDisplayList = glGenLists(1);
  glNewList(mDisplayList, GL_COMPILE);
  DrawMesh(mMesh);
  glEndList();

  // Init the camera for the new mesh
  mCameraUp = Vector3(0.0f, 0.0f, 1.0f);
  SetupCamera();
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------
// PreProcessMesh()
//-----------------------------------------------------------------------------
static void PreProcessMesh(Mesh &aMesh, Options &aOptions)
{
  // Nothing to do?
  if((aOptions.mScale == 1.0f) && (aOptions.mUpAxis == uaZ) &&
     (!aOptions.mFlipTriangles) && (!aOptions.mCalcNormals))
    return;

  // Create 3x3 transformation matrices for the vertices and the normals
  Vector3 vX, vY, vZ;
  Vector3 nX, nY, nZ;
  switch(aOptions.mUpAxis)
  {
    case uaX:
      nX = Vector3(0.0f, 0.0f, 1.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(-1.0f, 0.0f, 0.0f);
      break;
    case uaY:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 0.0f, 1.0f);
      nZ = Vector3(0.0f, -1.0f, 0.0f);
      break;
    case uaZ:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(0.0f, 0.0f, 1.0f);
      break;
    case uaNX:
      nX = Vector3(0.0f, 0.0f, -1.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(1.0f, 0.0f, 0.0f);
      break;
    case uaNY:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 0.0f, -1.0f);
      nZ = Vector3(0.0f, 1.0f, 0.0f);
      break;
    case uaNZ:
      nX = Vector3(-1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(0.0f, 0.0f, -1.0f);
      break;
  }
  vX = nX * aOptions.mScale;
  vY = nY * aOptions.mScale;
  vZ = nZ * aOptions.mScale;

  cout << "Processing... " << flush;
  SysTimer timer;
  timer.Push();

  // Update all vertex coordinates
  for(CTMuint i = 0; i < aMesh.mVertices.size(); ++ i)
    aMesh.mVertices[i] = vX * aMesh.mVertices[i].x +
                         vY * aMesh.mVertices[i].y +
                         vZ * aMesh.mVertices[i].z;

  // Update all normals
  if(aMesh.HasNormals() && !aOptions.mNoNormals)
  {
    for(CTMuint i = 0; i < aMesh.mNormals.size(); ++ i)
      aMesh.mNormals[i] = nX * aMesh.mNormals[i].x +
                          nY * aMesh.mNormals[i].y +
                          nZ * aMesh.mNormals[i].z;
  }

  // Flip trianlges?
  if(aOptions.mFlipTriangles)
  {
    CTMuint triCount = aMesh.mIndices.size() / 3;
    for(CTMuint i = 0; i < triCount; ++ i)
    {
      CTMuint tmp = aMesh.mIndices[i * 3];
      aMesh.mIndices[i * 3] = aMesh.mIndices[i * 3 + 1];
      aMesh.mIndices[i * 3 + 1] = tmp;
    }
  }

  // Calculate normals?
  if((!aOptions.mNoNormals) && aOptions.mCalcNormals &&
     (!aMesh.HasNormals()))
    aMesh.CalculateNormals();

  double dt = timer.PopDelta();
  cout << 1000.0 * dt << " ms" << endl;
}