3D to 2D object Transform source code in c++ with OpenGL

Here is the implementation of 3d to 2d conversion algorithm in c.

INPUT:




OUTPUT:




SOURCE CODE:

#include <windows.h>

#include <GL/glut.h>

#include <stdlib.h>

#include <stdio.h>

#include <iostream>

#include <math.h>



using namespace std;



int x,y,z,sx[100],sy[100],sz[100],ex,ey,ez;



void drawLine(int x1, int y1,int x2,int y2)

{

    glBegin(GL_LINES);



    glVertex2i(x1,y1);

    glVertex2i(x2,y2);



    glEnd();

}







static void display(void)

{



    int i,dis,xx[100],yy[100],px,py;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);





    glColor3d(0,255,255);







   /* drawLine(100,200,200,100);

    drawLine(200,100,300,200);

    drawLine(300,200,200,300);

    drawLine(100,200,200,300);*/

    //glReadPixels(200 , 100 , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , boundary_color);



    dis = z;





  /*  xx = dis*sx/(dis + sz); yy = dis*sy/(dis + sz);

    px = dis*ex/(dis + sz); py = dis*ey/(dis + sz);

    cout << dis << " "<<xx<<","<<yy<<"||"<<px<<","<<py<<endl;

    */



    for(i=1;i<=8;i++)

    {

        xx[i] = dis*sx[i]/(dis + sz[i]);

        yy[i] = dis*sy[i]/(dis + sz[i]);

        cout << xx[i] <<" "<<yy[i]<<endl;

    }



    glColor3d(255,0,0);

    drawLine(xx[1],yy[1],xx[2],yy[2]);

    drawLine(xx[1],yy[1],xx[4],yy[4]);

    drawLine(xx[1],yy[1],xx[6],yy[6]);



    drawLine(xx[2],yy[2],xx[3],yy[3]);

    drawLine(xx[2],yy[2],xx[7],yy[7]);



    drawLine(xx[3],yy[3],xx[4],yy[4]);

    drawLine(xx[3],yy[3],xx[8],yy[8]);



    drawLine(xx[4],yy[4],xx[5],yy[5]);



    drawLine(xx[5],yy[5],xx[6],yy[6]);

    drawLine(xx[5],yy[5],xx[8],yy[8]);



    drawLine(xx[6],yy[6],xx[7],yy[7]);



    drawLine(xx[7],yy[7],xx[8],yy[8]);









 //   drawLine(xx,yy,px,py);





    /*drawPoint(200,200);

    glReadPixels(200 , 100 , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , fill_color);*/



    //boundaryFill(200,200);



    glutSwapBuffers();

}



int main(int argc, char *argv[])

{

    int i;



    glutInit(&argc, argv);

    glutInitWindowSize(640,600);

    glutInitWindowPosition(10,10);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);



    cout << "D :"<<endl;

    cin >> z;

    //cout << "Give 2 End point of 3D-line :"<<endl;

    //cin >> sx >> sy >> sz >> ex >> ey >> ez;

    cout << "Input For 3D object"<<endl;

    for(i=1;i<=8;i++)

    {

        cin >> sx[i] >> sy[i] >> sz[i];

        sx[i] *= 250;

        sy[i] *= 250;

        sz[i] *= 250;

        sx[i] += 100;

        sy[i] += 100;

        sz[i] += 100;



    }

    glutCreateWindow("3D to 2D");





    glOrtho(0,640,0,480,0,1);



    glutDisplayFunc(display);





    glutMainLoop();



    return EXIT_SUCCESS;

}





/*

50



0 0 0

1 0 0

1 1 0

0 1 0

0 1 1

0 0 1

1 0 1

1 1 1





*/

Post a Comment