Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void stage3()
- {
- if (near == far) return;
- ifstream stage2;
- ofstream stage3;
- stage2.open ("stage2.txt");
- stage3.open ("stage3.txt");
- stage3 << std::fixed;
- stage3 << std::setprecision(7);
- // process input from stage2 and write to stage3
- fov_x = fov_y * aspectRatio;
- double r, t;
- t = near*tan(pi*fov_y/(2*180));
- r = near*tan(pi*fov_x/(2*180));
- matrix proj = matrix::make_identity(4);
- proj.values[0][0] = near/r;
- proj.values[1][1] = near/t;
- proj.values[2][2] = -(far+near)/(far-near);
- proj.values[2][3] = -(2*far*near)/(far-near);
- proj.values[3][3] = 0;
- proj.values[3][2] = -1;
- int tc = 0;
- while(tc < triangleCount)
- {
- tc++;
- homogeneous_point input[3];
- homogeneous_point output[3];
- homogeneous_point outfinal[3];
- for(int i=0; i<3; i++)
- {
- double x,y,z;
- stage2 >> x >> y >> z;
- homogeneous_point h(x,y,z);
- input[i]=h;
- }
- int oput = 0;
- for(int i=0; i<3; i++)
- {
- homogeneous_point s = input[i];
- homogeneous_point p = input[(i+1)%3];
- if(s.z < -near && p.z < -near)
- {
- output[oput] = p;
- oput++;
- }
- // s inside and p outside
- else if(s.z < -near && p.z >= -near)
- {
- homogeneous_point temp(s.x, s.y, -near);
- output[oput] = temp;
- oput++;
- }
- // s outside and p inside
- else if(s.z >= -near && p.z < -near)
- {
- homogeneous_point temp(p.x, p.y, -near);
- output[oput] = temp;
- oput++;
- output[oput] = p;
- oput++;
- }
- // both outside
- else
- {
- }
- }
- //clipping by far plane
- oput = 0;
- for(int i=0; i<3; i++)
- {
- homogeneous_point s = output[i];
- homogeneous_point p = output[(i+1)%3];
- // both inside the clip region
- if(s.z >= -far && p.z >= -far)
- {
- outfinal[oput] = p;
- oput++;
- }
- // s inside and p outside
- else if(s.z >= -far && p.z < -far)
- {
- homogeneous_point temp(s.x, s.y, -far);
- outfinal[oput] = temp;
- oput++;
- }
- // s outside and p inside
- else if(s.z < -far && p.z >= -far)
- {
- homogeneous_point temp(p.x, p.y, -far);
- outfinal[oput] = temp;
- oput++;
- outfinal[oput] = p;
- oput++;
- }
- // both outside
- else
- {
- }
- }
- // final output write to stage3 file
- for(int i=1; i<4; i++)
- {
- homogeneous_point now = proj*outfinal[i%3];
- stage3 << now.x <<" "<<now.y<< " "<<now.z<<" "<<endl;
- }
- stage3<<endl;
- }
- stage3.close();
- stage2.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement