Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:24:17+00:00 2026-05-28T16:24:17+00:00

I have an issue right now with a program im trying to create. The

  • 0

I have an issue right now with a program im trying to create. The program takes a file passed through argv , parses it for commands, extracts values, and runs glut commands based off the info.

I have a method , directFile , and im specifically focusing on the switch case ‘c’. The file im passing has the command ‘cone’ which just creates a cone with a radius of .5 and a height of 1. So when the switch sees this command it calls the function ‘drawCone’.

Issue im having is that drawCone does not draw a cone in the glut windows. It does nothing. But if i put that same code in the display function it works just fine. Im very new to glut so be easy on me! But i need some advice on what to do to make my code do what i want it to.

#include <gl\glew.h>
#include <gl\freeglut.h>
#include <gl\GLU.h>
#include <stdio.h>

void directFile(char input[]);
void extractVals(char *input,double *val);
void makeLower(char *input);
void drawCone();

int g_mainWindow = -1;
float g_lightPos[] = {1, 1, -1, 0};

/*
    Draw a cone
*/
void drawCone(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,0,0);
    glutSolidCone(.5,1,8,1);




    //glLoadIdentity();

    glFlush();

}


void display()
{


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /*
    glColor3f(1,0,0);
    glutSolidCone(.5,1,8,1);*/


    //glLoadIdentity();

    glFlush();
}

void reshape(int w, int h)
{
    float aspect = w / (float)h;

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION_MATRIX);
    glLoadIdentity();
    glOrtho(-aspect, aspect, -1, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW_MATRIX);
}


void idle()
{
    glutSetWindow(g_mainWindow);
    glutPostRedisplay();
}

/*
    Takes a file input and parses out the instructions needed to draw an object
*/
void directFile(char input[100]){
    char switchVal [10] , *s = switchVal;
    double val[4];

    s = strtok(input, " \n\0");
    switch(*s){
        case 'g'://translate object
            extractVals(s , val);
            break;
        case 's'://scales an object
            printf("%s is the command to scale, now which one is it?\n",s);
            extractVals(s , val);
            if(val[3] == 1.){
                printf("The object will be scaled by %f\n", val[0]);
            } else if (val[3] == 3.){
                printf("The object will be shrunk by sx: %f , sy: %f, sz: %f\n", val[0] , val[1] , val[2]);
            }
            break;
        case 'r'://rotates an object
            printf("%s will rotate the image!\n",s);
            break;
        case 'c'://this can call draw cone , cube, or change colors.
            if(strcmp(s , "cone") == 0){
                printf("It appears you have your self a %s. Lets draw it!\n", s);
                drawCone();
            } else if (strcmp(s , "cube") == 0){
                printf("%s is cool too\n" , s);
            } else if (*s == 'c'){
                printf("Welp command was \"%s\", lets change some colors huh?\n",s);
            }
            break;
        case 't'://draw a torus or tea pot
            break;
        case 'o'://reads a meshfile
            break;
        case 'f'://save current frame buffer.
            break;
        case 'm':
            break;
    }
}

/*
    Using a tolenizer this extracts out values needed for other functions to draw.
*/
void extractVals(char *input, double *val){
    int i=0;
    input = strtok(NULL, " ,");
    while(input != NULL){
        val[i] = atof(input);
        input = strtok(NULL, " ,");
        i++;
    }
    val[3] = i--;
}

/*
    Since the read file is not case sesitive we will force everything lowercase.
*/
void makeLower(char *input)
{
    while (*input != '\0')
    {
        *input = tolower(*input);
        input++;
    }
}

/*
    main class!
*/
int main(int argc, char **argv) {

    //imports file from ar
    FILE *file = fopen(argv[1], "r");//file opened
    char linebyline [50], *lineStr = linebyline;


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
    g_mainWindow = glutCreateWindow("Hello, glut");

    while(!feof(file) && file != NULL){
        fgets(lineStr , 50, file);
        makeLower(lineStr);
        directFile(lineStr);
    }
    fclose(file);

    glClearColor(0.5, 0.5, 0.5, 0);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);



    glutMainLoop();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:24:18+00:00Added an answer on May 28, 2026 at 4:24 pm

    You must call cone drawing from inside the display callback function.

    Then you could read the file in the Idle function, and each command generate a glutPostRedisplay(), to obtain an animated display. Something like:

    FILE *file;
    void Idle()
    {
      /* parse a command from file */
      /* store the data for later draw */
      glutPostRedisplay();
    }
    
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        /* use data read from Idle
        glColor3f(1,0,0);
        glutSolidCone(.5,1,8,1);
        */
        glFlush();
        // also consider glutSwapBuffers for smoothness
    }
    
    int main()
    {
       file = fopen(...);
    }
    

    A problem of glut is that never return the control to the caller. If you use freeglut you can control this aspect.

    EDIT alternatively, consider using ‘display lists‘: i.e.

    GLuint commands;
    void display()
    {
        ...
        glCallList(commands);
        ...
    }
    int main()
    {
       ...
       commands = glGenLists(1);
       glNewList(commands, GL_COMPILE);
       /* read file and post display commands */
       glEndList();
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tough problem right now. I am having a big dictionary file
I have to create a C# program that deals well with reading in huge
I have a C program for an exercise and it has a strange issue
I am having a bit of an issue right now. Is there any simple
We have a chat program that works with only a couple of browsers right
EDIT: Link should work now, sorry for the trouble I have a text file
I'm designing a program now and I've come across a UI issue. I want
First to tell you my issue: I have written a program that based on
This is getting extremely irritating. Right now I have a winforms application, and things
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.