Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: i am having problem with this coding?? can someone help me??

  1. #1
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    1

    i am having problem with this coding?? can someone help me??

    i have this coding... i can draw polygon...
    but i need to make translation, scale, rotation, reflection...
    my lecturer don't teach me coding but she ask me to do it...
    really needs help... plsss....


    Code cpp:
    #include <stdio.h>;
    #include <GL/glut.h>;
     
     
    int pixelarray[10][2];
     
    int noofclick=1;
     
     
     
    char  drawline = 'Y';
    float x1,x2,y1,y2,b=0;
    float tempx, tempy;
     
    void setPixel(int x, int y)
    {
            glPointSize(1.0f);
            glBegin(GL_POINTS);
            glVertex2i(x,y);
            glEnd();
            glFlush();
    }
    void setPixel2(int x, int y)
    {
            glPointSize(1.0f);
            glBegin(GL_POINTS);
            glVertex2i(x,480-y);
            glEnd();
            //glFlush();
    }
     
    void setPixel3(int x, int y)
    {
            glColor3f(0.0,0.0,255.0);
            glPointSize(1.0f);
            glBegin(GL_POINTS);
            glVertex2i(x,y);
            glEnd();
            glFlush();
    }
     
    void horizontal()
    {
    if(x1>x2)
    {
    float temp;
    temp = x1;
    x1 = x2;
    x2 = temp;
    }
    for(float x=x1; x<=x2; x++)
    {
    setPixel(x,y1);
    }
    }
     
    void vertical()
    {
    if(y1>y2)
    {
    float temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }
    for(float y=y1; y<=y2; y++)
    {
    setPixel(x1,y);
    }
    }
     
    void bresenham1()
    {
    if(x1>x2)
    {
    float temp;
    temp = x1;
    x1 = x2;
    x2 = temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }
    int x = x1, y = y1;
    int dx = x2-x1;
    int dy = y2-y1;
    int dT = 2*(dy-dx);
    int dS = 2*dy;
    int d = 2*dy-dx;
     
    setPixel(x,y);
    while(x<x2)
    {
    x++;
    if(d<0)
    {
    d = d+dS;
    }
    else
    {
    d = d+dT;
    y++;
    }
    setPixel(x,y);
    }
    setPixel(x2,y2);
    }
     
    void bresenham2()
    {
    if(x1>x2)
    {
    float temp;
    temp = x1;
    x1 = x2;
    x2 = temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }
    int x = x1, y = y1;
    int dx = x2-x1;
    int dy = y2-y1;
    int dT = 2*(dy+dx);
    int dS = 2*dy;
    int d = -(2*dx+dy);
     
    setPixel(x,y);
    while(x<x2)
    {
    x++;
    if(d<0)
    {
    d = d-dS;
    }
    else
    {
    y--;
    d = d-dT;
    }
    setPixel(x,y);
    }
    setPixel(x2,y2);
    }
     
    void bresenham3()
    {
    if(y1>y2)
    {
    float temp;
    temp = x1;
    x1 = x2;
    x2 = temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }
    int x = x1, y = y1;
    int dx = x2-x1;
    int dy = y2-y1;
    int dT = 2*(dx-dy);
    int dS = 2*dx;
    int d = 2*dx-dy;
     
    setPixel(x,y);
    while(y<y2)
    {
    y++;
    if(d<0)
    {
    d = d+dS;
    }
    else
    {
    x++;
    d = d+dT;
    }
    setPixel(x,y);
    }
    setPixel(x2,y2);
    }
     
    void bresenham4()
    {
    if(y1>y2)
    {
    float temp;
    temp = x1;
    x1 = x2;
    x2 = temp;
    temp = y1;
    y1 = y2;
    y2 = temp;
    }
    int x = x1, y = y1;
    int dx = x2-x1;
    int dy = y2-y1;
    int dT = 2*(dy+dx);
    int dS = 2*dx;
    int d = -(2*dy+dx);
     
    setPixel(x,y);
    while(y<y2)
    {
    y++;
    if(d<0)
    {
    d = d-dS;
    }
    else
    {
    x--;
    d = d-dT;
    }
    setPixel(x,y);
    }
    setPixel(x2,y2);
    }
     
    void myMouse(int button, int state, int x, int y)
    {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && drawline == 'Y')
    {
    if(b==0)
    {
    x1 = x;
    y1 = 480-y;
    pixelarray[0][0] = x1;
    pixelarray[0][1] = y1;
     
     
     
     
    b = 1;
    printf("%f %f \n", x1,y1);
    /*int tmpx=x1, tmpy=y2;
    GLubyte pixel_color[3];
    glReadPixels(tmpx, tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
    printf("PIXEL: %d - %d - %d\n", pixel_color[0], pixel_color[1], pixel_color[2]);*/
     
    }
    else if(b==1)
    {
    x2 = x;
    y2 = 480-y;
     
    noofclick = noofclick+1;
    pixelarray[noofclick][0] = x2;
    pixelarray[noofclick][1] = y2;
     
    b = 0;
    printf("%f %f \n", x1,y1);
    /*int tmpx=x1, tmpy=y2;
    GLubyte pixel_color[3];
    glReadPixels(tmpx, tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
    printf("PIXEL: %d - %d - %d\n", pixel_color[0], pixel_color[1], pixel_color[2]);*/
    //tempx=x2;
    //tempy=y2;
     
    if(y1==y2) horizontal();
    else if(x1==x2) vertical();
     
    float m = (y2-y1)/(x2-x1);
     
    if(0<m && m<1) bresenham1();
    else if(0>m && m>-1) bresenham2();
    else if(1<m) bresenham3();
    else if(-1>m) bresenham4();
    }
    }
    }
     
    /*IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII*/
    void filing(int x, int y)
    {
        GLubyte pixel_color[3];
        printf("x=%d y=%d\n",x,480-y);
     
        glReadPixels(x, 480-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
        //printf("PIXEL filling : %d - %d - %d\n", pixel_color[0], pixel_color[1], pixel_color[2]);
     
        //if((pixel_color[0]==255)&&(pixel_color[1]==255)&&(pixel_color[2]==255))
        //{
        //    if((pixel_color[0]!=255)&&(pixel_color[1]!=0)&&(pixel_color[2]!=0))
        //    {
     
        if(((pixel_color[0]==255)&&(pixel_color[1]==255)&&(pixel_color[2]==255)))
        {    
                setPixel3(x,480-y);
                filing((x+1),y);
                filing((x-1),y);
                filing(x,(y+1)); 
                filing(x,(y-1)); 
        }
        //    }
        //}
    }
    /*IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII*/
     
    void myMouse2(int button, int state, int x, int y)
    {
        int tmpx=x, tmpy=y;
        GLubyte pixel_color[3], pixel_colory[3];
        int noCross=0;
     
        while(tmpx>0)
        {
            glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
     
            if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
            {
                printf("\nPIXEL: %d - %d - %d\n", pixel_color[0], pixel_color[1], pixel_color[2]);
                noCross++;
     
                tmpx--;
                tmpx--;
     
            }
            tmpx--;
        }
     
        if(noCross%2==1)
        {
            glColor3f(0.0,0.0,255.0);
            printf("\n%d: You are clicking inside the polygon.", noCross);
            {
                glColor3f(0.0,0.0,255.0);
                tmpx=x, tmpy=y;
                filing(tmpx,tmpy);
            }
                /*while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpx--;
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpx=x;
                    tmpy--;
                    printf("\nTempy==> %d", tmpy);
                }
                tmpy=y,tmpx=x;
                //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpy++;
     
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpy=y;
                    tmpx++;
                    printf("\nTempx==> %d", tmpx);
                }
                        //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
     
            }
            //secondtime-------------------------------------------------------------------------------
            {
                tmpx=x, tmpy=y;
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpx++;
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpx=x;
                    tmpy++;
                    printf("\nTempy==> %d", tmpy);
                }
                tmpy=y,tmpx=x;
                //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpy--;
     
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpy=y;
                    tmpx--;
                    printf("\nTempx==> %d", tmpx);
                }
                        //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
     
            }
            //second time------------------------------------------------------------------------------
     
            //third time-----------------------------------
            {
                tmpx=x, tmpy=y;
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpx++;
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpx=x;
                    tmpy--;
                    printf("\nTempy==> %d", tmpy);
                }
                tmpy=y,tmpx=x;
                //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpy++;
     
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpy=y;
                    tmpx--;
                    printf("\nTempx==> %d", tmpx);
                }
                        //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
     
            }
            //secondtime-------------------------------------------------------------------------------
            {
                tmpx=x, tmpy=y;
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpx--;
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpx=x;
                    tmpy++;
                    printf("\nTempy==> %d", tmpy);
                }
                tmpy=y,tmpx=x;
                //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
                while(1)
                {    
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_colory[0]);
                    if((pixel_colory[0]==255)&&(pixel_colory[1]==0)&&(pixel_colory[2]==0))
                        {
                            //tmpy=0;
                            break;
                        }
                while(1)
                {
                    setPixel2(tmpx,tmpy);
                    tmpy--;
     
     
                    glReadPixels(tmpx, 480-tmpy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE , &pixel_color[0]);
                    if((pixel_color[0]==255)&&(pixel_color[1]==0)&&(pixel_color[2]==0))
                    {
                        //tmpx=0;
                        break;
                    }
     
                }
     
                    tmpy=y;
                    tmpx++;
                    printf("\nTempx==> %d", tmpx);
                }
                        //-----------------------------------------------------------------------------
                glColor3f(0.0,0.0,255.0);
                glFlush();
            }
            //third time-----------------------------------
            */
        }
        else if((noCross%2==0)||(noCross==0))
        {
            printf("\n%d: You are clicking outside the polygon.", noCross);
        }
        noCross=0;
        /*switch(noCross)
        {
        case 0:printf("%d: You are clicking outside the polygon.", noCross);break;
        case 1:printf("%d: You are clicking outside the polygon.", noCross);break;
        }*/
    }
     
     
    void translateobj(int distx, int disty)
    {
        int i=0;
        while (i<noofclick)
        if (i==0)
            x1=myarray[i][0]+distx;
            y1=myarray[i][1]+disty;
     
        else
            x2=myarray[i][0]+distx;
            y2=myarray[i][1]+disty;
            call bresenham;
     
    }
     
    void scaleobj(int factorx, int factory)
    {
     
     
    }
     
    void rotateobj(int angle)
    {
     
     
    }
     
    void reflectobj(char axis)
    {
     
     
    }
     
    void clipobj()
    {
     
     
    }
     
     
    void myKeyboardAct(unsigned char c, int x, int y)
    {
        switch(c)
        {
        case 'f': glutMouseFunc(myMouse2);
                  break;
     
     
        case '1': translateobj(10,15);
            break;
        case '2': translateobj(-5,10);
            break;
        case '3': scaleobj(2,2);
            break;
        case '4': scaleobj(0.5,0.8);
            break;
        case '5': rotateobj(30);
            break;
        case '6': rotateobj(-45);
            break;
        case '7': reflectobj('x');
            break;
        case '8': reflectobj('y');
            break;
        case 'c': clipobj();
            break;
     
        }
    }
     
     
    void myDisplay(void)
    {
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    gluOrtho2D(0.0,640.0,0.0,480.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glutMouseFunc(myMouse);
     
    glFlush();
    }
     
    int main(int argc, char** argv)
    {
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow ("A simple Line");
    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKeyboardAct);
     
    glutMainLoop ( );
    return 0;
    }
    Last edited by Dark Photon; 01-16-2014 at 05:29 PM.

  2. #2
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    1
    Hi all. I'm TeeLaPulak's friend.
    We have tried out a lot of coding but none of them can work.
    Can anyone please guide us? At least give us some idea? :'(
    Honestly our lecturer never taught us how to do,
    and we have struggle for this almost 2 weeks already..

  3. #3
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    2

    Unhappy please..have a mercy

    he has been through a lot of difficulties in 2 weeks...help him.have a mercy

  4. #4
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    2
    wowowoworrrrrrrrrrrrrrrrrrrrrrrrrrr
    Last edited by selphie; 01-16-2014 at 07:36 AM. Reason: oh why

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,098
    For old OpenGL read
    http://nehe.gamedev.net/tutorial/lessons_01__05/22004/

    for newer
    http://www.arcsynthesis.org/gltut/

    We can answer specific question but not general questions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •