[译]GLUT教程 - 整合代码3
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III
这里我们准备包含一些前面几节展示过的素材.我们准备添加菜单到应用程序,子菜单和菜单交换.
直接复制粘贴下面代码到你的项目.鼠标右键会打开菜单.按键's'和'c'会生效到菜单选项.
#include <stdlib.h> #include <math.h>#ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut.h> #endif// angle of rotation for the camera direction float angle = 0.0f;// actual vector representing the camera's direction float lx=0.0f,lz=-1.0f;// XZ position of the camera float x=0.0f, z=5.0f;// the key states. These variables will be zero //when no key is being presses float deltaAngle = 0.0f; float deltaMove = 0; int xOrigin = -1;// Constant definitions for Menus #define RED 1 #define GREEN 2 #define BLUE 3 #define ORANGE 4#define FILL 1 #define LINE 2#define SHRINK 1 #define NORMAL 2// Pop up menu identifiers int fillMenu, shrinkMenu, mainMenu, colorMenu;// color for the nose float red = 1.0f, blue=0.5f, green=0.5f;// scale of snowman float scale = 1.0f;// menu status int menuFlag = 0;void changeSize(int w, int h) {// Prevent a divide by zero, when window is too short// (you cant make a window of zero width).if (h == 0)h = 1;float ratio = w * 1.0 / h;// Use the Projection Matrix glMatrixMode(GL_PROJECTION);// Reset Matrix glLoadIdentity();// Set the viewport to be the entire windowglViewport(0, 0, w, h);// Set the correct perspective.gluPerspective(45.0f, ratio, 0.1f, 100.0f);// Get Back to the Modelview glMatrixMode(GL_MODELVIEW); }void drawSnowMan() {glScalef(scale, scale, scale);glColor3f(1.0f, 1.0f, 1.0f);// Draw BodyglTranslatef(0.0f ,0.75f, 0.0f);glutSolidSphere(0.75f,20,20);// Draw HeadglTranslatef(0.0f, 1.0f, 0.0f);glutSolidSphere(0.25f,20,20);// Draw Eyes glPushMatrix();glColor3f(0.0f,0.0f,0.0f);glTranslatef(0.05f, 0.10f, 0.18f);glutSolidSphere(0.05f,10,10);glTranslatef(-0.1f, 0.0f, 0.0f);glutSolidSphere(0.05f,10,10);glPopMatrix();// Draw Nose glColor3f(red, green, blue);glRotatef(0.0f,1.0f, 0.0f, 0.0f);glutSolidCone(0.08f,0.5f,10,2);glColor3f(1.0f, 1.0f, 1.0f); }void computePos(float deltaMove) {x += deltaMove * lx * 0.1f;z += deltaMove * lz * 0.1f; }void renderScene(void) {if (deltaMove)computePos(deltaMove);// Clear Color and Depth BuffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Reset transformations glLoadIdentity();// Set the cameragluLookAt( x, 1.0f, z,x+lx, 1.0f, z+lz,0.0f, 1.0f, 0.0f);// Draw ground glColor3f(0.9f, 0.9f, 0.9f);glBegin(GL_QUADS);glVertex3f(-100.0f, 0.0f, -100.0f);glVertex3f(-100.0f, 0.0f, 100.0f);glVertex3f( 100.0f, 0.0f, 100.0f);glVertex3f( 100.0f, 0.0f, -100.0f);glEnd();// Draw 36 SnowMenfor(int i = -3; i < 3; i++)for(int j=-3; j < 3; j++) {glPushMatrix();glTranslatef(i*10.0f, 0.0f, j * 10.0f);drawSnowMan();glPopMatrix();}glutSwapBuffers(); }// ----------------------------------- // KEYBOARD // -----------------------------------void processNormalKeys(unsigned char key, int xx, int yy) {glutSetMenu(mainMenu);switch (key) {case 27:glutDestroyMenu(mainMenu);glutDestroyMenu(fillMenu);glutDestroyMenu(colorMenu);glutDestroyMenu(shrinkMenu);exit(0);break;case 's':if (!menuFlag)glutChangeToSubMenu(2,"Shrink",shrinkMenu);break;case 'c':if (!menuFlag)glutChangeToSubMenu(2,"Color",colorMenu);break;}if (key == 27)exit(0); }void pressKey(int key, int xx, int yy) {switch (key) {case GLUT_KEY_UP : deltaMove = 0.5f; break;case GLUT_KEY_DOWN : deltaMove = -0.5f; break;} }void releaseKey(int key, int x, int y) {switch (key) {case GLUT_KEY_UP :case GLUT_KEY_DOWN : deltaMove = 0;break;} }// ----------------------------------- // MOUSE // -----------------------------------void mouseMove(int x, int y) {// this will only be true when the left button is downif (xOrigin >= 0) {// update deltaAngledeltaAngle = (x - xOrigin) * 0.001f;// update camera's directionlx = sin(angle + deltaAngle);lz = -cos(angle + deltaAngle);} }void mouseButton(int button, int state, int x, int y) {// only start motion if the left button is pressedif (button == GLUT_LEFT_BUTTON) {// when the button is releasedif (state == GLUT_UP) {angle += deltaAngle;xOrigin = -1;}else {// state = GLUT_DOWNxOrigin = x;}} }// ----------------------------------- // MENUS // -----------------------------------void processMenuStatus(int status, int x, int y) {if (status == GLUT_MENU_IN_USE)menuFlag = 1;elsemenuFlag = 0; }void processMainMenu(int option) {// nothing to do in here// all actions are for submenus }void processFillMenu(int option) {switch (option) {case FILL: glPolygonMode(GL_FRONT, GL_FILL); break;case LINE: glPolygonMode(GL_FRONT, GL_LINE); break;} }void processShrinkMenu(int option) {switch (option) {case SHRINK: scale = 0.5f; break;case NORMAL: scale = 1.0f; break;} }void processColorMenu(int option) {switch (option) {case RED :red = 1.0f;green = 0.0f;blue = 0.0f; break;case GREEN :red = 0.0f;green = 1.0f;blue = 0.0f; break;case BLUE :red = 0.0f;green = 0.0f;blue = 1.0f; break;case ORANGE :red = 1.0f;green = 0.5f;blue = 0.5f; break;} }void createPopupMenus() {shrinkMenu = glutCreateMenu(processShrinkMenu);glutAddMenuEntry("Shrink",SHRINK);glutAddMenuEntry("NORMAL",NORMAL);fillMenu = glutCreateMenu(processFillMenu);glutAddMenuEntry("Fill",FILL);glutAddMenuEntry("Line",LINE);colorMenu = glutCreateMenu(processColorMenu);glutAddMenuEntry("Red",RED);glutAddMenuEntry("Blue",BLUE);glutAddMenuEntry("Green",GREEN);glutAddMenuEntry("Orange",ORANGE);mainMenu = glutCreateMenu(processMainMenu);glutAddSubMenu("Polygon Mode", fillMenu);glutAddSubMenu("Color", colorMenu);// attach the menu to the right button glutAttachMenu(GLUT_RIGHT_BUTTON);// this will allow us to know if the menu is active glutMenuStatusFunc(processMenuStatus); }// ----------------------------------- // MAIN // -----------------------------------int main(int argc, char **argv) {// init GLUT and create windowglutInit(&argc, argv);glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);glutInitWindowPosition(100,100);glutInitWindowSize(320,320);glutCreateWindow("Lighthouse3D - GLUT Tutorial");// register callbacks glutDisplayFunc(renderScene);glutReshapeFunc(changeSize);glutIdleFunc(renderScene);glutIgnoreKeyRepeat(1);glutKeyboardFunc(processNormalKeys);glutSpecialFunc(pressKey);glutSpecialUpFunc(releaseKey);// here are the two new functions glutMouseFunc(mouseButton);glutMotionFunc(mouseMove);// OpenGL init glEnable(GL_DEPTH_TEST);glEnable(GL_CULL_FACE);// init Menus createPopupMenus();// enter GLUT event processing cycle glutMainLoop();return 1; }
转载于:https://www.cnblogs.com/live41/p/3392322.html
[译]GLUT教程 - 整合代码3相关推荐
- [译]GLUT教程 - 每秒帧数
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Frames per Second 你的程序实际上跑得多快? 有时我们 ...
- [译]GLUT教程 - 重整子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...
- [译]GLUT教程 - 键盘高级特性
Lighthouse3d.com >> GLUT Tutorial >> Input >> Advanced Keyboard 本节我们会介绍另外4个处理键盘事件的 ...
- [译]GLUT教程 - 笔划字体
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Stroke Fonts 笔划字体是用线条生成的.跟位图字体相反,笔划字 ...
- GLUT教程 - glutPostRedisplay函数
http://www.cnblogs.com/live41/p/3395899.html [译]GLUT教程 - glutPostRedisplay函数 Lighthouse3d.com >&g ...
- [译]Vulkan教程(04)基础代码
[译]Vulkan教程(04)基础代码 General structure 通用结构 In the previous chapter you've created a Vulkan project w ...
- [译]Vulkan教程(05)Instance
[译]Vulkan教程(05)Instance Creating an instance 创建一个instance The very first thing you need to do is ini ...
- [译][Tkinter 教程14] menu 菜单
已获原作者授权. 原系列地址: Python Tkinter 简介 一提到"menu"这个词, 很多人首先想到的是餐馆里的菜单. 虽然餐馆菜单和计算机程序中的菜单看起来一点也不像, ...
- [译]RabbitMQ教程C#版 - 远程过程调用(RPC)
先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...
最新文章
- 从哪些方面可以选出优质的香港服务器?
- [Python学习] 专题三.字符串的基础知识
- NSArray ----NSMutableArray
- 《乐在C语言》一2.2 数据类型
- 了解ADF生命周期中的ADF绑定
- 小程序 | 微信小程序多图上传(点击上传、可修改删除与放大回显)
- C语言函数一章教学,C语言-第1章教学案.doc
- CSDN 蒋涛对话英特尔中国区董事长王锐:我愿是当代的一个开发者
- 【LOJ】#6391. 「THUPC2018」淘米神的树 / Tommy
- RPA机器人可以为工业制造业带来什么巨变
- tomcat自定义错误页面
- Lightroom Classic CC 2019 for Mac永久破解激活方法(含lr cc 2019破解补丁)
- 浅谈域名抢注和域名投资
- java short int 转换_基本类型'short'-Java中的强制转换
- js摇号程序_车管所怎么摇号流程及查询
- 4399怎么修复游戏服务器,[ 服务器 ]4399官方服务器(改ip了)
- 关于计算机学院的毕业论文致谢,计算机学院毕业生论文致谢范文
- 网络传输介质有哪几种
- 双网卡同时上内外网的方法
- @Valid 和@Validated 总结