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

hem*_*kar 4 opengl

reshape()函数如何在此代码中工作?如何从glutReshapeFunc(reshape)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;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 6

似乎该glutReshapeFunc()函数采用指向函数的指针; 据推测,事实上,它被宣布为有点类似于:

void glutReshapeFunc(void (*function)(int x, int y));
Run Code Online (Sandbox Code Playgroud)

类似地,glutDisplayFunc()获取另一个指向函数glutKeyboardFunc()的指针,并且还获取指向函数的指针.当一个函数由name指定而没有函数调用后面的括号时,它会缩减为'指向函数的指针'(或者你可以将一个裸函数名称视为指向函数体的指针,就像一个裸数组名称是一个指针到数组的开头).

您必须阅读手册以发现函数的参数xy参数的用途keyboard().它们不会被显示的代码使用.它们很可能是某种东西的位置,但如果不阅读手册,那么它就不那么明确了.


dat*_*olf 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在关于重塑回调中的内容:在初学者教程中放置的所有内容实际上在显示功能中做得更好.设置视口,设置投影我的意思.稍后您可能想要绘制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();
}
Run Code Online (Sandbox Code Playgroud)