2012-01-27 12 views
0

作成しようとしているプログラムに問題があります。プログラムは、argvを通過したファイルを受け取り、コマンドを解析し、値を抽出し、情報に基づいてglutコマンドを実行します。ファイル解釈とglutコマンド

私はdirectFileというメソッドを持っていて、特にスイッチケース 'c'に焦点を当てています。ファイルを渡すコマンドには '円錐'というコマンドがあり、半径が.5で高さが1の円錐が作成されます。したがって、スイッチがこのコマンドを見ると、関数 'drawCone'が呼び出されます。

問題点imは、drawConeがglutウィンドウに円錐を描かないということです。それは何もしません。しかし、同じコードを表示関数に入れてもうまく動作します。私の上で簡単にglutに非常に新しいです!しかし、私は自分のコードを自分の望むようにするために何をすべきかについていくつかアドバイスが必要です。

#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

あなたはから表示コールバック関数内コーン描画を呼び出す必要があります。

次に、Idle関数でファイルを読み取ることができ、各コマンドはglutPostRedisplay()を生成してアニメーション表示を取得します。何かのように:

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(...); 
} 

供給過剰の問題は、呼び出し元に制御を返すことはありませんということです。 freeglutを使用する場合は、このアスペクトを制御できます。

EDIT代わりに、 'display lists' を使用することを検討してください:すなわち

GLuint commands; 
void display() 
{ 
    ... 
    glCallList(commands); 
    ... 
} 
int main() 
{ 
    ... 
    commands = glGenLists(1); 
    glNewList(commands, GL_COMPILE); 
    /* read file and post display commands */ 
    glEndList(); 
    ... 
} 
+0

これは、ファイルを複数回読みますか?私は一度だけそれを読んでほしい。 – meriley

+0

申し訳ありませんが、それは明確ではありませんでした:私は、各コマンドの読み込み時に再表示を生成することを意味しました。 – CapelliC

+0

詳細をお寄せいただきありがとうございます。 glLoadIdentityの後のコメントをglColorで置き換えます。これらの関数はアイドル状態の関数と呼ばれていますか?または、後で実行できるコードの例だけですか? – meriley