【OpenGL绘制企鹅】记录用OpenGL绘制的企鹅---(一)
2013-10-13 22:54
393 查看
代码+exe+中间文件 down!!我的是vs12工程
HERE
Firstly, result is like this...
here is code...
Hmmm...Bad codes...
我们的OpenGL第一个大作业,后期还要在进行加工。目前眼睛用Sphere实现,随后会改成纹理贴图。May be 15 later.
TIPS:
按键:
“o”:切换为正交投影。
“p”:切换为透视投影。
HERE
Firstly, result is like this...
here is code...
Hmmm...Bad codes...
我们的OpenGL第一个大作业,后期还要在进行加工。目前眼睛用Sphere实现,随后会改成纹理贴图。May be 15 later.
TIPS:
按键:
“o”:切换为正交投影。
“p”:切换为透视投影。
#include <stdlib.h> #include <gl\glut.h> #include <gl\GL.h> #include <gl\GLU.h> #include <math.h> #include <iostream> #define PI 3.141592 static float c= PI/180.0f; //弧度和角度转换参数 static int du = 90,oldmy = -1,oldmx = -1; //du是视点绕y轴的角度,opengl里默认y轴是上方向 static float r = 2.0f,h = 0.0f; //r是视点绕y轴的半径,h是视点高度即在y轴 static float width = 500, height = 500; static bool isPers = true; void init() { glClearColor (1.0, 1.0, 1.0, 1.0); glShadeModel (GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); const GLfloat light_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_position[] = { 0.5f, 0.5f, -0.5f, 1.0f }; const GLfloat mat_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; const GLfloat mat_diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f }; const GLfloat mat_specular[] = { 0.3f, 0.3f, 0.3f, 1.0f }; const GLfloat high_shininess[] = { 100.0f }; glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); //关闭背面顶点的计算 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE , GL_FALSE); } void display() { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); if(isPers){ gluPerspective(50, 1, 1, 10); gluLookAt( r * cos(c * du), h, r * sin(c * du), 0, 0, 0, 0, 1, 0); } else { glLoadIdentity(); gluOrtho2D(-(GLdouble) height/height, (GLdouble) height/height, -(GLdouble) height/height, (GLdouble) height/height); } static int list = 0; if( list == 0 ) { // 嘴的数组 GLfloat PointA[] = {0, 0.0, 0.28}, PointB[] = {-0.15, 0.1, 0.28}, PointC[] = {0.15, 0.1, 0.28}, PointD[] = {0, 0.1, 0.45}, PointE[] = {0, 0.15, 0.28}; GLfloat ColorR[] = {0, 0, 1}; // 组成嘴的六个三角形 glNewList(list, GL_COMPILE); glColor3f(1, 0.6, 0); glBegin(GL_TRIANGLES); // 平面BDC glColor3f(1, 0.6, 0); glVertex3fv(PointC); glColor3f(1, 1.0, 0); glVertex3fv(PointD); glColor3f(1, 0.6, 0); glVertex3fv(PointB); // 平面ADB glColor3f(1, 0.6, 0); glVertex3fv(PointA); glColor3f(1, 0.6, 0); glVertex3fv(PointB); glColor3f(1, 0.8, 0); glVertex3fv(PointD); // 平面ACD glColor3f(1, 1.0, 0); glVertex3fv(PointD); glColor3f(1, 0.6, 0); glVertex3fv(PointC); glColor3f(1, 0.6, 0); glVertex3fv(PointA); glColor3f(1, 0.6, 0); glVertex3fv(PointE); glColor3f(1, 0.6, 0); glVertex3fv(PointB); glColor3f(1, 1.0, 0); glVertex3fv(PointD); glColor3f(1, 0.6, 0); glVertex3fv(PointE); glColor3f(1, 1.0, 0); glVertex3fv(PointD); glColor3f(1, 0.6, 0); glVertex3fv(PointC); glColor3f(1, 1.0, 0); glVertex3fv(PointD); glColor3f(1, 0.6, 0); glVertex3fv(PointB); glColor3f(1, 0.6, 0); glVertex3fv(PointC); glEnd(); glEndList(); glPushMatrix(); // 嘴 glPushMatrix(); glCallList(list); glPopMatrix(); //围脖 glPushMatrix(); glColor3d(1, 0.0, 0); glTranslated(0.0, 0.0, 0.0); glRotated(90, 1.0, 0.0, 0.0); glutSolidTorus(0.16, 0.17, 10, 10); glPopMatrix(); // 头 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(0.0, 0.15, 0.0); glutSolidSphere( 0.3, 40, 40 ); glPopMatrix(); // 身体 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(0.0, -0.2, 0.0); glutSolidSphere( 0.35, 40, 40 ); glPopMatrix(); // 左手 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(-0.22, 0.0, 0.1); glRotated(40, 0, 0, 1); glRotated(-90, 0, 1, 0); GLUquadricObj *objCylinder = gluNewQuadric(); gluCylinder(objCylinder, 0.08, 0.03, 0.3, 32, 5); glPopMatrix(); // 右手 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(0.22, 0.0, 0.1); glRotated(-40, 0, 0, 1); glRotated(90, 0, 1, 0); GLUquadricObj *objCylinder1 = gluNewQuadric(); gluCylinder(objCylinder1, 0.08, 0.03, 0.3, 32, 5); glPopMatrix(); // 左脚 glPushMatrix(); glColor3f(1, 0.5, 0); glTranslated(-0.2, -0.45, 0); glScaled(1.0,1.0,1.6); glutSolidSphere(0.13, 8, 5); glPopMatrix(); // 右脚 glPushMatrix(); glColor3f(1, 0.5, 0); glTranslated(0.2, -0.45, 0); glScaled(1.0,1.0,1.6); glutSolidSphere(0.13, 8, 5); glPopMatrix(); // 左眼睛 glPushMatrix(); glColor3f(1, 1, 1); glTranslated(-0.1, 0.2, 0.3); glScaled(1.0,1.8,0.1); glutSolidSphere(0.05, 20, 10); glPopMatrix(); // 右眼睛 glPushMatrix(); glColor3f(1, 1, 1); glTranslated(0.1, 0.2, 0.3); glScaled(1.0,1.8,0.1); glutSolidSphere(0.05, 20, 10); glPopMatrix(); // 左眼珠 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(0.1, 0.2, 0.31); glScaled(1.0, 1.0, 0.01); glutSolidSphere(0.03, 20, 10); glPopMatrix(); // 右眼珠 glPushMatrix(); glColor3f(0, 0, 0); glTranslated(-0.1, 0.2, 0.31); glScaled(1.0, 1.0, 0.01); glutSolidSphere(0.03, 20, 10); glPopMatrix(); glPopMatrix(); glLoadIdentity(); glutSwapBuffers(); } } void keyboard (unsigned char key, int x, int y) { switch (key) { case 'o': isPers = false; break; case 'p': du = 90; oldmy = -1; oldmx = -1; r = 2.0; h = 0.0; isPers = true; break; case 27: exit(0); break; default: break; } display(); } void reshape(int w, int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(isPers){ gluPerspective(50, 1, 1, 10); gluLookAt( r * cos(c * du), h, r * sin(c * du), 0, 0, 0, 0, 1, 0); } else { glLoadIdentity(); gluOrtho2D(-(GLdouble) height/height, (GLdouble) height/height, -(GLdouble) height/height, (GLdouble) height/height); } glMatrixMode(GL_MODELVIEW); } void Mouse(int button, int state, int x, int y) //处理鼠标点击 { if(state == GLUT_DOWN) //第一次鼠标按下时,记录鼠标在窗口中的初始坐标 oldmx = x,oldmy = y; } void onMouseMove(int x,int y) //处理鼠标拖动 { du += x - oldmx; //鼠标在窗口x轴方向上的增量加到视点绕y轴的角度上,这样就左右转了 h += 0.03f * ( y - oldmy ); //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了 if(h > 5.0f) h = 5.0f; //视点y坐标作一些限制,不会使视点太奇怪 else if(h < -5.0f) h = -5.0f; oldmx = x, oldmy = y; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备 display(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMouseFunc(Mouse); glutMotionFunc(onMouseMove); glutMainLoop(); return 0; }
相关文章推荐
- 解决Vista系统OpenGL驱动问题的方法整理
- vs 不显示行号的操作方法
- linux vs linux的优势
- 先装VS再装IIS时出错的解决方法
- 通过OpenGL ES混合模式缩放视频缓冲区来适应显示尺寸
- 关于visual studio 2012 update 2中的新功能介绍
- 关于jQuery $.isNumeric vs. $.isNaN vs. isNaN
- 《实战OpenGL三维可视化系统开发与源码精解》
- VC/VS 注释快捷键
- 微软为码农更改Visual Studio设计
- OpenGL ES入门指南
- 转载一篇openGL的教程收集
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- 从零开始学习OpenGL ES之三 – 透视
- 从零开始学习OpenGL ES之四 – 光效
- 从零开始学习OpenGL ES之一补遗 – 代号
- 从零开始学习OpenGL ES之五 – 材质
- 从零开始学习OpenGL ES之五补遗 – setupView重写
- OpenGL ES系列之0 – 建立Xcode项目
- OpenGL开发库的详细介绍