1,glTranslatef(-1.5f,0.0f,-6.0f);这里是被观察的物体在移动,还是视点的位置移动呢
我的理解是后者(就好像是照相机在移动位置来捕捉画面那样),默认的物体位置是屏幕中心,向左移动视图并将视图推远以便被观察的物体能进入,试着调整Z坐标的值,可以看到Z越往里面去,物体就越小,越往外面来,物体越大,这和照相机调整焦距类似。
2.使用Flat coloring(单调着色)给几何对象涂上固定的一种颜色。使用Smoothcoloring(平滑着色)将几个顶点的不同颜色混合在一起,创建漂亮的色彩混合。要注意窗口调整大小以及被其他窗口挡住后重新激活时要重新设置Shade Model。
int
COpenGLDemoView::DrawGLScene(GLvoid)
{ // Here's Where We Do All The Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
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();
return TRUE; // Everything Went OK
}
void COpenGLDemoView::OnActivateView(BOOL bActivate, CView * pActivateView, CView * pDeactiveView)
{
// TODO: Add your specialized code here and/or call the base class
InitGL(); // 激活窗口时强制要求还原openGL状态
CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
void COpenGLDemoView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
width = cx;
height = cy;
if (height == 0 ) // Prevent A Divide By Zero By
{
height = 1 ; // Making Height Equal One
}
glShadeModel(GL_SMOOTH);
glViewport( 0 , 0 ,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective( 45.0f ,(GLfloat)width / (GLfloat)height, 0.1f , 100.0f ); // 透视投影
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
BOOL COpenGLDemoView::InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor( 0.0 , 0.0 , 0.0 , 0.0 ); // Black Background
glClearDepth( 1.0f ); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
{ // Here's Where We Do All The Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
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();
return TRUE; // Everything Went OK
}
void COpenGLDemoView::OnActivateView(BOOL bActivate, CView * pActivateView, CView * pDeactiveView)
{
// TODO: Add your specialized code here and/or call the base class
InitGL(); // 激活窗口时强制要求还原openGL状态
CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
void COpenGLDemoView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
width = cx;
height = cy;
if (height == 0 ) // Prevent A Divide By Zero By
{
height = 1 ; // Making Height Equal One
}
glShadeModel(GL_SMOOTH);
glViewport( 0 , 0 ,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective( 45.0f ,(GLfloat)width / (GLfloat)height, 0.1f , 100.0f ); // 透视投影
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
BOOL COpenGLDemoView::InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor( 0.0 , 0.0 , 0.0 , 0.0 ); // Black Background
glClearDepth( 1.0f ); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
3,如果是下面这样设置颜色,则请问正方形的颜色会是怎么样的呢?
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(); // 正方形绘制结束