过剩的reshape()函数有什么用?

时间:2013-07-09 02:53:20

标签: opengl

reshape()函数如何在此代码中运行?如果在glutReshapeFunc(reshape)的重塑中没有任何参数,它是如何从glutReshapeFunc(reshape)获取参数的? int x函数中int yvoid keyboard (unsigned char key, int x, int y)的价值是多少?

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

static int year = 0, day = 0;

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);

   glPushMatrix();
   glutWireSphere(1.0, 20, 16);   /* draw sun */
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
   glTranslatef (2.0, 0.0, 0.0);
   glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
   glutWireSphere(0.2, 10, 8);    /* draw smaller planet */
   glPopMatrix();
   glutSwapBuffers();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key) {
      case `d':
         day = (day + 10) % 360;
         glutPostRedisplay();
         break;
      case `D':
         day = (day - 10) % 360;
         glutPostRedisplay();
         break;
      case `y':
         year = (year + 5) % 360;
         glutPostRedisplay();
         break;
      case `Y':
         year = (year - 5) % 360;
         glutPostRedisplay();
         break;
      default:
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}

3 个答案:

答案 0 :(得分:5)

似乎glutReshapeFunc()函数接受一个函数指针;据推测,事实上,它被宣布为有点类似于:

void glutReshapeFunc(void (*function)(int x, int y));

类似地,glutDisplayFunc()采用另一个指向函数的指针,glutKeyboardFunc()也采用指向函数的指针。当一个函数由name指定而没有函数调用后面的括号时,它会缩减为'指向一个函数的指针'(或者你可以把一个裸函数名称想象成一个指向函数体的指针,就像一个裸数组名是一个指针到数组的开头。)

您必须阅读本手册,以了解xy功能的keyboard()和{{1}}参数的用途。它们不会被显示的代码使用。它们很可能是某种东西的位置,但如果不阅读手册,那么它就不那么明确了。

答案 1 :(得分:5)

reshapekeyboard函数用作所谓的回调。您正在为这些函数提供GLUT指针,GLUT会保留这些指针并使用参数调用这些函数,这些都是在GLUT文档中指定的时间。

关于那样:

void (*display_callback)(void);
void (*reshape_callback)(int, int);
void (*keyboard_callback(unsigned char, int, int);
/* ... */

void eventloop(...)
{
    while(...) {
        if( keyboard_event )
              keyboard_callback(keyboard_event->key, mouse_x, mouse_y);

        if( window_reshaped )
              reshape_callback(window->width, window->height);

        if( needs_redraw )
              display_callback();
    }
}

现在关于重塑回调的内容:在初学者教程中放置的 Everything 实际上在显示功能中做得更好。设置视口,设置投影我的意思。稍后您可能想要绘制HUD,一些文本或小地图,或拆分视图。一旦你达到这一点,进行视口和投影设置的重塑功能就变成了一种负担。所以现在摆脱它。

void display(void)
{
   int const w = glutGet(GLUT_WINDOW_WIDTH);
   int const h = glutGet(GLUT_WINDOW_HEIGHT);

   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);

   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   glPushMatrix();
   glutWireSphere(1.0, 20, 16);   /* draw sun */
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
   glTranslatef (2.0, 0.0, 0.0);
   glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
   glutWireSphere(0.2, 10, 8);    /* draw smaller planet */
   glPopMatrix();

   glutSwapBuffers();
}

答案 2 :(得分:0)

我希望这将是这个问题的直接答案。 reshape 函数是回调函数,只要应用程序窗口的大小或形状发生变化,就会调用该函数。重塑函数有2个参数,它们是重构窗口的 width height 。主要是这些参数用于设置新的视口