Ejemplo n.º 1
0
void CreateGridMesh(
    const VoxelGrid<Discretizer>& vg,
    std::vector<Vector3>& vertices)
{
    std::vector<Vector3> voxel_mesh;
    CreateBoxMesh(vg.res().x(), vg.res().y(), vg.res().z(), voxel_mesh);

    vertices.reserve(voxel_mesh.size() * vg.sizeX() * vg.sizeY() * vg.sizeZ());

    for (int x = 0; x < vg.sizeX(); ++x) {
        for (int y = 0; y < vg.sizeY(); ++y) {
            for (int z = 0; z < vg.sizeZ(); ++z) {
                const MemoryCoord mc(x, y, z);
                const WorldCoord wc(vg.memoryToWorld(mc));

                Vector3 wp(wc.x, wc.y, wc.z);

                std::printf("%d, %d, %d -> %0.3f, %0.3f, %0.3f\n", x, y, z, wc.x, wc.y, wc.z);

                // translate all triangles by the voxel position
                for (const Vector3& v : voxel_mesh) {
                    vertices.push_back(v + wp);
                }
            }
        }
    }
}
Ejemplo n.º 2
0
  csRef<iMeshWrapper> DynamicsDebugger::CreateColliderMesh
    (iDynamicsSystemCollider* collider, iMaterialWrapper* material)
  {
    csRef<iMeshWrapper> mesh;

    switch (collider->GetGeometryType ())
      {
      case BOX_COLLIDER_GEOMETRY:
	{
	  // Get box geometry
	  csVector3 boxSize;
	  collider->GetBoxGeometry (boxSize);
	  boxSize /= 2.0;
	  const csBox3 box(-boxSize, boxSize);

	  // Create box
	  mesh = CreateBoxMesh (box, material, collider->GetLocalTransform (),
				sector);
	}
	break;

      case SPHERE_COLLIDER_GEOMETRY:
	{
	  // Get sphere geometry
	  csSphere sphere;
	  collider->GetSphereGeometry (sphere);

	  // Create sphere
	  mesh = CreateSphereMesh (sphere, material, sector);
	}
	break;

      case CYLINDER_COLLIDER_GEOMETRY:
	{
	  // Get cylinder geometry
	  float length, radius;
	  collider->GetCylinderGeometry (length, radius);

	  // Create cylinder
	  mesh = CreateCylinderMesh (length, radius, material,
				     collider->GetLocalTransform (), sector);
	}
	break;

      case CAPSULE_COLLIDER_GEOMETRY:
	{
	  // Get capsule geometry
	  float length, radius;
	  collider->GetCapsuleGeometry (length, radius);

	  // Create capsule
	  mesh = CreateCapsuleMesh (length, radius, material,
				    collider->GetLocalTransform (), sector);
	}
	break;

      case TRIMESH_COLLIDER_GEOMETRY:
	{
	  // Get mesh geometry
	  csVector3* vertices = 0;
	  int* indices = 0;
	  size_t vertexCount, triangleCount;
	  collider->GetMeshGeometry (vertices, vertexCount, indices, triangleCount);

	  // Create mesh
	  mesh = CreateCustomMesh (vertices, vertexCount, indices, triangleCount,
				   material, collider->GetLocalTransform (), sector);
	}
	break;

      case CONVEXMESH_COLLIDER_GEOMETRY:
	{
	  // Get mesh geometry
	  csVector3* vertices = 0;
	  int* indices = 0;
	  size_t vertexCount, triangleCount;
	  collider->GetConvexMeshGeometry (vertices, vertexCount, indices, triangleCount);

	  // Create mesh
	  mesh = CreateCustomMesh (vertices, vertexCount, indices, triangleCount, material,
				   collider->GetLocalTransform (), sector);
	}
	break;

      default:
	// TODO: plan meshes
	break;
      }

    return mesh;
  }
//-------------------------------------------------------------------------
FKCW_3D_Mesh* CreateBoxMesh(float r)
{
	return CreateBoxMesh(r,
		FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1),
		FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1),FKCW_3D_Vector4(1, 1, 1, 1));
}
bool CameraControlApplication::Init() {
  // Initial camera setup.
  const float aspectRatio =
      static_cast<float>(GetClientSize().width) / GetClientSize().height;
  _camera = Camera(Vector3(0.0f, 0.0f, 4.0f),
                   Vector3(0.0f, 0.0f, 0.0f),
                   60.0f, aspectRatio, 0.1f, 100.0f);

  // Setup OpenGL state.
  glEnable(GL_CULL_FACE);
  glFrontFace(GL_CCW);

  // Create geometry.
  _boxMesh = CreateBoxMesh(1.0f, 1.0f, 1.0f, true, true);

  // Create a vertex-array-object and record the vertex-format.
  glGenVertexArrays(1, &_vaoTrianglePositions);
  glBindVertexArray(_vaoTrianglePositions);
  CHECK_GL_ERROR;

    // Create a vertex-buffer-object to be associated with the bound vertex-array-object.
    glGenBuffers(1, &_vboTrianglePositions);
    glBindBuffer(GL_ARRAY_BUFFER, _vboTrianglePositions);
    // Fill vertex buffer with data.
    glBufferData(GL_ARRAY_BUFFER, 
                 sizeof(Vector3) * _boxMesh->GetVertexCount(), 
                 _boxMesh->GetPositions(), GL_STATIC_DRAW);
    CHECK_GL_ERROR;

    glGenBuffers(1, &_eboTriangles);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eboTriangles);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 
                 sizeof(Triangle) * _boxMesh->GetTriangleCount(), 
                 _boxMesh->GetTriangles(), GL_STATIC_DRAW);
    CHECK_GL_ERROR;

    // Setup vertex format.
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

  glBindVertexArray(0);

  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

  // Create the program.
  bool success = _program.CreateVertexFragmentProgramFromFile(
      "../resources/shaders/CameraControlVS.glsl", 
      "../resources/shaders/CameraControlFS.glsl");
  // Get uniforms (shader plumbing).
  if (success) {
    // Connect matrix uniformblock.
    Matrix4 modelviewProjectionMat = _camera.PerspectiveProjectionMatrix() * _camera.ViewMatrix();
    Matrix4 modelviewMat = _camera.ViewMatrix();
    Matrix4 projectionMat = _camera.PerspectiveProjectionMatrix();

    std::vector<UniformBlock::Entry> matrixUniforms = {
        {"dummyMat", kDummyMat}, 
        {"modelviewMat", kModelviewMat}, 
        {"projectionMat", kProjectionMat}, 
        {"modelviewProjectionMat", kModelviewProjectionMat}};
    _matrixUniformBlock.Create(_program.Handle(), "MatrixBlock", matrixUniforms);
    _matrixUniformBlock.SetUniform(kModelviewMat, &modelviewMat.n);
    _matrixUniformBlock.SetUniform(kProjectionMat, &projectionMat.n);
    _matrixUniformBlock.SetUniform(kModelviewProjectionMat, &modelviewProjectionMat.n);
    _matrixUniformBlock.Upload();

    // Fragment shader just has a single uniform.
    _fragColorUniform = glGetUniformLocation(_program.Handle(), "fragColor");
    
    success = _fragColorUniform != -1;
  }
  CHECK_GL_ERROR;

  return success;
}