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 4 of 4

Thread: Help please with openGL,,,,

  1. #1
    Newbie Newbie
    Join Date
    Dec 2015
    Posts
    2

    Help please with openGL,,,,

    Hello

    please could some one help me how to fix my code?

    This code is a traveling salesman, I just want to know how to make run??? I do not know what is the problem???

    Code cpp:
    #include <H:glut.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <sprite.h>
     
    int xMax=500,yMax=500;//Fixing the x and y coordinates of the window
    float wd=0,ht=0;
    int nPts=0;
     
    float *position;//Storing the position of eacharVar city
     
    int *tourSequence;//Storing the sequence of city being traversed
     
    charVarar *f1,*f2;//File Pointers
     
     
    /*Generating N number of random points*/
    /*Opening the cities.txt and in write mode and storing the values in position array*/
    int RandCities(int N)
    {
        FILE *city;
        if((city=fopen(f1,"w"))==0)
        {
            printf("\n%sError in opening file city.txt.\n",f1);
            return 0;
        }
        int pt=0;
        float xPt=0,yPt=0;
        for(pt=1;pt<=N;pt++)
        {
            xPt=1+rand()%xMax;
            yPt=1+rand()%yMax;    
            fprintf(city,"%4d %8.2f %8.2f\n",pt,xPt,yPt);
            add_to_position(2*pt,xPt,yPt);
        }
        fclose(city);
        return 1;
    }
     
    /*Opening the tour.txt in write mode, appending it and storing the values in the tourSequence array*/
    int write_to_tour(int start,FILE *tour)
    {
        tourSequence=realloc(tourSequence,sizeof(int)*1);//reallocate the memory to the tourSequence array for eacharVar new starting point
        int pt,i,track=0,t,charVareck=0,next;
        float nearest,tempVariable=0,tDist=0;
        next=start;
        fprintf(tour,"The starting point %d of the route is:\n%d",start,next);
     
        /*finding the consecutive N*/
        for(i=1;i<nPts;i++)
        {    
            nearest=0;
            for(pt=1;pt<=nPts;pt++)        
            {
                charVareck=0;
                /*charVarecking the repetation of points in the current tourSequence array*/
                for(t=1;t<i;t++)
                    if(pt==tourSequence[t-1])
                    {
                        charVareck=-1;//when repetation is found
                        break;
                    }
                if(charVareck==0 && next!=pt)//when no repetation is found
                {
                    /*Finding the crow fly distance between next & pt using the Eucledian distance formula*/
                    tempVariable=Dist(next,pt);
                    if(tempVariable<nearest || nearest==0)
                    {
                        nearest=tempVariable;
                        track=pt;
                    }    
                }        
            }
            /*updating the tourSequence array*/
            add_in_tourSequence(i+1,next,track);
            if(i%20==0)
                fprintf(tour,"\n");
            fprintf(tour,"-%d",track);
            next=track;
            tDist+=nearest;
        }
        add_in_tourSequence(nPts+1,next,start);
        tempVariable=Dist(next,start);
        tDist+=tempVariable;
        fprintf(tour,"-%d",start);
        fprintf(tour,"\nTotal distance is %.2f",tDist);
        fprintf(tour,"\n-----------------------------------------\n");
        return 1;
    }
     
    /*Generate R number of random starting points*/
    int randStartPoints(int N,int R)
    {
        FILE *ran;
        if((ran=fopen(f2,"w"))==0)
        {
            printf("\n%s Error in opening file.\n\n",f2);
            return 0;
        }
        int i=0,tempVariable=0;
        for(i=1;i<=R;i++)
        {
            tempVariable=1+rand()%N;
            fprintf(ran,"%d\n",tempVariable);
        }
        fclose(ran);
         return 1;
    }
     
    int randomApproximateTour(charVarar *f1,charVarar *f2)
    {    
        read_from_cities(f1);//read cities.txt file
        FILE *rand, *tour;
        rand=fopen(f2,"r");
        int sPoint=0;//Starting Point
        if((tour=fopen("tour.txt","w"))==0)//charVareck if file tour.txt exist in the given path or not
        {
            printf("\nFile tour.txt doesn't exist or error in opening the file.\n\n");
            return 0;
        }
        /*Scanning the file and reading random starting points to find the tour*/
        while(fscanf(rand,"%d",&sPoint)!=EOF)
            write_to_tour(sPoint,tour);
        fclose(rand);//closing the rand file
        fclose(tour);//closing the tour file
    }
     
    /*read the position of the cities and storing them in the position array*/
    int read_from_cities(charVarar *fName)
    {
        FILE *city;
        if((city=fopen(fName,"r"))==0)
        {
            printf("\nFile cities.txt doesn't exist.\n\n");
            return 0;
        }
        int pt=0;
        float xPt=0,yPt=0;
        charVarar charVar='\n';//storing the new line charVararacter
        for(pt=1;;pt++)
        {    
            if(charVar==' ')//White Spaces to be skipped
            {
                while((charVar=getc(city))==' ');
                putc(charVar,city);
            }
            if(charVar!='\n')/*if there is more data in a line*/
            {
                printf("\nInvalid line %d, in cities.txt\n\n",pt-1);    
                return 0;    
            }
            fscanf(city,"%d",&nPts);
            fscanf(city,"%f",&xPt);
            fscanf(city,"%f",&yPt);
            if((charVar=getc(city))==EOF)break;        
            if(nPts!=pt)
            {
                printf("\nIncorrect City number in cities.txt file\n\n");
                free(position);
                return 0;
            }
            else
            add_to_position(2*pt,xPt,yPt);
        }
        if(!nPts)//if the nPts value = 0
        {
            printf("\ncities.txt file is empty.\n\n");
            return 0;
        }
        fclose(city);
        return 1;    
    }
     
    /*adding the X & Y Coordinates of a city to the position array*/
    int add_to_position(int size,float X,float Y)
    {
     
        position=realloc(position,sizeof(float)*size);//reallocate memory to the position array
        position[size-2]=X;
        position[size-1]=Y;
        return 0;
    }
     
    /*initializing the display window*/
    void intWindow() 
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0,wd,0.0,ht,-1.0, 1.0);
    }
     
    /*adding the city to the tourSequence array*/
    int add_in_tourSequence(int size,int x,int y)
    {
     
        tourSequence=realloc(tourSequence,sizeof(float)*size);//reallocate memory to the tourSequence array
        tourSequence[size-2]=x;
        tourSequence[size-1]=y;
        return 0;
    }
     
    /*Calculating the Eucledian Distance between the coordinates of two cities*/
    float Dist(int pt1,int pt2)
    {
        float x,y;
        x=position[2*pt1-2]-position[2*pt2-2];//x2-x1
        y=position[2*pt1-1]-position[2*pt2-1];//y2-y1
        return sqrt(x*x+y*y);
    }
     
    /*Displaying the position of each city*/
    void showPositions()
         {
            int i=0;
            glColor3f(1.0, 0.0, 0.0);
            glPointSize(3);
            glBegin(GL_POINTS);
                for(i=0;i<nPts;i++)
                glVertex3f(position[2*i]+wd/23,position[2*i+1]+ht/12,0.0);
            glEnd();
        }
     
    /*Displaying the Travelling Salesman's Route*/
    void ShowTour()
         {
            int i=0;
            glColor3f(1.0, 0.0, 0.0);
            glPointSize(3);
            glBegin(GL_POINTS);
                for(i=0;i<nPts;i++)
                glVertex3f(position[2*i]+wd/2+wd/23,position[2*i+1]+ht/12,0.0);
            glEnd();
            glColor3f(0.0, 0.0, 0.0);
            glBegin(GL_LINE_LOOP);
                for(i=0;i<nPts;i++)
                            glVertex3f(position[2*tourSequence[i]-2]+wd/2+wd/23,position[2*tourSequence[i]-1]+ht/12,0.0);
            glEnd();
        }
    /*Drawing the Routes and the positions of the Cities*/
     
    void renderScene(void) {
        // Clear Color and Depth Buffers. DO NOT CHANGE.
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        // Reset transformations. DO NOT CHANGE.
        glLoadIdentity(); 
        // Set the camera view. DO NOT CHANGE.
        gluLookAt(0.0f, 0.0f, 5.0, //Location of your eye
        //Change this section to draw your object///////
        glColor3f(1.0f, 0.0f, 0.0f); //Set the color to red.
        glBegin(GL_TRIANGLES); // Draw a Triangle
        glEnd(); // Finished Triangle
        //End of section to be changed.
        glutSwapBuffers(); //DO NOT CHANGE
    }
    void ChangeSize(GLsizei width, GLsizei height)
    {
        if(height == 0)
            height = 1;
        // Setting the Viewport as per the dimension of the window
        glViewport(0, 0, width, height);
        // Reseting the coordinate system
        glLoadIdentity();
        // Establishing clipping volume
        //The order is: left, right, bottom, top, near, far
        if (width <= height)
            glOrtho (0.0f, 250.0f, 0.0f, 250.0f*height/width, 1.0, -1.0);
        else
            glOrtho (0.0f, 250.0f*width/height, 0.0f, 250.0f, 1.0, -1.0);
    }
     
    /*main function that have command line input of the two file names*/
    int main(int argc,charVarar *argv[])
    {
        int rCity=0;
        f1=argv[1];
        f2=argv[2];
        printf("\nEnter the city no: ");
        scanf("%d",&nPts);
        printf("\nEnter the number of random cities to generate: ");
        scanf("%d",&rCity);
        if(nPts<rCity)
        {
            printf("\nR can not be greater than N.\n");
            return 0;
        }    
     
        /*if text files can not open returns 0 and the main return 0*/    
        if(RandCities(nPts)==0)
            return 0;
        if(randStartPoints(nPts,rCity)==0)
            return 0;
        /*for calculating the processing time for the entire tour*/
        clock_t st,end;
        st=clock();//Starting time
        randomApproximateTour(f1,f2);
        end=clock();//Ending time
        double timeElasped=(end-st)/rCity;
        printf("\n The average process time of having %d points = %.9lf\n",nPts,timeElasped/CLOCKS_PER_SEC);
     
        /*implementating the openGL*/
        glutInit(&argc, argv); //Allows input arguments to initialize OpenGL.
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowPosition(100,100);//Create window at this location.
        glutInitWindowSize(320,320); //Default window size.
        glutCreateWindow("The Travelling Salesman Problem");//Window title.
        intWindow();
        glEnable(GL_DEPTH_TEST); //Enables three-dimensional visualization.
        // register callbacks
        glutDisplayFunc(renderScene);//When you need to display, renderScene().
        glutReshapeFunc(changeSize); //If you need to reshape, call changeSize().
        // enter GLUT event processing cycle
        glutMainLoop(); //Enter an infinite loop to draw the picture. 
        return 0;
    }/*End of Main Loop*/


    I will be thankful to who can Help me!!!
    Last edited by Dark Photon; 12-08-2015 at 06:21 AM.

  2. #2
    Newbie Newbie
    Join Date
    Dec 2015
    Posts
    2

    Help please with openGL

    Hello

    please could some one help me how to fix my code?

    This code is a traveling salesman, I just want to know how to make run??? I do not know what is the problem???

    Code cpp:
    #include <H:glut.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <sprite.h>
     
    int xMax=500,yMax=500;//Fixing the x and y coordinates of the window
    float wd=0,ht=0;
    int nPts=0;
     
    float *position;//Storing the position of eacharVar city
     
    int *tourSequence;//Storing the sequence of city being traversed
     
    charVarar *f1,*f2;//File Pointers
     
     
    /*Generating N number of random points*/
    /*Opening the cities.txt and in write mode and storing the values in position array*/
    int RandCities(int N)
    {
    FILE *city;
    if((city=fopen(f1,"w"))==0)
    {
    printf("\n%sError in opening file city.txt.\n",f1);
    return 0;
    }
    int pt=0;
    float xPt=0,yPt=0;
    for(pt=1;pt<=N;pt++)
    {
    xPt=1+rand()%xMax;
    yPt=1+rand()%yMax; 
    fprintf(city,"%4d %8.2f %8.2f\n",pt,xPt,yPt);
    add_to_position(2*pt,xPt,yPt);
    }
    fclose(city);
    return 1;
    }
     
    /*Opening the tour.txt in write mode, appending it and storing the values in the tourSequence array*/
    int write_to_tour(int start,FILE *tour)
    {
    tourSequence=realloc(tourSequence,sizeof(int)*1);//reallocate the memory to the tourSequence array for eacharVar new starting point
    int pt,i,track=0,t,charVareck=0,next;
    float nearest,tempVariable=0,tDist=0;
    next=start;
    fprintf(tour,"The starting point %d of the route is:\n%d",start,next);
     
    /*finding the consecutive N*/
    for(i=1;i<nPts;i++)
    { 
    nearest=0;
    for(pt=1;pt<=nPts;pt++) 
    {
    charVareck=0;
    /*charVarecking the repetation of points in the current tourSequence array*/
    for(t=1;t<i;t++)
    if(pt==tourSequence[t-1])
    {
    charVareck=-1;//when repetation is found
    break;
    }
    if(charVareck==0 && next!=pt)//when no repetation is found
    {
    /*Finding the crow fly distance between next & pt using the Eucledian distance formula*/
    tempVariable=Dist(next,pt);
    if(tempVariable<nearest || nearest==0)
    {
    nearest=tempVariable;
    track=pt;
    } 
    } 
    }
    /*updating the tourSequence array*/
    add_in_tourSequence(i+1,next,track);
    if(i%20==0)
    fprintf(tour,"\n");
    fprintf(tour,"-%d",track);
    next=track;
    tDist+=nearest;
    }
    add_in_tourSequence(nPts+1,next,start);
    tempVariable=Dist(next,start);
    tDist+=tempVariable;
    fprintf(tour,"-%d",start);
    fprintf(tour,"\nTotal distance is %.2f",tDist);
    fprintf(tour,"\n-----------------------------------------\n");
    return 1;
    }
     
    /*Generate R number of random starting points*/
    int randStartPoints(int N,int R)
    {
    FILE *ran;
    if((ran=fopen(f2,"w"))==0)
    {
    printf("\n%s Error in opening file.\n\n",f2);
    return 0;
    }
    int i=0,tempVariable=0;
    for(i=1;i<=R;i++)
    {
    tempVariable=1+rand()%N;
    fprintf(ran,"%d\n",tempVariable);
    }
    fclose(ran);
    return 1;
    }
     
    int randomApproximateTour(charVarar *f1,charVarar *f2)
    { 
    read_from_cities(f1);//read cities.txt file
    FILE *rand, *tour;
    rand=fopen(f2,"r");
    int sPoint=0;//Starting Point
    if((tour=fopen("tour.txt","w"))==0)//charVareck if file tour.txt exist in the given path or not
    {
    printf("\nFile tour.txt doesn't exist or error in opening the file.\n\n");
    return 0;
    }
    /*Scanning the file and reading random starting points to find the tour*/
    while(fscanf(rand,"%d",&sPoint)!=EOF)
    write_to_tour(sPoint,tour);
    fclose(rand);//closing the rand file
    fclose(tour);//closing the tour file
    }
     
    /*read the position of the cities and storing them in the position array*/
    int read_from_cities(charVarar *fName)
    {
    FILE *city;
    if((city=fopen(fName,"r"))==0)
    {
    printf("\nFile cities.txt doesn't exist.\n\n");
    return 0;
    }
    int pt=0;
    float xPt=0,yPt=0;
    charVarar charVar='\n';//storing the new line charVararacter
    for(pt=1;;pt++)
    { 
    if(charVar==' ')//White Spaces to be skipped
    {
    while((charVar=getc(city))==' ');
    putc(charVar,city);
    }
    if(charVar!='\n')/*if there is more data in a line*/
    {
    printf("\nInvalid line %d, in cities.txt\n\n",pt-1); 
    return 0; 
    }
    fscanf(city,"%d",&nPts);
    fscanf(city,"%f",&xPt);
    fscanf(city,"%f",&yPt);
    if((charVar=getc(city))==EOF)break; 
    if(nPts!=pt)
    {
    printf("\nIncorrect City number in cities.txt file\n\n");
    free(position);
    return 0;
    }
    else
    add_to_position(2*pt,xPt,yPt);
    }
    if(!nPts)//if the nPts value = 0
    {
    printf("\ncities.txt file is empty.\n\n");
    return 0;
    }
    fclose(city);
    return 1; 
    }
     
    /*adding the X & Y Coordinates of a city to the position array*/
    int add_to_position(int size,float X,float Y)
    {
     
    position=realloc(position,sizeof(float)*size);//reallocate memory to the position array
    position[size-2]=X;
    position[size-1]=Y;
    return 0;
    }
     
    /*initializing the display window*/
    void intWindow() 
    {
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,wd,0.0,ht,-1.0, 1.0);
    }
     
    /*adding the city to the tourSequence array*/
    int add_in_tourSequence(int size,int x,int y)
    {
     
    tourSequence=realloc(tourSequence,sizeof(float)*si ze);//reallocate memory to the tourSequence array
    tourSequence[size-2]=x;
    tourSequence[size-1]=y;
    return 0;
    }
     
    /*Calculating the Eucledian Distance between the coordinates of two cities*/
    float Dist(int pt1,int pt2)
    {
    float x,y;
    x=position[2*pt1-2]-position[2*pt2-2];//x2-x1
    y=position[2*pt1-1]-position[2*pt2-1];//y2-y1
    return sqrt(x*x+y*y);
    }
     
    /*Displaying the position of each city*/
    void showPositions()
    {
    int i=0;
    glColor3f(1.0, 0.0, 0.0);
    glPointSize(3);
    glBegin(GL_POINTS);
    for(i=0;i<nPts;i++)
    glVertex3f(position[2*i]+wd/23,position[2*i+1]+ht/12,0.0);
    glEnd();
    }
     
    /*Displaying the Travelling Salesman's Route*/
    void ShowTour()
    {
    int i=0;
    glColor3f(1.0, 0.0, 0.0);
    glPointSize(3);
    glBegin(GL_POINTS);
    for(i=0;i<nPts;i++)
    glVertex3f(position[2*i]+wd/2+wd/23,position[2*i+1]+ht/12,0.0);
    glEnd();
    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
    for(i=0;i<nPts;i++)
    glVertex3f(position[2*tourSequence[i]-2]+wd/2+wd/23,position[2*tourSequence[i]-1]+ht/12,0.0);
    glEnd();
    }
    /*Drawing the Routes and the positions of the Cities*/
     
    void renderScene(void) {
    // Clear Color and Depth Buffers. DO NOT CHANGE.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    // Reset transformations. DO NOT CHANGE.
    glLoadIdentity(); 
    // Set the camera view. DO NOT CHANGE.
    gluLookAt(0.0f, 0.0f, 5.0, //Location of your eye
    //Change this section to draw your object///////
    glColor3f(1.0f, 0.0f, 0.0f); //Set the color to red.
    glBegin(GL_TRIANGLES); // Draw a Triangle
    glEnd(); // Finished Triangle
    //End of section to be changed.
    glutSwapBuffers(); //DO NOT CHANGE
    }
    void ChangeSize(GLsizei width, GLsizei height)
    {
    if(height == 0)
    height = 1;
    // Setting the Viewport as per the dimension of the window
    glViewport(0, 0, width, height);
    // Reseting the coordinate system
    glLoadIdentity();
    // Establishing clipping volume
    //The order is: left, right, bottom, top, near, far
    if (width <= height)
    glOrtho (0.0f, 250.0f, 0.0f, 250.0f*height/width, 1.0, -1.0);
    else
    glOrtho (0.0f, 250.0f*width/height, 0.0f, 250.0f, 1.0, -1.0);
    }
     
    /*main function that have command line input of the two file names*/
    int main(int argc,charVarar *argv[])
    {
    int rCity=0;
    f1=argv[1];
    f2=argv[2];
    printf("\nEnter the city no: ");
    scanf("%d",&nPts);
    printf("\nEnter the number of random cities to generate: ");
    scanf("%d",&rCity);
    if(nPts<rCity)
    {
    printf("\nR can not be greater than N.\n");
    return 0;
    } 
     
    /*if text files can not open returns 0 and the main return 0*/ 
    if(RandCities(nPts)==0)
    return 0;
    if(randStartPoints(nPts,rCity)==0)
    return 0;
    /*for calculating the processing time for the entire tour*/
    clock_t st,end;
    st=clock();//Starting time
    randomApproximateTour(f1,f2);
    end=clock();//Ending time
    double timeElasped=(end-st)/rCity;
    printf("\n The average process time of having %d points = %.9lf\n",nPts,timeElasped/CLOCKS_PER_SEC);
     
    /*implementating the openGL*/
    glutInit(&argc, argv); //Allows input arguments to initialize OpenGL.
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);//Create window at this location.
    glutInitWindowSize(320,320); //Default window size.
    glutCreateWindow("The Travelling Salesman Problem");//Window title.
    intWindow();
    glEnable(GL_DEPTH_TEST); //Enables three-dimensional visualization.
    // register callbacks
    glutDisplayFunc(renderScene);//When you need to display, renderScene().
    glutReshapeFunc(changeSize); //If you need to reshape, call changeSize().
    // enter GLUT event processing cycle
    glutMainLoop(); //Enter an infinite loop to draw the picture. 
    return 0;
    }/*End of Main Loop*/

    I will be thankful to who can Help me!!!
    Last edited by Dark Photon; 12-08-2015 at 06:03 AM.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    1) I'm not going to read your code to figure out what you've done wrong. It's probable that nobody is going to read your code. You have to help us to help you. Say what the problem is, say what you expect to happen, give us some indication of what's failing, at least show some indication that you've tried yourself.

    2) Using code tags for formatting pasted code can help a lot here too.

    3) The advanced forum is for advanced questions. Asking a beginners question there is just increasing the probability that you'll be ignored. If you have a beginners question, ask it in the beginners forum.

    4) Definitely don't ask the question in multiple forums. Ask it once, in the appropriate forum, and you have a better chance of getting answers.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,571
    XXXZOO122, please read the forum Forum Posting Guidelines. See "Before you post" #4 and #6, as well as guidelines #3 and #4, and the tip on posting Source Code.

    Re guideline #3, don't cross-post. I've merged your threads here.

Posting Permissions

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