GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 103
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);//设置光源位置 107
x,y,z,w四个变量组成上面的数组,其中前三个代表位置,最后一个代表离场景近远(W可不予理会)
坐标系 右手边为x正,上方为y正,从屏幕向你为z正
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 150
gluLookAt(
eyex,eyey,eyez, // 这里是观察者位置
targetx,targety,targetz // 这是观察者看着的那点
upx,upy,upz ) // 这是观察者头顶向着的方向
glRotatef(30.0, 1.0, 0.0, 0.0);//旋转使物体被观测的角度变化 多处出现
glRotatef( angle, x, y, z )函数的作用是将当前坐标系以a( x, y, z )向量为旋转轴旋转angle角度
glTranslatef(4.5f, 0.0f, -6.0f);//将画笔移动到另一个地点,方便绘制下一个图形 174
注意坐标系约定和函数参数单位!
实验结果图。。
核心代码
void CTribleangleView::OnDraw(CDC* pDC) { CTribleangleDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here //初始化各种设置 wglMakeCurrent(pDC->m_hDC, m_hGLRC);//将绘图上下文绑定到设备上下文上 glClearColor(0.5, 0.5, 0.5, 0.0);//设置背景色 glClearDepth(1.0f);//初始化深度 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清空缓冲区 glEnable(GL_DEPTH_TEST);//开启深度测试 glShadeModel(GL_SMOOTH);//平滑阴影模式 CRect rcClient; GetClientRect(&rcClient); GLsizei w = rcClient.Width();//视区宽度 GLsizei h = rcClient.Height();//视区高度 glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION);//选择投影矩阵 glLoadIdentity();//重置投影矩阵 gluPerspective(60.0, (GLfloat)w / h, 0.1, 100.0);//设置投影方式及投影参数 glMatrixMode(GL_MODELVIEW);//选择模式矩阵 glLoadIdentity();//重置模式矩阵 //设置光照(定义一个位于左上方的白色定位光源) GLfloat lmodel_ambient[] = { 1.0f,1.0f,1.0f,1.0f };//定义环境光 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);//设置环境光 GLfloat light0_ambient[] = { 1.0,1.0,1.0,1.0 };//定义光源环境光 GLfloat light0_diffuse[] = { 1.0,1.0,1.0,1.0 };//定义光源散射光 GLfloat light0_specular[] = { 1.0,1.0,1.0,1.0 };//定义光源反射光 GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);//设置光源环境光 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);//设置光源散射光 glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);//设置光源反射光 glLightfv(GL_LIGHT0, GL_POSITION, light0_position);//设置光源位置 glEnable(GL_LIGHT0);//启用光源 glEnable(GL_LIGHTING);//启用光照效果 //定义黄铜材质 GLfloat brass_ambient[] = { 0.329412f,0.223529f,0.027451f,1.0f };//定义材质环境光 GLfloat brass_diffuse[] = { 0.780392f,0.568627f,0.113725f,1.0f };//定义材质散射光 GLfloat brass_specular[] = { 0.992157f,0.941176f,0.807843f,1.0f };//定义材质反射光 GLfloat brass_sinines[] = { 100.0f };//定义材质镜面反射强度 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, brass_ambient);//设置材质环境光 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, brass_diffuse);//设置材质散射光 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, brass_specular);//设置材质反射光 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, brass_sinines);//设置材质镜面反射强度 //塑料材质 GLfloat plastic_ambient[] = { 0.0f,0.0f,0.00f,1.0f };//定义材质环境光 GLfloat plastic_diffuse[] = { 0.7f,0.85f,0.7f,1.0f };//定义材质散射光 GLfloat plastic_specular[] = { 0.75f,0.75f,0.75f,1.0f };//定义材质反射光 GLfloat plastic_sinines[] = { 10.0f };//定义材质镜面反射强度 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, plastic_ambient);//设置材质环境光 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plastic_diffuse);//设置材质散射光 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, plastic_specular);//设置材质反射光 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, plastic_sinines);//设置材质镜面反射强度 //平面纹理坐标生成 static GLfloat xequalzero[] = { 1.0,1.0,1.0,1.0 }; static GLfloat slanted[] = { 1.0,1.0,1.0,1.0 }; static GLfloat *currentCoeff; static GLenum currentPlane; static GLint currentGenMode; static float roangles; glClearColor(1.0, 1.0, 1.0, 1.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); #define stripleImageWidth 32 GLubyte stripleImage[4 * stripleImageWidth]; int j; for (j = 0; j < stripleImageWidth; ++j) { stripleImage[4 * j + 0] = (GLubyte)((j <= 4) ? 255 : 0); stripleImage[4 * j + 1] = (GLubyte)((j>4) ? 255 : 0); stripleImage[4 * j + 2] = (GLbyte)(0); stripleImage[4 * j + 3] = (GLbyte)255; } glPixelStoref(GL_UNPACK_ALIGNMENT,1); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage1D(GL_TEXTURE_1D, 0, 4, stripleImageWidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, stripleImage); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); currentCoeff = xequalzero; currentGenMode = GL_OBJECT_LINEAR; currentPlane = GL_OBJECT_PLANE; glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode); glTexGenfv(GL_S, currentPlane, currentCoeff); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_1D); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); glMaterialf(GL_FRONT, GL_SHININESS, 64.0); roangles = 45.0; //设置坐标系统 glTranslatef(0.0f, -0.1f, -2.0f);//将坐标系统下移0.1,后移2.0 glRotatef(45, 1.0, 0.0, 0.0);//将坐标系统绕x轴逆时针旋转45度 glRotatef(30, 0.0, -1.0, 0.0);//将坐标系统绕y轴顺时针旋转30度 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glTranslatef(-1.5f, 0.0f, -6.0f); glRotatef(30.0, 1.0, 0.0, 0.0); glRotatef(30.0, 0.0, 1.0, 0.0); glutSolidCube(2.0); glLoadIdentity(); glTranslatef(2.5f, 0.0f, -6.0f); glRotatef(0.0f, 1.0f, 0.0f, 0.0f); glutSolidSphere(2.0, 32, 32); glDisable(GL_TEXTURE_2D);//禁用纹理映射 glPopMatrix(); glFlush();//强制输出 SwapBuffers(pDC->m_hDC);//交换缓冲区 wglMakeCurrent(pDC->m_hDC, NULL);//置绘图上下文为非当前状态,释放设备上下文 }
时间: 2024-12-17 07:15:27