我现在遇到了一个问题,我试图创建一个程序。该程序获取通过argv传递的文件,解析命令,提取值,并根据信息运行glut命令。
我有一个方法,directFile,我专注于切换案例'c'。 im传递的文件有'cone'命令,它只创建一个半径为.5且高度为1的圆锥。所以当开关看到这个命令时,它会调用函数'drawCone'。
问题是,drawCone不会在过剩窗口中绘制一个圆锥体。它什么都不做。但是,如果我在显示功能中放置相同的代码,它就可以正常工作。我很容易过剩,所以对我来说很容易!但我需要一些建议,让我的代码做我想做的事情。
#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();
}
答案 0 :(得分:1)
您必须从内部显示回调函数调用锥形图。
然后你可以在空闲功能中读取文件,每个命令都会生成一个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 ,则可以控制此方面。
编辑或者,考虑使用&#39; display lists&#39;:即
GLuint commands;
void display()
{
...
glCallList(commands);
...
}
int main()
{
...
commands = glGenLists(1);
glNewList(commands, GL_COMPILE);
/* read file and post display commands */
glEndList();
...
}