1,glTranslatef(-1.5f,0.0f,-6.0f);这里是被观察的物体在移动,还是视点的位置移动呢
我的理解是后者(就好像是照相机在移动位置来捕捉画面那样),默认的物体位置是屏幕中心,向左移动视图并将视图推远以便被观察的物体能进入,试着调整Z坐标的值,可以看到Z越往里面去,物体就越小,越往外面来,物体越大,这和照相机调整焦距类似。
2.使用Flat coloring(单调着色)给几何对象涂上固定的一种颜色。使用Smoothcoloring(平滑着色)将几个顶点的不同颜色混合在一起,创建漂亮的色彩混合。要注意窗口调整大小以及被其他窗口挡住后重新激活时要重新设置Shade Model。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->intCOpenGLDemoView::DrawGLScene(GLvoid)
{//Here'sWhereWeDoAllTheDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glBegin(GL_TRIANGLES);//绘制三角形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);//上顶点
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//右下
glEnd();//三角形绘制结束
glShadeModel(GL_FLAT);
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(1.0,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glEnd();//正方形绘制结束
glFlush();
returnTRUE;//EverythingWentOK
}
voidCOpenGLDemoView::OnActivateView(BOOLbActivate,CView*pActivateView,CView*pDeactiveView)
{
//TODO:Addyourspecializedcodehereand/orcallthebaseclass
InitGL();//激活窗口时强制要求还原openGL状态
CView::OnActivateView(bActivate,pActivateView,pDeactiveView);
}
voidCOpenGLDemoView::OnSize(UINTnType,intcx,intcy)
{
CView::OnSize(nType,cx,cy);
//TODO:Addyourmessagehandlercodehere
GLsizeiwidth,height;
width=cx;
height=cy;
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glShadeModel(GL_SMOOTH);
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
BOOLCOpenGLDemoView::InitGL(GLvoid)//AllSetupForOpenGLGoesHere
{
glShadeModel(GL_SMOOTH);//EnableSmoothShading
glClearColor(0.0,0.0,0.0,0.0);//BlackBackground
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
returnTRUE;//InitializationWentOK
}
{//Here'sWhereWeDoAllTheDrawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glBegin(GL_TRIANGLES);//绘制三角形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);//上顶点
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//右下
glEnd();//三角形绘制结束
glShadeModel(GL_FLAT);
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(1.0,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glEnd();//正方形绘制结束
glFlush();
returnTRUE;//EverythingWentOK
}
voidCOpenGLDemoView::OnActivateView(BOOLbActivate,CView*pActivateView,CView*pDeactiveView)
{
//TODO:Addyourspecializedcodehereand/orcallthebaseclass
InitGL();//激活窗口时强制要求还原openGL状态
CView::OnActivateView(bActivate,pActivateView,pDeactiveView);
}
voidCOpenGLDemoView::OnSize(UINTnType,intcx,intcy)
{
CView::OnSize(nType,cx,cy);
//TODO:Addyourmessagehandlercodehere
GLsizeiwidth,height;
width=cx;
height=cy;
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glShadeModel(GL_SMOOTH);
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
BOOLCOpenGLDemoView::InitGL(GLvoid)//AllSetupForOpenGLGoesHere
{
glShadeModel(GL_SMOOTH);//EnableSmoothShading
glClearColor(0.0,0.0,0.0,0.0);//BlackBackground
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
returnTRUE;//InitializationWentOK
}
3,如果是下面这样设置颜色,则请问正方形的颜色会是怎么样的呢?
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->glShadeModel(GL_FLAT);
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(1.0,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glEnd();//正方形绘制结束
呵呵,答案是:颜色会是绿色的,也就是说是以最后那个顶点的颜色为标准的,这是因为Flat coloring(单调着色)是会给四边形涂上固定的一种颜色,而最后一个顶点的颜色设置后,就覆盖了前面的颜色设置。
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS);//绘制正方形
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);//左上
glColor3f(1.0,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);//右下
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);//左下
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);//右上
glEnd();//正方形绘制结束
本文介绍了OpenGL中的视点移动原理及如何使用单调着色和平滑着色来为几何对象上色。通过实例展示了如何通过调整坐标使物体进入视图,并解释了单调着色下正方形颜色的确定方式。
526

被折叠的 条评论
为什么被折叠?



