OpenGL代码,谁能帮忙注释一下谢谢,要交作业了,自己不太懂额

void CRectangleopenglView::OnDestroy()
{
CView::OnDestroy();

// TODO:  在此处添加消息处理程序代码
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
    ::wglDeleteContext(hrc);
if (m_pDC)
    delete m_pDC;

}

BOOL CRectangleopenglView::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return true;
//return CView::OnEraseBkgnd(pDC);
}

void CRectangleopenglView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO:  在此处添加消息处理程序代码

int w = cx;  int h = cy;
ww = w; hh = h;
glViewport(0, 0, w, h); //设置视口与窗口匹配
glMatrixMode(GL_PROJECTION); //重新设置坐标系统
glLoadIdentity();
//建立正交变换下的剪切体
if (w<h)
    glOrtho(-2.0 * scle, 2.0 * scle, -2.0*h / w * scle, 2.0*h / w * scle, -5.0, 5.0);
else
    glOrtho(-2.0*w / h * scle, 2.0*w / h * scle, -2.0 * scle, 2.0 * scle, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void CRectangleopenglView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
angle+=3;
if(angle>=360)
angle=0;
Invalidate();
CView::OnTimer(nIDEvent);

}

void CRectangleopenglView::OnWork1()
{
// TODO: 在此添加命令处理程序代码
DrawType = 1;
}

void CRectangleopenglView::OnWork2()
{
// TODO: 在此添加命令处理程序代码
DrawType = 2;
}

void CRectangleopenglView::OnWork3()
{
// TODO: 在此添加命令处理程序代码
DrawType = 3;
}

void CRectangleopenglView::OnWork4()
{
// TODO: 在此添加命令处理程序代码
DrawType = 4;
}
void CRectangleopenglView::DrawSphere(double xx, double yy, double zz,double radius)

{

double dPoint1[3], dPoint2[3], dPoint3[3];
double dNormal[3];
    glPushMatrix();
//  glRotatef(angle,0,1,1);
    double PI= 3.1415;
    int M=50,N=50;
//  float radius = 0.5;
//float xx=0,yy=0,zz=0;
    float step_z = PI / M;
    float step_xy = 2 * PI / N;
    float x[4], y[4], z[4];

    float angle_z = 0.0;
    float angle_xy = 0.0;
    int i = 0, j = 0;
//  float a[3],b[3],c[3];

//  glRotatef(45, 0, 1, 0);

// glRotatef(45, 1, 0, 0);

    //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    //glBindTexture(GL_TEXTURE_2D,TypeTexture.ID);
        glBegin(GL_QUADS);
    for (i = 0; i<M; i++)
    {
        angle_z = i * step_z;

        for (j = 0; j<N; j++)
        {
            angle_xy = j * step_xy;

            x[0] = radius * sin(angle_z) * cos(angle_xy);
            y[0] = radius * sin(angle_z) * sin(angle_xy);
            z[0] = radius * cos(angle_z);

            x[1] = radius * sin(angle_z + step_z) * cos(angle_xy);
            y[1] = radius * sin(angle_z + step_z) * sin(angle_xy);
            z[1] = radius * cos(angle_z + step_z);

            x[2] = radius*sin(angle_z + step_z)*cos(angle_xy + step_xy);
            y[2] = radius*sin(angle_z + step_z)*sin(angle_xy + step_xy);
            z[2] = radius*cos(angle_z + step_z);

            x[3] = radius * sin(angle_z) * cos(angle_xy + step_xy);
            y[3] = radius * sin(angle_z) * sin(angle_xy + step_xy);
            z[3] = radius * cos(angle_z);



            dPoint1[0] = x[0];
            dPoint1[1] = y[0];
            dPoint1[2] = z[0];

            dPoint2[0] = x[1];
            dPoint2[1] = y[1];
            dPoint2[2] = z[1];

            dPoint3[0] = x[2];
            dPoint3[1] = y[2];
            dPoint3[2] = z[2];


            CalculateNormal(dPoint1, dPoint2, dPoint3, dNormal);
            glNormal3dv(dNormal);

            for (int k = 0; k<4; k++)
            {
                //  glColor3f(xx + x[k], yy + y[k], zz + z[k]);
                //glTexCoord2f(1.0, 0.0);
                glVertex3f(xx + x[k], yy + y[k], zz + z[k]);
            }

        }
    }
    glEnd();

}

void CRectangleopenglView::DrawRectangle()
{

/** 选择纹理 */
glBindTexture(GL_TEXTURE_2D, Texture[0].ID);
/** 开始绘制四边形 */
glBegin(GL_QUADS);
/// 前侧面
glNormal3f(0.0f, 0.0f, 1.0f);                               /**< 指定法线指向观察者 */
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
/// 后侧面
glNormal3f(0.0f, 0.0f, -1.0f);                              /**< 指定法线背向观察者 */
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
/// 顶面
glNormal3f(0.0f, 1.0f, 0.0f);                               /**< 指定法线向上 */
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
/// 底面
glNormal3f(0.0f, -1.0f, 0.0f);                              /**< 指定法线朝下 */
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
/// 右侧面
glNormal3f(1.0f, 0.0f, 0.0f);                               /**< 指定法线朝右 */
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
/// 左侧面
glNormal3f(-1.0f, 0.0f, 0.0f);                              /**< 指定法线朝左 */
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();                                                        /**< 四边形绘制结束 */

}

void CRectangleopenglView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

point1 = point;
CView::OnLButtonDown(nFlags, point);

}

void CRectangleopenglView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

double i = (point.x - point1.x)*(point.x - point1.x) + (point.y - point1.y)*(point.y - point1.y);
double t = sqrt(i);
sin1 = (point.y - point1.y) / t;
cos1 = (point.x - point1.x) / t;
CView::OnLButtonUp(nFlags, point);

}

void CRectangleopenglView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

if (nChar == 'A')
{
    scle+=0.5;
    if (scle >= 5)
        scle = 5;
    glMatrixMode(GL_PROJECTION); //重新设置坐标系统
    glLoadIdentity();
    //建立正交变换下的剪切体

    if (ww<hh)
        glOrtho(-2.0 * scle, 2.0 * scle, -2.0*hh / ww * scle, 2.0*hh / ww * scle, -5.0, 5.0);
    else
        glOrtho(-2.0*ww / hh * scle, 2.0*ww / hh * scle, -2.0 * scle, 2.0 * scle, -5.0, 5.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}

else if (nChar == 'S')
{
    scle-=0.5;
    if (scle <= 0.5)
        scle = 0.5;
    glMatrixMode(GL_PROJECTION); //重新设置坐标系统
    glLoadIdentity();
    //建立正交变换下的剪切体

    if (ww<hh)
        glOrtho(-2.0 * scle, 2.0 * scle, -2.0*hh / ww * scle, 2.0*hh / ww * scle, -5.0, 5.0);
    else
        glOrtho(-2.0*ww / hh * scle, 2.0*ww / hh * scle, -2.0 * scle, 2.0 * scle, -5.0, 5.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}



CView::OnKeyDown(nChar, nRepCnt, nFlags);

}

2个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问