示例#1
0
//----------Plane Class----------//
void Scene::Plane::init_visualization()
{
	m_positions.clear();
	m_colors.clear();
	m_normals.clear();
	m_indices.clear();

	glm::vec3 center(m_value * m_normal);
	glm::vec3 local_x, local_z;
	local_x = glm::cross(m_normal, glm::vec3(0.0f, 0.0f, 1.0f));
	if(glm::length(local_x) < 0.00001f)
		local_x = glm::cross(m_normal, glm::vec3(1.0f, 0.0f, 0.0f));
	local_x = glm::normalize(local_x);
	local_z = glm::normalize(glm::cross(local_x, m_normal));

	glm::vec3 mat_color(0.6f);
	unsigned int slice = 5;

	glm::vec3 vertex(center);
	m_positions.push_back(center);
	m_normals.push_back(m_normal);
	m_colors.push_back(mat_color);

	float delta = 360.0f / slice;
	float radius = 100.0f;
	glm::vec3 local_pos;
	for(float theta = 0.0f; theta < 359.99f; theta += delta)
	{
		local_pos.x = radius * cos(glm::radians(theta));
		local_pos.z = radius * sin(glm::radians(theta));

		vertex = local_pos.x * local_x - local_pos.z * local_z + center;

		m_positions.push_back(vertex);
		m_normals.push_back(m_normal);
		m_colors.push_back(mat_color);
	}
	for(unsigned int i = 0; i < slice; ++i)
	{
		m_indices.push_back(0);
		m_indices.push_back(i + 1);
		m_indices.push_back(i + 2);
	}
	m_indices.push_back(0);
	m_indices.push_back(slice);
	m_indices.push_back(1);
}
//----------OBJMesh Class----------//
void ObjMesh::read_from_file(char* filename)
{
	m_positions.clear();
	m_colors.clear();
	m_normals.clear();
	m_indices.clear();
	glm::vec3 mat_color(0.6); 
	// vertices and color

	std::ifstream infile(filename);
    if(!infile.good())
    {
        printf("Error in loading file %s\n", filename);
        exit(0);
    }
    char buffer[256];
    unsigned int ip0, ip1, ip2;
    unsigned int n0, n1, n2;
    glm::vec3 pos;

    while(!infile.getline(buffer,255).eof())
    {
        buffer[255] = '\0';
        if(buffer[0] == 'v' && (buffer[1] == ' ' || buffer[1] == 32))
        {
            if(sscanf_s(buffer, "v %f %f %f", &pos.x, &pos.y, &pos.z) == 3)
            {
				pos = m_scaling * pos;
                m_positions.push_back(pos);
            }
            else
            {
                printf("Vertex is not in desired format.\n");
                exit(0);
            }
        }
        else if (buffer[0] == 'v' && buffer[1] == 'n' && (buffer[2] == ' ' || buffer[2] == 32))
        {
            // load normals from obj file.
        }
        else if (buffer[0] == 'f' && (buffer[1] == ' ' || buffer[1] == 32))
        {
            if(sscanf_s(buffer, "f %u %u %u", &ip0, &ip1, &ip2) == 3)
			{
				m_indices.push_back(--ip0);
				m_indices.push_back(--ip1);
				m_indices.push_back(--ip2);
			}
            else if(sscanf_s(buffer, "f %u//%u %u//%u %u//%u", &ip0, &n0, &ip1, &n1, &ip2, &n2) == 6)
			{
				m_indices.push_back(--ip0);
				m_indices.push_back(--ip1);
				m_indices.push_back(--ip2);
			}
            else if(sscanf_s(buffer, "f %u/%u %u/%u %u/%u", &ip0, &n0, &ip1, &n1, &ip2, &n2) == 6)
			{
				m_indices.push_back(--ip0);
				m_indices.push_back(--ip1);
				m_indices.push_back(--ip2);
			}
            else
            {
                printf("Triangle indices is not in desired format.\n");
                exit(0);
            }
        }
    }
	// normals

    unsigned int id, size;
    bool vert_norm = (m_normals.size() != m_positions.size());
    if(vert_norm)
        m_normals.resize(m_positions.size(), glm::vec3(0.0f));

    size = m_indices.size();
    glm::uvec3 triangle;
    glm::vec3 p0, p1, p2;
    glm::vec3 norm;
    float phi0, phi1, phi2;
    float pi = glm::radians(180.0f);
    for(id = 0; id < size; id+=3)
    {
		triangle = glm::uvec3(m_indices[id], m_indices[id+1], m_indices[id+2]);
        p0 = m_positions[triangle.x];
        p1 = m_positions[triangle.y];
        p2 = m_positions[triangle.z];
        norm = glm::normalize(glm::cross(p1 - p0, p2 - p0));
        // calculate vertex normal
        if(vert_norm)
        {
            phi0 = std::acos(glm::dot(p1 - p0, p2 - p0) / (glm::length(p1 - p0) * glm::length(p2 - p0)));
            phi1 = std::acos(glm::dot(p0 - p1, p2 - p1) / (glm::length(p0 - p1) * glm::length(p2 - p1)));
            phi2 = pi - phi0 - phi1;

            m_normals[triangle.x] += phi0 * norm;
            m_normals[triangle.y] += phi1 * norm;
            m_normals[triangle.z] += phi2 * norm;
        }
    }
    // re-normalize all normals
    for(std::vector<glm::vec3>::iterator iter = m_normals.begin(); iter != m_normals.end(); ++iter)
	{
        *iter = glm::normalize(*iter);
		m_colors.push_back(mat_color);
		//m_colors.push_back(*iter);
	}
}
示例#3
0
//----------Sphere Class----------//
void Scene::Sphere::init_visualization()
{
	m_positions.clear();
	m_colors.clear();
	m_normals.clear();
	m_indices.clear();

	glm::vec3 mat_color(0.6f);
	unsigned int slice = 24, stack = 10;

	glm::vec3 tnormal(0.0f, 1.0f, 0.0f), tpos;
	tpos = m_center + m_radius * tnormal;

	m_positions.push_back(tpos);
	m_normals.push_back(tnormal);
	m_colors.push_back(mat_color);

	float theta_z, theta_y, sin_z;
	float delta_y = 360.0f / slice, delta_z = 180.0f / stack;
	//loop over the sphere
	for(theta_z = delta_z; theta_z < 179.99f; theta_z += delta_z)
	{
		for(theta_y = 0.0f; theta_y < 359.99f; theta_y += delta_y)
		{
			sin_z = sin(glm::radians(theta_z));

			tnormal.x = sin_z * cos(glm::radians(theta_y));
			tnormal.y = cos(glm::radians(theta_z));
			tnormal.z = -sin_z * sin(glm::radians(theta_y));

			tpos = m_center + m_radius * tnormal;

			m_positions.push_back(tpos);
			m_normals.push_back(tnormal);
			m_colors.push_back(mat_color);
		}
	}
	tnormal = glm::vec3(0.0f, -1.0f, 0.0f);
	tpos = m_center + m_radius * tnormal;

	m_positions.push_back(tpos);
	m_normals.push_back(tnormal);
	m_colors.push_back(mat_color);

	//indices
	unsigned int j = 0, k = 0;
	for(j = 0; j < slice - 1; ++j)
	{
		m_indices.push_back(0);
		m_indices.push_back(j + 1);
		m_indices.push_back(j + 2);
	}
	m_indices.push_back(0);
	m_indices.push_back(slice);
	m_indices.push_back(1);

	for(j = 0; j < stack - 2; ++j)
	{
		for(k = 1 + slice * j; k < slice * (j + 1); ++k)
		{
			m_indices.push_back(k);
			m_indices.push_back(k + slice);
			m_indices.push_back(k + slice + 1);

			m_indices.push_back(k);
			m_indices.push_back(k + slice + 1);
			m_indices.push_back(k + 1);
		}
		m_indices.push_back(k);
		m_indices.push_back(k + slice);
		m_indices.push_back(k + 1);

		m_indices.push_back(k);
		m_indices.push_back(k + 1);
		m_indices.push_back(k + 1 - slice);
	}

	unsigned int bottom_id = (stack - 1) * slice + 1;
	unsigned int offset = bottom_id - slice;
	for(j = 0; j < slice - 1; ++j)
	{
		m_indices.push_back(j + offset);
		m_indices.push_back(bottom_id);
		m_indices.push_back(j + offset + 1);
	}
	m_indices.push_back(bottom_id - 1);
	m_indices.push_back(bottom_id);
	m_indices.push_back(offset);

	if(m_indices.size() != 6 * (stack - 1) * slice)
		printf("indices number not correct!\n");
}
//----------Cube Class----------//
void Cube::init_visualization()
{
    m_positions.clear();
    m_colors.clear();
    m_normals.clear();
    m_indices.clear();

    glm::vec3 mat_color(0.6);

	// front face 012, 321
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,1));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,1));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,1));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,1));
	m_indices.push_back(0);
	m_indices.push_back(1);
	m_indices.push_back(2);
	m_indices.push_back(3);
	m_indices.push_back(2);
	m_indices.push_back(1);

	// back face 654 567
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,-1));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,-1));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,-1));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,0,-1));
	m_indices.push_back(6);
	m_indices.push_back(5);
	m_indices.push_back(4);
	m_indices.push_back(5);
	m_indices.push_back(6);
	m_indices.push_back(7);

	// right face 8 9 10, 11 10 9
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(1,0,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(1,0,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(1,0,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(1,0,0));
	m_indices.push_back(8);
	m_indices.push_back(9);
	m_indices.push_back(10);
	m_indices.push_back(11);
	m_indices.push_back(10);
	m_indices.push_back(9);

	// left face 14 13 12, 13 14 15
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(-1,0,0));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(-1,0,0));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(-1,0,0));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(-1,0,0));
	m_indices.push_back(14);
	m_indices.push_back(13);
	m_indices.push_back(12);
	m_indices.push_back(13);
	m_indices.push_back(14);
	m_indices.push_back(15);

	// top face 16 17 18, 19 18 17
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,1,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,1,0));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,1,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, +m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,1,0));
	m_indices.push_back(16);
	m_indices.push_back(17);
	m_indices.push_back(18);
	m_indices.push_back(19);
	m_indices.push_back(18);
	m_indices.push_back(17);

	// bottom face 22 21 20, 21 22 23
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,-1,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, +m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,-1,0));
	m_positions.push_back(glm::vec3(-m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,-1,0));
	m_positions.push_back(glm::vec3(+m_hf_dims.x, -m_hf_dims.y, -m_hf_dims.z));
	m_colors.push_back(mat_color);
	m_normals.push_back(glm::vec3(0,-1,0));
	m_indices.push_back(22);
	m_indices.push_back(21);
	m_indices.push_back(20);
	m_indices.push_back(21);
	m_indices.push_back(22);
	m_indices.push_back(23);
}