Beispiel #1
0
Intersection SquarePlane::GetIntersection(Ray r)
{
    //Transform the ray
    Ray r_loc = r.GetTransformedCopy(transform.invT());
    Intersection result;

    //Ray-plane intersection
    float t = glm::dot(glm::vec3(0,0,1), (glm::vec3(0.5f, 0.5f, 0) - r_loc.origin)) / glm::dot(glm::vec3(0,0,1), r_loc.direction);
    glm::vec4 P = glm::vec4(t * r_loc.direction + r_loc.origin, 1);
    //Check that P is within the bounds of the square
    if(t > 0 && P.x >= -0.5f && P.x <= 0.5f && P.y >= -0.5f && P.y <= 0.5f)
    {
        result.point = glm::vec3(transform.T() * P);
        result.normal = glm::normalize(glm::vec3(transform.invTransT() * glm::vec4(ComputeNormal(glm::vec3(P)), 0)));
        result.object_hit = this;
        result.t = glm::distance(result.point, r.origin);
        result.texture_color = Material::GetImageColorInterp(GetUVCoordinates(glm::vec3(P)), material->texture);
        // Store the tangent and bitangent
        glm::vec3 tangent;
        glm::vec3 bitangent;
        ComputeTangents(ComputeNormal(glm::vec3(P)), tangent, bitangent);
        result.tangent = glm::normalize(glm::vec3(transform.T() * glm::vec4(tangent, 0)));
        result.bitangent = glm::normalize(glm::vec3(transform.T() * glm::vec4(bitangent, 0)));

        return result;
    }
    return result;
}
Beispiel #2
0
    // Loads the mesh from an .obj File
    //
    // path     The path route of the file
    void Mesh::LoadMesh(const std::string& path)
    {
        std::vector< glm::vec3 > _vertices;
        std::vector< glm::vec2 > _uvs;
        std::vector< glm::vec3 > _normals;
        std::vector< glm::vec3 > _tangents;
        std::vector< glm::vec3 > _bitangents;

        LoadMeshFromFile(path, _vertices, _uvs, _normals);                
        ComputeTangents (_vertices, _uvs, _normals, _tangents, _bitangents);
        
        // Index every data (vertices, uvs, normals, tangents and bitangents)
        indexVBO_TBN    (_vertices, _uvs, _normals, _tangents, _bitangents, 
                 indices, vertices,  uvs,  normals,  tangents,  bitangents);

        InitializeGLBuffers();
    }
Beispiel #3
0
Datei: Ase.cpp Projekt: RinM/CGA
bool CLoadASE::ImportASE(t3DModel *pModel, char *strFileName)
{
	char strMessage[255] = {0};

	if(!pModel || !strFileName) return false;

	m_FilePointer = fopen(strFileName, "r");

	if(!m_FilePointer) {
		sprintf(strMessage, "Unable to find or open the file: %s", strFileName);
		MessageBox(NULL, strMessage, "Error", MB_OK);
		return false;
	}

	ReadAseFile(pModel);

	ComputeNormals(pModel);
	ComputeTangents(pModel);

	fclose(m_FilePointer);

	return true;
}
Beispiel #4
0
bool ObjectModel::loadOBJ(
	const char * path)
{
	vector< XMFLOAT3 > temp_vertices;
	vector< XMFLOAT2 > temp_uvs;
	vector< XMFLOAT3 > temp_normals;

	FILE * file = fopen(path, "r");
	if (file == NULL)
	{
		printf("Impossible to open the file !\n");
		return false;
	}

	while (true)
	{
		char lineHeader[128];
		// read the first word of the line
		int res = fscanf(file, "%s", lineHeader);
		if (res == EOF)
			break; // EOF = End Of File. Quit the loop.

		// else : parse lineHeader
		if (strcmp(lineHeader, "v") == 0)
		{
			XMFLOAT3 vertex;
			fscanf_s(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
			temp_vertices.push_back(vertex);
		}
		else if (strcmp(lineHeader, "vt") == 0)
		{
			XMFLOAT2 uv;
			fscanf_s(file, "%f %f\n", &uv.x, &uv.y);
			temp_uvs.push_back(uv);
		}
		else if (strcmp(lineHeader, "vn") == 0)
		{
			XMFLOAT3 normal;
			fscanf_s(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
			temp_normals.push_back(normal);
		}
		else if (strcmp(lineHeader, "f") == 0)
		{
			string vertex1, vertex2, vertex3;
			unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
			int matches = fscanf_s(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n",
				&vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1],
				&uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2],
				&normalIndex[2]);
			if (matches != 9)
			{
				printf("File can't be read by our simple parser : ( Try exporting with other options\n");
				return false;
			}
			vertexIndices.push_back(vertexIndex[0]);
			vertexIndices.push_back(vertexIndex[1]);
			vertexIndices.push_back(vertexIndex[2]);
			uvIndices.push_back(uvIndex[0]);
			uvIndices.push_back(uvIndex[1]);
			uvIndices.push_back(uvIndex[2]);
			normalIndices.push_back(normalIndex[0]);
			normalIndices.push_back(normalIndex[1]);
			normalIndices.push_back(normalIndex[2]);
		}
		else
		{
			// kill line
			char commentBuffer[1000];
			fgets(commentBuffer, 1000, file);
		}
	}

	Simple_Vert v;
	v.m_color = XMFLOAT4(1, 1, 1, 0.5f);

	for (unsigned int i = 0; i < vertexIndices.size(); i++)
	{
		unsigned int vertexIndex = vertexIndices[i];
		unsigned int uvIndex = uvIndices[i];
		unsigned int normIndex = vertexIndices[i];

		v.m_vect = XMFLOAT4(temp_vertices[vertexIndex - 1].x, temp_vertices[vertexIndex - 1].y, temp_vertices[vertexIndex - 1].z, 1.0f);
		XMFLOAT2 uv = temp_uvs[uvIndex - 1];
		XMFLOAT3 norm = temp_normals[normIndex - 1];

		StrideStruct _ss;
		_ss.m_vect = v.m_vect;
		uv.y = 1 - uv.y;
		_ss.v_uvs = uv;

		_ss.v_normals = norm;
		m_stride.push_back(_ss);

		v_vertices.push_back(v);

		vertexIndices[i] = i;
	}
	ComputeTangents();
	return true;
}