Back Face Removal Opengl Program in C

Back Face Removal Opengl Program in C

clipping-alorithm-sutherland-in-opengl

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

double xmin=-454.0,ymin=-250.0,xmax=454.0,ymax=250.0;
double xvmin=-454.0,yvmin =-250.0, xvmax=454.0,yvmax=250.0;

float Points[8][3]={{-50,-50,50},{50,-50,50},{50,50,50},{-50,50,50},{-50,-50,-50},{50,-50,-50},{50,50,-50},{-50,50,-50}};
int surface[6]={0};
float ViewingPositions[26][3]={{0,0,100},{0,100,100},{0,100,0},{0,100,-100},{0,0,-100},{0,-100,-100},{0,-100,0},{0,-100,100},{0,0,100},{-100,0,100},{-100,0,0},{-100,0,-100},{0,0,-100},{100,0,-100},{100,0,0},{100,0,100},{0,0,100},{100,0,0},{100,-100,0},{0,-100,0},{-100,-100,0},{-100,0,0},{-100,100,0},{0,100,0},{100,100,0},{100,0,0}};

void Axes(void)
{
glColor3f (1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-2000, 0, 0);
glVertex3f( 2000, 0, 0);
glEnd();
glBegin(GL_LINES);
glVertex3f(0, -2000, 0);
glVertex3f(0, 2000, 0);
glEnd();
glBegin(GL_LINES);
glVertex3f(0, 0, -2000);
glVertex3f(0, 0, 2000);
glEnd();
}

void Draw(float a[8][3])
{
int i;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Axes();
    if(surface[0]==1){
        glColor3f (1.0, 0.0, 0.0);
        glBegin(GL_POLYGON);
glVertex3f(a[0][0],a[0][1],a[0][2]);
glVertex3f(a[1][0],a[1][1],a[1][2]);
glVertex3f(a[2][0],a[2][1],a[2][2]);
glVertex3f(a[3][0],a[3][1],a[3][2]);
glEnd();
    }

    i=0;
    if(surface[1]==1){
        glColor3f (0.0, 1.0, 0.0);
        glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
        glEnd();
    }

    if(surface[2]==1){
        glColor3f (0.0, 0.0, 1.0);
        glBegin(GL_POLYGON);
glVertex3f(a[0][0],a[0][1],a[0][2]);
glVertex3f(a[3][0],a[3][1],a[3][2]);
glVertex3f(a[7][0],a[7][1],a[7][2]);
glVertex3f(a[4][0],a[4][1],a[4][2]);
        glEnd();
    }

i=1;
    if(surface[3]==1){
        glColor3f (1.0, 1.0, 0.0);
        glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
        glEnd();
    }

i=2;
    if(surface[4]==1){
        glColor3f (1.0, 0.0, 1.0);
        glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
        glEnd();
    }

i=4;
    if(surface[5]==1){
        glColor3f (0.0, 1.0, 1.0);
        glBegin(GL_POLYGON);
glVertex3f(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3f(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3f(a[2+i][0],a[2+i][1],a[2+i][2]);
glVertex3f(a[3+i][0],a[3+i][1],a[3+i][2]);
        glEnd();
    }
}

void ROX()
{
glRotatef(45.0, 1.0, 0.0, 0.0);
Draw(Points);
    glFlush();
}

void ROY()
{
glRotatef(45.0, 0.0, 1.0, 0.0);
Draw(Points);
    glFlush();
}
void ROZ()
{
glRotatef(45.0, 0.0, 0.0, 1.0);
Draw(Points);
    glFlush();
}

void XZ()
{
    glRotatef(270.0, 0.0, 1.0, 0.0);
Draw(Points);
    glFlush();
}

void XZ1()
{
    glRotatef(90.0, 0.0, 1.0, 0.0);
Draw(Points);
    glFlush();
}

float *crossproduct(float *a, float *b){
    float *c;
    c = (float *)malloc(3*sizeof(float));
    c[0] = a[1]*b[2] - a[2]*b[1];
    c[1] = -a[0]*b[2] + a[2]*b[0];
    c[2] = a[0]*b[1] - a[1]*b[0];
    return c;
}

float dotproduct(float *a, float *b){
    float value;
    value = a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
    return value;
}

void BackFaceRemoval(int i){
    float *a,*b;
    int j;

    a = (float *)malloc(3*sizeof(float));
    b = (float *)malloc(3*sizeof(float));

    //Check for surface 1
    a[0] = Points[0][0]-Points[1][0];
    a[1] = Points[0][1]-Points[1][1];
    a[2] = Points[0][2]-Points[1][2];
    b[0] = Points[2][0]-Points[1][0];
    b[1] = Points[2][1]-Points[1][1];
    b[2] = Points[2][2]-Points[1][2];

    b = crossproduct(a,b);

    a[0] = Points[4][0]-Points[0][0];
    a[1] = Points[4][1]-Points[0][1];
    a[2] = Points[4][2]-Points[0][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[0][0];
    a[1] = ViewingPositions[i][1]-Points[0][1];
    a[2] = ViewingPositions[i][2]-Points[0][2];

    if(dotproduct(a,b)<0){
        surface[0]=0;
    }
    else{
        surface[0]=1;
    }

    //Check for surface 2
    a[0] = Points[0][0]-Points[1][0];
    a[1] = Points[0][1]-Points[1][1];
    a[2] = Points[0][2]-Points[1][2];
    b[0] = Points[5][0]-Points[1][0];
    b[1] = Points[5][1]-Points[1][1];
    b[2] = Points[5][2]-Points[1][2];

    b = crossproduct(a,b);

    a[0] = Points[3][0]-Points[0][0];
    a[1] = Points[3][1]-Points[0][1];
    a[2] = Points[3][2]-Points[0][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[0][0];
    a[1] = ViewingPositions[i][1]-Points[0][1];
    a[2] = ViewingPositions[i][2]-Points[0][2];

    if(dotproduct(a,b)<0){
        surface[1]=0;
    }
    else{
        surface[1]=1;
    }

    //Check for surface 3
    a[0] = Points[0][0]-Points[3][0];
    a[1] = Points[0][1]-Points[3][1];
    a[2] = Points[0][2]-Points[3][2];
    b[0] = Points[7][0]-Points[3][0];
    b[1] = Points[7][1]-Points[3][1];
    b[2] = Points[7][2]-Points[3][2];

    b = crossproduct(b,a);

    a[0] = Points[2][0]-Points[0][0];
    a[1] = Points[2][1]-Points[0][1];
    a[2] = Points[2][2]-Points[0][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[0][0];
    a[1] = ViewingPositions[i][1]-Points[0][1];
    a[2] = ViewingPositions[i][2]-Points[0][2];

    if(dotproduct(a,b)<0){
        surface[2]=0;
    }
    else{
        surface[2]=1;
    }

    //Check for surface 4
    a[0] = Points[2][0]-Points[1][0];
    a[1] = Points[2][1]-Points[1][1];
    a[2] = Points[2][2]-Points[1][2];
    b[0] = Points[2][0]-Points[6][0];
    b[1] = Points[2][1]-Points[6][1];
    b[2] = Points[2][2]-Points[6][2];

    b = crossproduct(b,a);

    a[0] = Points[4][0]-Points[1][0];
    a[1] = Points[4][1]-Points[1][1];
    a[2] = Points[4][2]-Points[1][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[1][0];
    a[1] = ViewingPositions[i][1]-Points[1][1];
    a[2] = ViewingPositions[i][2]-Points[1][2];

    if(dotproduct(a,b)<0){
        surface[3]=0;
    }
    else{
        surface[3]=1;
    }

    //Check for surface 5
    a[0] = Points[6][0]-Points[2][0];
    a[1] = Points[6][1]-Points[2][1];
    a[2] = Points[6][2]-Points[2][2];
    b[0] = Points[2][0]-Points[3][0];
    b[1] = Points[2][1]-Points[3][1];
    b[2] = Points[2][2]-Points[3][2];

    b = crossproduct(b,a);

    a[0] = Points[4][0]-Points[2][0];
    a[1] = Points[4][1]-Points[2][1];
    a[2] = Points[4][2]-Points[2][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[2][0];
    a[1] = ViewingPositions[i][1]-Points[2][1];
    a[2] = ViewingPositions[i][2]-Points[2][2];

    if(dotproduct(a,b)<0){
        surface[4]=0;
    }
    else{
        surface[4]=1;
    }

    //Check for surface 6
    a[0] = Points[4][0]-Points[5][0];
    a[1] = Points[4][1]-Points[5][1];
    a[2] = Points[4][2]-Points[5][2];
    b[0] = Points[6][0]-Points[5][0];
    b[1] = Points[6][1]-Points[5][1];
    b[2] = Points[6][2]-Points[5][2];

    b = crossproduct(b,a);

    a[0] = Points[2][0]-Points[4][0];
    a[1] = Points[2][1]-Points[4][1];
    a[2] = Points[2][2]-Points[4][2];

    if(dotproduct(a,b)>0){
        b[0]=-b[0];
        b[1]=-b[1];
        b[2]=-b[2];
    }

    a[0] = ViewingPositions[i][0]-Points[4][0];
    a[1] = ViewingPositions[i][1]-Points[4][1];
    a[2] = ViewingPositions[i][2]-Points[4][2];

    if(dotproduct(a,b)<0){
        surface[5]=0;
    }
    else{
        surface[5]=1;
    }

    for(j=0;j<6;j++){
        //printf("%d\t",surface[j]);
    }
    //printf("\n");
}

void Initial()
{
    Draw(Points);
    glFlush();
}

void keyPressed (unsigned char key, int x, int y) {
switch(key){
        case 49 :   ROX();
                    break;
        case 50 :   ROY();
                    break;
        case 51 :   ROZ();
                    break;
}
}

void display(void)
{
Draw(Points);
glFlush();
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 1.0);
glOrtho(-454.0, 454.0, -250.0, 250.0, -250.0, 250.0);
glEnable(GL_DEPTH_TEST);
}


int main (int argc, char *argv)
{
    int i;
glutInit(&argc, &argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (1000, 600);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Hidden Surface Removal");
init ();
glutDisplayFunc(display);
for(i=0; i<26; i++){
        BackFaceRemoval(i);

        if(i==0){
            Initial();
        }
   else if(i==1){
            //Sleep(2000);
            ROX();
   }
        else if(i<=8){
            //Sleep(1000);
            ROX();
        }
        else if(i==9){
            //Sleep(2000);
            ROY();
        }
        else if(i<=16){
            //Sleep(1000);
            ROY();
        }
        else if(i==17){
            //Sleep(2000);
            XZ();
        }
        else if(i<25){
            //Sleep(1000);
            ROZ();
        }
        else if(i==25){
            //Sleep(1000);
            ROZ();
            BackFaceRemoval(1);
            //Sleep(1000);
            XZ1();
        }
}

glutMainLoop();
return 0;
}

Related Posts