[译]GLUT教程 - 整合代码1
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far
以下是前面几节的完整整合代码:
#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 = ; 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() { 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(1.0f, 0.5f , 0.5f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,,);
} void computePos(float deltaMove) { x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
} void computeDir(float deltaAngle) { angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
} void renderScene(void) { if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle); // 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.0,,j * 10.0);
drawSnowMan();
glPopMatrix();
} glutSwapBuffers();
} void pressKey(int key, int xx, int yy) { switch (key) {
case GLUT_KEY_LEFT : deltaAngle = -0.01f; break;
case GLUT_KEY_RIGHT : deltaAngle = 0.01f; break;
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_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = ;break;
}
} 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); glutSpecialFunc(pressKey); // here are the new entries
glutIgnoreKeyRepeat();
glutSpecialUpFunc(releaseKey); // OpenGL init
glEnable(GL_DEPTH_TEST); // enter GLUT event processing cycle
glutMainLoop(); return ;
}
[译]GLUT教程 - 整合代码1的更多相关文章
- [译]GLUT教程 - 整合代码2
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...
- [译]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教程 - 整合代码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的游戏模式是为开启 ...
随机推荐
- springboot的配置文件
一.springboot配置文件值的注入 @ConfigurationProperties(prefix = "xxx") 实例代码: @Component @Configurat ...
- Android开发之onCreate
最近看了一些开发人员做的APP开发,虽然都实现了比较完美的功能,不过很多地方很是牵强!我觉得:作为一个合格软件工程师,最重要的不是你写过多少代码,而是你研究过多少代码!所以,我更倡导多研究系统原本定制 ...
- Ubuntu 16.04没有/etc/default/rcS文件的UTC设置选项的问题解决
继续上一篇文章介绍了CentOS的时钟设置:http://www.cnblogs.com/EasonJim/p/8111747.html,大致的知道了BIOS在Linux的时区设置. 而现在关心的问题 ...
- 1· linux命令:查看依赖的共享库 - ldd命令
今天使用qt链接mysql的时候,发现提示driver not load 而出现此错误一般是没有mysql驱动的原因,但是qt5.4已经提供了mysql驱动的 查看plugins/sqldrivers ...
- JAVA常见算法题(十六)
package com.xiaowu.demo; //猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个: //第二天早上又将剩下的桃子吃掉一半,而且又多吃了一个. //以后 ...
- JAVA常见算法题(十四)
package com.xiaowu.demo; /** * 输入某年某月某日,判断这一天是这一年的第几天? * * * @author WQ * */ public class Demo14 { p ...
- JAVA常见算法题(七)
package com.xiaowu.demo; /** * 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. * * @author WQ * */ public class De ...
- 5分钟教程:如何通过UART获得root权限
写在前面的话 你知道物联网设备以及其他硬件制造商是如何调试和测试自家设备的吗?没错,绝大多数情况下,他们都会留下一个串行接口,这样就可以利用这个接口并通过shell来读取实时的调试日志或与硬件进行交互 ...
- 使用Spring的AbstractRoutingDataSource实现多数据源切换
https://www.cnblogs.com/softidea/p/7127874.html?utm_source=itdadao&utm_medium=referral https://b ...
- 【Hadoop】Hadoop MR异常处理
1.代码示例 package com.ares.hadoop.mr.flowsort; import java.io.IOException; import org.apache.hadoop.con ...