Esempio n. 1
0
void ElysiumEngine::saveObjToBin(const char *filename, const char *outputFile)
{
    
    Mesh *mesh = loadMeshFromObj(filename);
    
    FILE *output = fopen(outputFile,"wb");
    
    fwrite(&mesh->vertexCount, sizeof(unsigned int),1,output);
    fwrite(&mesh->indexCount, sizeof(unsigned int),1, output);
    
    fwrite(mesh->vertices,sizeof(VertexData),mesh->vertexCount,output);
    fwrite(mesh->indices,sizeof(unsigned int), mesh->indexCount * 3,output);
    
    fclose(output);
}
Esempio n. 2
0
void init()
{
	setupGL();
  Surface::init();

	cam = new Camera();
  mdl = new Model();

  Mesh mesh = loadMeshFromObj("resources/meshes/dragon.obj", 0.1);
  Geometry dragon = createGeometryFromMesh(mesh);

  Surface *surface = new Surface();
  //surface->loadDiffuseTexture("resources/meshes/textures/sponza_floor_a_spec.tga");
  surfaces.insert(std::pair<std::string, Surface*> (std::string("default"), surface) );

  mdl->addGeometryAndSurface(&dragon, surface);

  // Geometry floor;

  // Geometry::sVertex v;
  // v.position = vec3(-1, 0,-1); v.texCoord = vec2(0,0); floor.addVertex(v);
  // v.position = vec3( 1, 0,-1); v.texCoord = vec2(1,0); floor.addVertex(v);
  // v.position = vec3( 1, 0, 1); v.texCoord = vec2(1,1); floor.addVertex(v);
  // v.position = vec3(-1, 0, 1); v.texCoord = vec2(0,1); floor.addVertex(v);

  // floor.addTriangle(uvec3(0,2,1));
  // floor.addTriangle(uvec3(0,3,2));

  // mdl->addGeometryAndSurface(&floor, surface);

  model = new Geometry();

  mesh = loadMeshFromObj("resources/meshes/sponza.obj", 0.01f);
  *model = createGeometryFromMesh(mesh);
  model->createStaticBuffers();

  std::vector<Mesh> meshes = loadMeshesFromObj("resources/meshes/sponza.obj", 0.01f);
  std::vector<Geometry> geometries = createGeometryFromMesh(meshes);

  std::vector<Material> materials = loadMaterialsFromMtl("resources/meshes/sponza.mtl");
  surfaces = createSurfaceFromMaterial(materials, "resources/meshes/");

  for(unsigned int i=0; i<geometries.size(); ++i)
  {
    mdl->addGeometryAndSurface(&geometries[i], surfaces[geometries[i].material]);
  }

  mdl->prepare();

  fsquad = new Geometry();

  Geometry::sVertex v;
  v.position = vec3(-1,-1, 0); v.texCoord = vec2(0,0); fsquad->addVertex(v);
  v.position = vec3( 1,-1, 0); v.texCoord = vec2(1,0); fsquad->addVertex(v);
  v.position = vec3( 1, 1, 0); v.texCoord = vec2(1,1); fsquad->addVertex(v);
  v.position = vec3(-1, 1, 0); v.texCoord = vec2(0,1); fsquad->addVertex(v);

  fsquad->addTriangle(uvec3(0,1,2));
  fsquad->addTriangle(uvec3(0,2,3));
  fsquad->createStaticBuffers();

	geometryShader = new Shader("resources/shaders/geometry_vert.glsl",
							"resources/shaders/geometry_frag.glsl");

  geometryBackShader = new Shader("resources/shaders/geometry_vert.glsl",
              "resources/shaders/geometry_back_frag.glsl");

  hbaoHalfShader = new Shader("resources/shaders/fullscreen_vert.glsl",
              "resources/shaders/hbao_frag.glsl");

  hbaoFullShader = new Shader("resources/shaders/fullscreen_vert.glsl",
              "resources/shaders/hbao_full_frag.glsl");

	compositShader = new Shader("resources/shaders/fullscreen_vert.glsl",
								"resources/shaders/composit_frag.glsl");

  blurXShader = new Shader("resources/shaders/fullscreen_vert.glsl",
                "resources/shaders/blur_x_frag.glsl");

  blurYShader = new Shader("resources/shaders/fullscreen_vert.glsl",
                "resources/shaders/blur_y_frag.glsl");

  downsampleShader = new Shader("resources/shaders/fullscreen_vert.glsl",
                    "resources/shaders/downsample_depth_frag.glsl");

  upsampleShader = new Shader("resources/shaders/fullscreen_vert.glsl",
                    "resources/shaders/upsample_aoz_frag.glsl");

  // Full res deferred base
  fboFullRes = new Framebuffer2D(WIDTH, HEIGHT);
  fboFullRes->attachBuffer(FBO_DEPTH, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, GL_LINEAR, GL_LINEAR);
  fboFullRes->attachBuffer(FBO_AUX0, GL_RGBA8, GL_RGBA, GL_FLOAT);
  fboFullRes->attachBuffer(FBO_AUX1, GL_RG16F, GL_RG, GL_FLOAT, GL_LINEAR, GL_LINEAR);
  fboFullRes->attachBuffer(FBO_AUX2, GL_RG16F, GL_RG, GL_FLOAT, GL_LINEAR, GL_LINEAR);
  //fboFullRes->attachBuffer(FBO_AUX3, GL_R8, GL_RED, GL_FLOAT);

  // Half res buffer for AO
  fboHalfRes = new Framebuffer2D(AO_WIDTH, AO_HEIGHT);
  //fboHalfRes->attachBuffer(FBO_DEPTH, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
  fboHalfRes->attachBuffer(FBO_AUX0, GL_R32F, GL_RED, GL_FLOAT, GL_LINEAR, GL_LINEAR);
  fboHalfRes->attachBuffer(FBO_AUX1, GL_R8, GL_RED, GL_FLOAT, GL_LINEAR, GL_LINEAR);


  float fovRad = cam->getFov() * 3.14159265f / 180.0f;

  vec2 FocalLen, InvFocalLen, UVToViewA, UVToViewB, LinMAD;

  FocalLen[0]      = 1.0f / tanf(fovRad * 0.5f) * ((float)AO_HEIGHT / (float)AO_WIDTH);
  FocalLen[1]      = 1.0f / tanf(fovRad * 0.5f);
  InvFocalLen[0]   = 1.0f / FocalLen[0];
  InvFocalLen[1]   = 1.0f / FocalLen[1];

  UVToViewA[0] = -2.0f * InvFocalLen[0];
  UVToViewA[1] = -2.0f * InvFocalLen[1];
  UVToViewB[0] =  1.0f * InvFocalLen[0];
  UVToViewB[1] =  1.0f * InvFocalLen[1];

  float near = cam->getNear(), far = cam->getFar();
  LinMAD[0] = (near-far)/(2.0f*near*far);
  LinMAD[1] = (near+far)/(2.0f*near*far);

  hbaoHalfShader->bind();
  int pos;
  pos = hbaoHalfShader->getUniformLocation("FocalLen");
  glUniform2f(pos, FocalLen[0], FocalLen[1]);
  pos = hbaoHalfShader->getUniformLocation("UVToViewA");
  glUniform2f(pos, UVToViewA[0], UVToViewA[1]);
  pos = hbaoHalfShader->getUniformLocation("UVToViewB");
  glUniform2f(pos, UVToViewB[0], UVToViewB[1]);
  pos = hbaoHalfShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);

  pos = hbaoHalfShader->getUniformLocation("AORes");
  glUniform2f(pos, (float)AO_WIDTH, (float)AO_HEIGHT);
  pos = hbaoHalfShader->getUniformLocation("InvAORes");
  glUniform2f(pos, 1.0f/(float)AO_WIDTH, 1.0f/(float)AO_HEIGHT);

  pos = hbaoHalfShader->getUniformLocation("R");
  glUniform1f(pos, AO_RADIUS);
  pos = hbaoHalfShader->getUniformLocation("R2");
  glUniform1f(pos, AO_RADIUS*AO_RADIUS);
  pos = hbaoHalfShader->getUniformLocation("NegInvR2");
  glUniform1f(pos, -1.0f / (AO_RADIUS*AO_RADIUS));
  pos = hbaoHalfShader->getUniformLocation("MaxRadiusPixels");
  glUniform1f(pos, AO_MAX_RADIUS_PIXELS / (float)RES_RATIO);

  pos = hbaoHalfShader->getUniformLocation("NoiseScale");
  glUniform2f(pos, (float)AO_WIDTH/(float)NOISE_RES, (float)AO_HEIGHT/(float)NOISE_RES);
  pos = hbaoHalfShader->getUniformLocation("NumDirections");
  glUniform1i(pos, AO_DIRS);
  pos = hbaoHalfShader->getUniformLocation("NumSamples");
  glUniform1i(pos, AO_SAMPLES);

  hbaoFullShader->bind();
  pos = hbaoFullShader->getUniformLocation("FocalLen");
  glUniform2f(pos, FocalLen[0], FocalLen[1]);
  pos = hbaoFullShader->getUniformLocation("UVToViewA");
  glUniform2f(pos, UVToViewA[0], UVToViewA[1]);
  pos = hbaoFullShader->getUniformLocation("UVToViewB");
  glUniform2f(pos, UVToViewB[0], UVToViewB[1]);
  pos = hbaoFullShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);

  pos = hbaoFullShader->getUniformLocation("AORes");
  glUniform2f(pos, (float)WIDTH, (float)HEIGHT);
  pos = hbaoFullShader->getUniformLocation("InvAORes");
  glUniform2f(pos, 1.0f/(float)WIDTH, 1.0f/(float)HEIGHT);

  pos = hbaoFullShader->getUniformLocation("R");
  glUniform1f(pos, AO_RADIUS);
  pos = hbaoFullShader->getUniformLocation("R2");
  glUniform1f(pos, AO_RADIUS*AO_RADIUS);
  pos = hbaoFullShader->getUniformLocation("NegInvR2");
  glUniform1f(pos, -1.0f / (AO_RADIUS*AO_RADIUS));
  pos = hbaoFullShader->getUniformLocation("MaxRadiusPixels");
  glUniform1f(pos, AO_MAX_RADIUS_PIXELS);

  pos = hbaoFullShader->getUniformLocation("NoiseScale");
  glUniform2f(pos, (float)WIDTH/(float)NOISE_RES, (float)HEIGHT/(float)NOISE_RES);
  pos = hbaoFullShader->getUniformLocation("NumDirections");
  glUniform1i(pos, AO_DIRS);
  pos = hbaoFullShader->getUniformLocation("NumSamples");
  glUniform1i(pos, AO_SAMPLES);

  blurXShader->bind();
  pos = blurXShader->getUniformLocation("AORes");
  glUniform2f(pos, AO_WIDTH, AO_HEIGHT);
  pos = blurXShader->getUniformLocation("InvAORes");
  glUniform2f(pos, 1.0f/AO_WIDTH, 1.0f/AO_HEIGHT);
  pos = blurXShader->getUniformLocation("FullRes");
  glUniform2f(pos, WIDTH, HEIGHT);
  pos = blurXShader->getUniformLocation("InvFullRes");
  glUniform2f(pos, 1.0f/WIDTH, 1.0f/HEIGHT);
  pos = blurXShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);

  blurYShader->bind();
  pos = blurYShader->getUniformLocation("AORes");
  glUniform2f(pos, AO_WIDTH, AO_HEIGHT);
  pos = blurYShader->getUniformLocation("InvAORes");
  glUniform2f(pos, 1.0f/AO_WIDTH, 1.0f/AO_HEIGHT);
  pos = blurYShader->getUniformLocation("FullRes");
  glUniform2f(pos, WIDTH, HEIGHT);
  pos = blurYShader->getUniformLocation("InvFullRes");
  glUniform2f(pos, 1.0f/WIDTH, 1.0f/HEIGHT);
  pos = blurYShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);

  downsampleShader->bind();
  pos = downsampleShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);
  pos = downsampleShader->getUniformLocation("ResRatio");
  glUniform1i(pos, RES_RATIO);

  upsampleShader->bind();
  pos = upsampleShader->getUniformLocation("LinMAD");
  glUniform2f(pos, LinMAD[0], LinMAD[1]);

  glGenTextures(1, &noiseTexture);
  glBindTexture(GL_TEXTURE_2D, noiseTexture);
  generateNoiseTexture(NOISE_RES, NOISE_RES);

}