[译]GLUT教程 - 整合代码6
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI
下面代码以窗体模式启动.你可以在一堆给定的设置中选择用哪个进入游戏模式,也可以返回窗体模式.命令是用位图字体显示在屏幕上.
- #include <stdio.h>
- #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 pressesed
- float deltaAngle = 0.0f;
- float deltaMove = ;
- int xOrigin = -;
- // color for the snowman's nose
- float red = 1.0f, blue=0.5f, green=0.5f;
- // scale of snowman
- float scale = 1.0f;
- // default font
- void *font = GLUT_STROKE_ROMAN;
- // width and height of the window
- int h,w;
- // variables to compute frames per second
- int frame;
- long time, timebase;
- char s[];
- char currentMode[];
- // this string keeps the last good setting
- // for the game mode
- char gameModeString[] = "640x480";
- void init();
- void changeSize(int ww, int hh) {
- h = hh;
- w = ww;
- // 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 renderBitmapString(
- float x,
- float y,
- float z,
- void *font,
- char *string) {
- char *c;
- glRasterPos3f(x, y,z);
- for (c=string; *c != '\0'; c++) {
- glutBitmapCharacter(font, *c);
- }
- }
- void renderStrokeFontString(
- float x,
- float y,
- float z,
- void *font,
- char *string) {
- char *c;
- glPushMatrix();
- glTranslatef(x, y,z);
- glScalef(0.002f, 0.002f, 0.002f);
- for (c=string; *c != '\0'; c++) {
- glutStrokeCharacter(font, *c);
- }
- glPopMatrix();
- }
- void restorePerspectiveProjection() {
- glMatrixMode(GL_PROJECTION);
- // restore previous projection matrix
- glPopMatrix();
- // get back to modelview mode
- glMatrixMode(GL_MODELVIEW);
- }
- void setOrthographicProjection() {
- // switch to projection mode
- glMatrixMode(GL_PROJECTION);
- // save previous matrix which contains the
- //settings for the perspective projection
- glPushMatrix();
- // reset matrix
- glLoadIdentity();
- // set a 2D orthographic projection
- gluOrtho2D(, w, h, );
- // switch back to modelview mode
- glMatrixMode(GL_MODELVIEW);
- }
- 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
- char number[];
- for(int i = -; i < ; i++)
- for(int j=-; j < ; j++) {
- glPushMatrix();
- glTranslatef(i*10.0f, 0.0f, j * 10.0f);
- drawSnowMan();
- sprintf(number,"%d",(i+)*+(j+));
- renderStrokeFontString(0.0f, 0.5f, 0.0f, (void *)font ,number);
- glPopMatrix();
- }
- // Code to compute frames per second
- frame++;
- time=glutGet(GLUT_ELAPSED_TIME);
- if (time - timebase > ) {
- sprintf(s,"Lighthouse3D - FPS:%4.2f",
- frame*1000.0/(time-timebase));
- timebase = time;
- frame = ;
- }
- setOrthographicProjection();
- void *font= GLUT_BITMAP_8_BY_13;
- glPushMatrix();
- glLoadIdentity();
- renderBitmapString(,,,font,(char *)"GLUT Tutorial @ Lighthouse3D");
- renderBitmapString(,,,font,s);
- renderBitmapString(,,,font,(char *)"F1 - Game Mode 640x480 32 bits");
- renderBitmapString(,,,font,(char *)"F2 - Game Mode 800x600 32 bits");
- renderBitmapString(,,,font,(char *)"F3 - Game Mode 1024x768 32 bits");
- renderBitmapString(,,,font,(char *)"F4 - Game Mode 1280x1024 32 bits");
- renderBitmapString(,,,font,(char *)"F5 - Game Mode 1920x1200 32 bits");
- renderBitmapString(,,,font,(char *)"F6 - Window Mode");
- renderBitmapString(,,,font,(char *)"Esc - Quit");
- renderBitmapString(,,,font,currentMode);
- glPopMatrix();
- restorePerspectiveProjection();
- glutSwapBuffers();
- }
- // -----------------------------------
- // KEYBOARD
- // -----------------------------------
- void processNormalKeys(unsigned char key, int xx, int yy) {
- switch (key) {
- case :
- if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != )
- glutLeaveGameMode();
- exit();
- break;
- }
- }
- 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;
- case GLUT_KEY_F1:
- // define resolution, color depth
- glutGameModeString("640x480:32");
- // enter full screen
- if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
- glutEnterGameMode();
- sprintf(gameModeString,"640x480:32");
- // register callbacks again
- // and init OpenGL context
- init();
- }
- else
- glutGameModeString(gameModeString);
- break;
- case GLUT_KEY_F2:
- // define resolution, color depth
- glutGameModeString("800x600:32");
- // enter full screen
- if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
- glutEnterGameMode();
- sprintf(gameModeString,"800x600:32");
- // register callbacks again
- // and init OpenGL context
- init();
- }
- else
- glutGameModeString(gameModeString);
- break;
- case GLUT_KEY_F3:
- // define resolution, color depth
- glutGameModeString("1024x768:32");
- // enter full screen
- if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
- glutEnterGameMode();
- sprintf(gameModeString,"1024x768:32");
- // register callbacks again
- // and init OpenGL context
- init();
- }
- else
- glutGameModeString(gameModeString);
- break;
- case GLUT_KEY_F4:
- // define resolution, color depth
- glutGameModeString("1280x1024:32");
- // enter full screen
- if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
- glutEnterGameMode();
- sprintf(gameModeString,"1280x1024:32");
- // register callbacks again
- // and init OpenGL context
- init();
- }
- else
- glutGameModeString(gameModeString);
- break;
- case GLUT_KEY_F5:
- // define resolution, color depth
- glutGameModeString("1920x1200");
- // enter full screen
- if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
- glutEnterGameMode();
- sprintf(gameModeString,"1920x1200");
- // register callbacks again
- // and init OpenGL context
- init();
- }
- else
- glutGameModeString(gameModeString);
- break;
- case GLUT_KEY_F6:
- // return to default window
- w = ;h = ;
- if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != ) {
- glutLeaveGameMode();
- //init();
- }
- break;
- }
- if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) == )
- sprintf(currentMode,"Current Mode: Window");
- else
- sprintf(currentMode,
- "Current Mode: Game Mode %dx%d at %d hertz, %d bpp",
- glutGameModeGet(GLUT_GAME_MODE_WIDTH),
- glutGameModeGet(GLUT_GAME_MODE_HEIGHT),
- glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE),
- glutGameModeGet(GLUT_GAME_MODE_PIXEL_DEPTH));
- }
- 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;
- }
- }
- }
- void init() {
- // register callbacks
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- glutIgnoreKeyRepeat();
- glutKeyboardFunc(processNormalKeys);
- glutSpecialFunc(pressKey);
- glutSpecialUpFunc(releaseKey);
- glutMouseFunc(mouseButton);
- glutMotionFunc(mouseMove);
- // OpenGL init
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- }
- // -----------------------------------
- // 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
- init();
- // enter GLUT event processing cycle
- glutMainLoop();
- return ;
- }
[译]GLUT教程 - 整合代码6的更多相关文章
- [译]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教程 - 整合代码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教程 - 整合代码3
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III 这里我们准备包含一 ...
- [译]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的游戏模式是为开启 ...
随机推荐
- 【贪心】Mixing Milk
题目描述 The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit ...
- 网页结构——head标签内
之前写网页都很标准的格式,最近一个项目出现了页面闪动等一系列问题[项目不是前后端分离], 所以这边有后台的功劳,有部分后台是不管你页面结构的,在他们操作的时候可能会在,你的head内meta前加内联c ...
- lua的模式匹配
模式: 字符类:(character classes) . all characters %a letters %c control characters %d digits %l lower -ca ...
- 每天一个liunx命令3之awk实现文本文件的抓取
=============================================================================grep -h -s -E 'HUAWEI_9 ...
- 正规化方程Normal Equations解析
如果需要代做算法,可以联系我...博客右侧有联系方式. 一.正规化方程概念 假设我们有m个样本.特征向量的维度为n.因此,可知样本为{(x(1),y(1)), (x(2),y(2)),... ..., ...
- Delphi7 实现窗体全屏方法
设置要全屏的窗体的ALign 属性为ALcLient ,此法最快.当然对我来说,我并不知道这个,所以走了远路,等后来在实现窗体禁止移动的时候才想到了这里,汗.注意:这种全屏方式不会挡了系统的任务栏.. ...
- webpack2.x基础属性讲解
webpack作为构建工具平时作为前端作为优化.模块编程.和分片打包的重要组成部分,大家可能并不陌生,如果没有时刻的去关注文档,那么大家可能不太清楚webpack已经默默然的升级到2.x了,对比1.x ...
- java程序监控tomcat中部署的项目的状态以及控制某些项目的启动停止
原文:http://blog.csdn.net/liuyuqin1991/article/details/49280777 步骤如下: ①:首先授权用户使获得这些权限 You can find the ...
- 发布Android开源库,看这个文章就够了!
最近在Flipboard实习期间写了一个轮播工具,技术上没什么难点,不过动画效果还是不错的,决定改改代码写个库开源出去.项目地址:http://github.com/chengdazhi/Decent ...
- Git:fatal: The remote end hung up unexpectedly
一.配置公共密钥 https://help.github.com/articles/generating-ssh-keys/ 二.设置缓冲值(push文件较大时导致错误) \.git\config [ ...