[译]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 = ;
int xOrigin = -; // 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 = ; 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 == )
h = ; float ratio = w * 1.0 / h; // Use the Projection Matrix
glMatrixMode(GL_PROJECTION); // Reset Matrix
glLoadIdentity(); // Set the viewport to be the entire window
glViewport(, , 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 Body
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,,); // Draw Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,,); // Draw Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,,);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,,);
glPopMatrix(); // Draw Nose
glColor3f(red, green, blue);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,,); 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 Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( 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 SnowMen for(int i = -; i < ; i++)
for(int j=-; j < ; 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 :
glutDestroyMenu(mainMenu);
glutDestroyMenu(fillMenu);
glutDestroyMenu(colorMenu);
glutDestroyMenu(shrinkMenu);
exit();
break; case 's':
if (!menuFlag)
glutChangeToSubMenu(,"Shrink",shrinkMenu);
break;
case 'c':
if (!menuFlag)
glutChangeToSubMenu(,"Color",colorMenu);
break;
}
if (key == )
exit();
} 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 = ;break;
}
} // -----------------------------------
// MOUSE
// ----------------------------------- void mouseMove(int x, int y) { // this will only be true when the left button is down
if (xOrigin >= ) { // update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f; // update camera's direction
lx = 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 pressed
if (button == GLUT_LEFT_BUTTON) { // when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
} // -----------------------------------
// MENUS
// ----------------------------------- void processMenuStatus(int status, int x, int y) { if (status == GLUT_MENU_IN_USE)
menuFlag = ;
else
menuFlag = ;
} 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 window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Lighthouse3D - GLUT Tutorial"); // register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene); glutIgnoreKeyRepeat();
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 ;
}
[译]GLUT教程 - 整合代码3的更多相关文章
- [译]GLUT教程 - 整合代码2
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...
- [译]GLUT教程 - 整合代码1
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far 以下是前面几节的完整整合代码: #inc ...
- [译]GLUT教程 - 整合代码8
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...
- [译]GLUT教程 - 整合代码7
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...
- [译]GLUT教程 - 整合代码6
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI 下面代码以窗体模式启动.你可以在 ...
- [译]GLUT教程 - 整合代码5
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far V 该代码与位图字体的代码类似.区别是 ...
- [译]GLUT教程 - 整合代码4
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV 以下代码使用了位图字 ...
- [译]GLUT教程(目录)
http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...
- [译]GLUT教程 - 游戏模式
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...
随机推荐
- App保持登录状态的常用方法(转)
我们在使用App时,一次登录后App如果不主动退出登录或者清除数据,App会在很长一段时间内保持登录状态,或者让用户感觉到登录一次就不用每次都输入用户密码才能进行登录.银行.金融涉及到支付类的App一 ...
- Xamarin XAML语言教程使用属性设置进度条的当前进度
Xamarin XAML语言教程使用属性设置进度条的当前进度 在图12.19~12.21中我们看到的是没有实现加载的进度条,即进度条的当前进度为0,如果开发者想要修改当前进度,可以使用两种方式:一种是 ...
- Topcoder 刷题之路_鶸的奋斗
最近碰到的题不是水题就是坑题,实在没意思,听说神犇们都在Topcoder上刷SRM,于是我决定将SRM的DIV 1刷个遍.这里是目录 哎..好多转博客不注明出处的,这里给出本博客的出处:http:// ...
- sql server 2005 链接服务器:未将服务器 配置为用于 RPC
原文:sql server 2005 链接服务器:未将服务器 配置为用于 RPC RPC: 远程过程调用(Remote Procedure Calls) --LinkedServer.Rpc 属性: ...
- CSS背景属性background
background属性是所有背景属性的缩写: 以下是这些背景属性: background-color:背景颜色 你可以通过颜色名称(red/green/blue)来设置 也可以用十六进制(#fff/ ...
- XCode下的iOS单元测试
XCode 内置了 OCUnit 单元测试框架,但目前最好用的测试框架应该是 GHUnit.通过 GHUnit + OCMock 组合,我们可以在 iOS 下进行较强大的单元测试功能.本文将演示如何在 ...
- XCTest(二)
New tool sets are making it easier and easier to engage in genuine agile development on iOS. In part ...
- DevExpress.XtraTreeList
1. DevExpress.XtraTreeList控件 将其简称为tree,tree其实就是一个树表控件,他像树一样包含具有父子关系的若干节点,同时每个节点又是一个带有多个字段的记录 ...
- GlusterFS分布式文件系统部署
GlusterFS是一个可伸缩的网络文件系统,使用常见的现成的硬件,您可以创建大型分布式存储流媒体解决方案.数据分析.和其他数据相关的任务.GlusterFS是自由和开源软件. 详细参考官网:http ...
- osg节点统计方法(点数 面数) 【转】
void statusNode(osg::ref_ptr<osg::Node> node,int& verNum,int& faceNum){ osg::ref_ptr&l ...