游戏项目:需要说明

时间:2015-04-21 10:12:27

标签: c opengl

我正在开发一个OpenGL项目,我需要对主题的核心组件做一些简短的解释,因为我需要向有需要的人解释。

以下是该计划的一部分

以下是程序中使用的全局变量和头文件

#include<GL/glut.h>
#include<math.h>
#include<stdbool.h>
#define PI 3.14159265f
#include<stdio.h>

GLfloat ballRadius = 0.2,xradius=0.2,xxradius=1.0; 
GLfloat ballX = 0.0f;   
GLfloat ballY = 0.0f;
GLfloat ballXMax,ballXMin,ballYMax,ballYMin;  
GLfloat xSpeed = 0.02f;  
GLfloat ySpeed = 0.007f;
int refreshMills = 30;  
GLfloat angle=0.0;
int xa,ya; 
int flag=0,flag1=0;
int score = 0;
void *currentfont;
GLfloat xo=0, yo=0, x, y;
GLdouble clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop;
void balldisp() ;
void scoredisp();

这是重塑功能。我需要做它究竟在做什么,它在计算和存储的内容。困惑于此

void reshape(GLsizei width,GLsizei height)
{                                                           
    GLfloat aspect = (GLfloat)width / (GLfloat)height;  
    glViewport(0,0,width,height);   
    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();               
    if(width >=height)
    {
        clipAreaXLeft = -1.0 * aspect;
        clipAreaXRight = 1.0 * aspect;
        clipAreaYBottom = -1.0;
        clipAreaYTop = 1.0;
    }
    else
    {
        clipAreaXLeft = -1.0;
        clipAreaXRight = 1.0 ;
        clipAreaYBottom = -1.0 / aspect;
        clipAreaYTop = 1.0/ aspect;
    }
    gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop+0.10);
    ballXMin = clipAreaXLeft + ballRadius;
    ballXMax =  clipAreaXRight - ballRadius;
    ballYMin =  clipAreaYBottom + ballRadius;
    ballYMax =  clipAreaYTop - ballRadius;  
}

以下是显示球的代码。它的计算方法以及速度和方向的设定方式。困惑于此

void balldisp() 
{
    glTranslatef(ballX,ballY,0.0f);    
    glBegin(GL_TRIANGLE_FAN);    
    color();
    glVertex2f(0.0f,0.0f);      
    int numSegments = 100;      
    int i;
    for(i=0;i<=numSegments;i++) 
    {
        angle = i*2.0f*PI/numSegments;
        glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRadius);
    }
    glEnd();    

    ballX += xSpeed;
    ballY += ySpeed;

    if(ballX > ballXMax)
    {   xa=ballX;
        ballX = ballXMax;
        xSpeed = -xSpeed;

    }
    else if(ballX < ballXMin)
    {   xa=ballX;
        ballX = ballXMin;
        xSpeed = -xSpeed;

    }
    if(ballY > ballYMax)
    {   ya=ballY;
        ballY = ballYMax;
        ySpeed = -ySpeed;

    }
    else if(ballY < ballYMin)
    {   ya=ballY;
        ballY = ballYMin;
        ySpeed = -ySpeed;

    }

我想知道重塑功能和球显示。他们在做什么以及如何在那里完成任务。

P.S。该项目涉及球的随机运动,该球撞击窗户的边界并向其他方向移动

1 个答案:

答案 0 :(得分:1)

reshape函数已在GLUT中注册(使用glutReshapeFunc),这样只要窗口大小发生变化,就会通过 GLUT调用请注意,放置OpenGL函数以在重塑函数中设置视口和/或投影矩阵是不好的样式,应该避免。所有与OpenGL绘图相关的函数(glViewport和矩阵设置都属于)都属于显示函数。

类似地,显示功能在GLUT中注册(使用glutDisplayFunc),这样只要窗口需要重绘,它就会被 GLUT调用(因为它可见,内容需要刷新)或glutPostRedisplay}请求重绘。