Exemplo n.º 1
0
void SCANLINE::Compute_Poly_Normal()
{//compute polygon normal, vertices normal and illumination model
    vector< vector<float> >vertex_normal(world_vertex.size(),vector<float>(3));
    vector< vector<float> >temp_Light_Intensity//vertex intensity
            (world_vertex.size(),vector<float>(3));
    vector <float> I_light(3);//light intensity
    I_light[0] = 0.8;//R
    I_light[1] = 0.0;//G
    I_light[2] = 0.0;//B

    vector <float> Intensity_diffuse(3);
    vector <float> Intensity_specular(3);
    vector <float> Intensity_ambient(3);
    vector <float> poly_vector_1(3);
    vector <float> poly_vector_2(3);
    vector <float> poly_normal(3);
    vector <float> H(3);
    vector <float> temp_H(3);
    //calculus H
    vector_summation(temp_H, vec_light,vec_view);
    for (int j=0; j<3 ;j++ )
        H[j] = temp_H[j]/vector_length(temp_H);
    ///---Intensity ambient----Ka*I
    for (int j=0; j<3 ;j++ )
        Intensity_ambient[j] = K_ambient*I_light[j];
    for(int i=0; i<(int)Poly.size(); i++)
    {//compute polygon normal
        vector_subtraction(poly_vector_1,
                           world_vertex[Poly[i][2]-1],
                           world_vertex[Poly[i][1]-1]);
        vector_subtraction(poly_vector_2,
                           world_vertex[Poly[i][2]-1],
                           world_vertex[Poly[i][3]-1]);
        //compute 2 vector on one polygon
        cross_product3D(poly_normal, poly_vector_1, poly_vector_2);
        //calculus cross product
//        float temp_length = vector_length(poly_normal);
//        for (int j=0; j<3 ;j++ )//(normalized)
//        	poly_normal[j] = poly_normal[j]/ temp_length;

        for(int j=1; j<(int)Poly[i][0]+1 ;j++)
        {//add normal of neibor polygon to vertex
            for (int p=0; p<3 ;p++ )
                vertex_normal[Poly[i][j]-1][p] += poly_normal[p];
        }

    }
    for(int i=0; i<(int)vertex_normal.size() ;i++ )
    {
        float temp_length = vector_length(vertex_normal[i]);
        for (int j=0; j<3 ;j++ )
        {   //normalize the normal

            vertex_normal[i][j] = vertex_normal[i][j] / temp_length;
            ///----------Intensity diffuse----Kd*I*(NL)
            Intensity_diffuse[j] =
                K_diffuse*I_light[j]*dot_product3D(vertex_normal[i],vec_light);
            ///----------Intensity specular----Ks*I*(NH)^n
            Intensity_specular[j] =
                K_specular*I_light[j]*pow(dot_product3D(vertex_normal[i],H),8);

            //---------------sum of all intensity
            temp_Light_Intensity[i][j]= Intensity_diffuse[j] +
                Intensity_specular[j] + Intensity_ambient[j];
        }

    }
    Light_Intensity = temp_Light_Intensity;
    //Light_Intensity match to vertex index

}
Exemplo n.º 2
0
int main(int argc, char **argv) {

	vector<vertex> vertices;
	vector<triangle> triangles;

	read_obj_file("cube.obj", &vertices, &triangles);

	int T = triangles.size();

	instanceVertexCount = T * 3;
	instanceVertices = (GLfloat*)malloc((sizeof(GLfloat)) * 3 * instanceVertexCount);
	instanceNormals = (GLfloat*)malloc((sizeof(GLfloat)) * 3 * instanceVertexCount);

	vertex* verts = (vertex*)instanceVertices;
	vertex* norms = (vertex*)instanceNormals;

	for (int i = 0; i < T; ++i)
	{
		triangle t = triangles[i];
		vertex v1 = vertices[t.i1];
		vertex v2 = vertices[t.i2];
		vertex v3 = vertices[t.i3];
		verts[i * 3 + 0] = v1;
		verts[i * 3 + 1] = v2;
		verts[i * 3 + 2] = v3;
		vertex n = vertex_normal(v1, v2, v3);
		norms[i * 3 + 0] = n;
		norms[i * 3 + 1] = n;
		norms[i * 3 + 2] = n;

	}

	for (int i = 0; i < T * 9; ++i) {

		instanceVertices[i] *= CUBE_SCALE;

	}

	/*GLuint tex_cube = SOIL_load_OGL_cubemap
		(
		"posx.jpg",
		"negx.jpg",
		"posy.jpg",
		"negy.jpg",
		"posz.jpg",
		"negz.jpg",
		SOIL_LOAD_RGB,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS
		);*/


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(300, 32);//optional
	glutInitWindowSize(1024, 768); //optional
	glutCreateWindow("Particle Window!");
	glewInit();

	init();

	// register callbacks
	glutKeyboardFunc(keyboard);
	glutTimerFunc(FRAME_MSEC, onFrame, 0);
	//glutDisplayFunc(display);

	printf("OpenGL version supported by this platform (%s): \n", (char const*)glGetString(GL_VERSION));

	glutMainLoop();

	return 0;
}
Exemplo n.º 3
0
void vertex_normal( Mesh& m, const Vector& n )
{
    vertex_normal(m, make_vec3(n.x, n.y, n.z));
}
Exemplo n.º 4
0
unsigned int push_vertex( Mesh& m, const Point& position, const Vector& normal )
{
    vertex_normal(m, normal);
    return push_vertex(m, position);
}
Exemplo n.º 5
0
unsigned int push_vertex( Mesh& m, const vec3& position, const vec3& normal )
{
    vertex_normal(m, normal);
    return push_vertex(m, position);
}
Exemplo n.º 6
0
unsigned int push_vertex( Mesh& m, const Point& position, const float u, const float v, const Vector& normal )
{
    vertex_texcoord(m, u, v);
    vertex_normal(m, normal);
    return push_vertex(m, position);
}