GLUT和绘制命令不绘图

时间:2012-01-28 06:02:05

标签: c visual-studio-2010 glut

我最近重新设计了我的应用程序,以便在空闲功能中完成数据收集。该程序从argv [1]读入一个文件并解析该文件中的命令。这部分似乎都没有问题,解析后的数据也很准确。

问题出在显示功能上。命令'cone'被调用(现在是我文件中唯一的命令),并且在控制台输出中显示。但是画面上没有显示圆锥。我还尝试将圆锥的绘制命令放在循环之外,以查看它是否有任何影响(并且它没有)。

非常感谢任何帮助。

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

void makeLower(char *input);
void extractVals(char *cmd, double *val);

FILE *file;
int g_mainWindow = -1;
float g_lightPos[] = {1, 1, -1, 0};
char commands [50][50];
int fileSize = -1;

void idle()
{   
  /* parse a command from file */
  /* store the data for later draw */
    char linebyline [50], *lineStr = linebyline;
    int i=0;

    while(!feof(file) && file != NULL){
        fgets(lineStr , 50, file);
        makeLower(lineStr);
        strcpy(commands[i] , lineStr);
        fileSize = i;
        i++;
    }

    glutSetWindow(g_mainWindow);
    glutPostRedisplay();
}

void makeLower(char *input)
{
    while (*input != '\0')
    {
        *input = tolower(*input);
        input++;
    }
}

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



void display()
{
    int i;
    char cmdTok[10] , *cmd = cmdTok;
    float val[5];

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    for(i = 0 ; i <= fileSize ; i++){
        cmd = strtok(commands[i] , " \n\0");
            switch(*cmd){
                case 'g'://translate object
                    extractVals(cmd , val);
                    glTranslatef(val[0] , val[1] , val[2]);
                    break;
                case 's'://scales an object
                    extractVals(cmd , val);
                    if (val[4] == 1.){
                        glScalef(val[0],val[0],val[0]);
                    }
                    else if (val[4] == 3.){
                        glScalef(val[0] , val[1] , val [2]);
                    }
                    break;
                case 'r'://rotates an object
                    break;
                case 'c'://this can call draw cone , cube, or change colors.
                    if(strcmp(cmd , "cone") == 0){
                        printf("drawing a cone\n");
                        glutSolidCone(.5 , 1 , 8, 1);
                    } else if (strcmp(cmd , "cube") == 0){
                        glutSolidCube(1);
                    } else if (*cmd == 'c'){
                        extractVals(cmd , val);
                        glColor3f(val[0] , val[1], val[2]);
                    }
                    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;
            }

    }

    glutSolidCone(.5 , 1 , 8, 1);
    glFlush();
    // also consider glutSwapBuffers for smoothness
}

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


int main(int argc, char **argv)
{
   file = fopen(argv[1], "r");

   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
   g_mainWindow = glutCreateWindow("Hello, glut");
   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();
   fclose(file);
}

看起来它与进口有关。我添加了几行代码,似乎有效。

#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>

并在显示中添加了

glutSwapBuffers();

1 个答案:

答案 0 :(得分:0)

你有没有尝试过

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);

glutMainLoop()之前(宽度和高度是窗口的宽度和高度)?

我还没有完成Windows OpenGL,所以如果有什么不同,请随时纠正我。