Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //********************************************
- // lab3.cpp
- //********************************************
- #include "stdafx.h"
- #include "Tool.h"
- #include "ToolDoc.h"
- #include "RenderView.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #ifndef M_PI
- #define M_PI 3.141592654
- #endif
- void CRenderView::drawSphere(double r)
- {
- float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
- float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
- float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
- float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
- float no_shininess = 0.0f;
- float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
- glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- if(m_Highlight)
- {
- // your codes for highlight here
- } else {
- glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
- glMaterialf(GL_FRONT, GL_SHININESS, no_shininess);
- }
- glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
- int i,j;
- int n = 20;
- for(i=0;i<n;i++)
- for(j=0;j<2*n;j++)
- if(m_Smooth)
- {
- glBegin(GL_POLYGON);
- // the normal of each vertex is actaully its own coordinates normalized for a sphere
- // your normal here
- glVertex3d(r*sin(i*M_PI/n)*cos(j*M_PI/n),r*cos(i*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
- // your normal here
- glVertex3d(r*sin((i+1)*M_PI/n)*cos(j*M_PI/n),r*cos((i+1)*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
- // your normal here
- glVertex3d(r*sin((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*cos((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
- // your normal here
- glVertex3d(r*sin(i*M_PI/n)*cos((j+1)*M_PI/n),r*cos(i*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
- glEnd();
- } else {
- glBegin(GL_POLYGON);
- // Explanation: the normal of the whole polygon is the coordinate of the center of the polygon for a sphere
- glNormal3d(sin((i+0.5)*M_PI/n)*cos((j+0.5)*M_PI/n),cos((i+0.5)*M_PI/n)*cos((j+0.5)*M_PI/n),sin((j+0.5)*M_PI/n));
- glVertex3d(r*sin(i*M_PI/n)*cos(j*M_PI/n),r*cos(i*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
- glVertex3d(r*sin((i+1)*M_PI/n)*cos(j*M_PI/n),r*cos((i+1)*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
- glVertex3d(r*sin((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*cos((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
- glVertex3d(r*sin(i*M_PI/n)*cos((j+1)*M_PI/n),r*cos(i*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
- glEnd();
- }
- }
- void CRenderView::drawMyCreation()
- {
- // your composite obejct here
- }
- void CRenderView::drawScene()
- {
- switch(obj_no) {
- case 0:
- drawSphere();
- break;
- case 1:
- // your primitive objects here
- break;
- case 2:
- drawMyCreation();
- break;
- case 3:
- // your another compsite object here
- break;
- default:
- break;
- };
- }
Add Comment
Please, Sign In to add comment