NeHe OpenGL lession 4
// lession4.c #include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* ASCII code for teh escape key. */
#define ESCAPE 27 /* The number of our GLUT window */
int window; /* rotation angle for the triangle. */
float rtri = 0.0f; /* rotation angle for the quadrilateral. */
float rquad = 0.0f; /* A general OpenGL initialization function. Sets all of the initial parameters. */
// We call this right after our OpoenGL window is created.
void initGL(int width, int height) {
// This will clear The background color to black.
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0); // Enables clearing of the depth buffer
glDepthFunc(GL_LESS); // The type of test to do.
glEnable(GL_DEPTH_TEST); // Enables depth testing.
glShadeModel(GL_SMOOTH); // Enalbes smooth color shading. glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset the projection matrix. // calculate the aspect ratio of the window.
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW);
} /* The function called when our window is resized (which shouldn't happend,
because we're fullscreen) */
void resizeGLScene(int width, int height) {
if (height == 0) // Prevent a divide by zero if the window is too small.
height = 1; glViewport(0, 0, width, height); // Reset the current viewport and perspective transformation glMatrixMode(GL_PROJECTION);
glLoadIdentity(); gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
} /* The main drawing function. */
void drawGLScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer
glLoadIdentity(); // Reset the view. glTranslatef(-1.5f, 0.0f, -6.0f); // Move left 1.5 units and into the screen 6.0. glRotatef(rtri, 0.0f, 1.0f, 0.0f); // Rotate the triangle on the y axis
// Draw a triangel (in smooth coloring mode)
glBegin(GL_POLYGON); // Start drawing a polygon
glColor3f(1.0f, 0.0f, 0.0f); // set the color to red
glVertex3f(0.0f, 1.0f, 0.0f); // top glColor3f(0.0f, 1.0f, 0.0f); // set the color to green.
glVertex3f(1.0f, -1.0f, 0.0f); // bottom right glColor3f(0.0f, 0.0f, 1.0f); // set the color to blue.
glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left.
glEnd(); // we're done with the polygon (smooth color interpolation) glLoadIdentity(); // make sure we're no longer rotated.
glTranslatef(1.5f, 0.0f, -6.0f); // Move Right 3 Units, and back into the screen 6.0 glRotatef(rquad, 1.0f, 0.0f, 0.0f); // Roate the quad on the x axis
// Draw a square (quadrilateral)
glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade. glBegin(GL_QUADS); // Start drawing a polygon (4 sides);
glVertex3f(-1.0f, 1.0f, 0.0f); // top left.
glVertex3f( 1.0f, 1.0f, 0.0f); // top right.
glVertex3f( 1.0f, -1.0f, 0.0f); // bottom right.
glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left.
glEnd(); // done with the polygon. rtri += 15.0f; // Increase the rotation variable for the triangle
rquad -= 15.0f; // decrease the rotation variable for the quad. // swap the buffers to display, since double buffering in used.
glutSwapBuffers();
} /* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y) {
/* sleep to avoid thrashing this procedure */
usleep(100); /* If escape is pressed, kill everything. */
if (key == ESCAPE) {
/*shut down our window */
glutDestroyWindow(window); /* exit the program...normal termination. */
exit(0);
}
} int main(int argc, char **argv) {
glutInit(&argc, argv); /* Select type of display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffer */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); /* Get a 640 x 480 window */
glutInitWindowSize(640, 480); /* the window starts at teh upper left corner of the screen */
glutInitWindowSize(0, 0); /* Openg a window */
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99"); /* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&drawGLScene); /* Go full screen. This is as soon as possible. */
// glutFullScreen(); /* Even if there are not events, redarw our gl scene. */
glutIdleFunc(&drawGLScene); /* register the function called when our window is resized. */
glutReshapeFunc(&resizeGLScene); /* Register the function claled when teh keyboard is pressed. */
glutKeyboardFunc(&keyPressed); /* Initialize our window. */
initGL(640, 480); /* start event processing engine. */
glutMainLoop(); return 1;
}
主要介绍了旋转
塞塞塞
NeHe OpenGL lession 4的更多相关文章
- Nehe Opengl
http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ Nehe Opengl,据说从基础到高端进阶都是很好的教程,准备学习这个序列,顺便记录下随 ...
- NeHe OpenGL教程 第四十八课:轨迹球
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十七课:CG顶点脚本
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十五课:顶点缓存
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十六课:全屏反走样
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十四课:3D光晕
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十三课:FreeType库
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十一课:体积雾气
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十二课:多重视口
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
随机推荐
- C++函数传值调用
C++的函数的参数调用是传值方式. 想要改变传值调用,有引用和指针两种方式.其中,引用的实现机理也是通过一个指针,但是具体和指针传值的方式又不一样.具体见:C++中的指针与引用 对于指针传值,其实实际 ...
- BZOJ 1974: [Sdoi2010]auction 代码拍卖会( dp )
在1, 11, 111……中选<=8个, + 11..(n个1)拼出所有可能...这些数mod p至多有p中可能, 找出循环的处理一下. 那么dp就很显然了...dp(i, j, k)表示前i种 ...
- Spark源码学习2
转自:http://www.cnblogs.com/hseagle/p/3673123.html 在源码阅读时,需要重点把握以下两大主线. 静态view 即 RDD, transformation a ...
- php功能---删除空目录
header('content-type:text/html;charset:utf-8'); function display($dir){ //判断是否是一个目录 if(!is_dir($dir) ...
- scanf一次给多个变量赋值
本节课程笔记: 一是对多个变量进行赋值,二是对非法输入的值做正确处理(处理方式了解即可,相关函数知识后期讲解),三是美化scanf代码加入输出说明. /* Name:scanf一次给多个变量赋值 Co ...
- Android中自定义属性的使用
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- cmd dos 下 无法显示中文
在做程序开发的时候经常需要在使用命令行进行操作, dos环境本身是不支持中文的,有时候中文编码的问题就像苍蝇一样讨厌,下面提供几种常用的手段解决win7环境下中文显示乱码的问题: 方法一: 修改注册表 ...
- css包含块containing block
<css权威指南>P167: The Containing Block Every element is laid out with respect to its containing b ...
- Codeforces Round#2
A. Winner 题目大意:一些人在玩游戏,给出n次某人获得的分数,现在请计算出胜者,如果不止一个最高分,则取最先达到该分数的人. 题解:模拟得分过程,首先计算出最高分,然后获取先到达或超过最高分的 ...
- jquery阻止默认滑动
$(".swiper-slide").click(function(){ var index = imgarr[$(this).index()]; var content = &q ...