neHe OpenGL lession 5

lession 5 3D

// lession 5 3D

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

float rtri; 		// angle for the triangel
float rquad; 		// angle for the quad

void initGL(void)  // Create some everyday functions
{
	glShadeModel(GL_SMOOTH); 				// Enable smooth shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f); 	// black background
	glClearDepth(1.0f);						// depth buffer setup
	glEnable(GL_DEPTH_TEST); 				// Depth buffer setup
	glDepthFunc(GL_LEQUAL); 				// the type of depth testing to do.
	glEnable(GL_COLOR_MATERIAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display(void) // create the siplay functions
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 	// Clear screen and depth buffer
	glLoadIdentity(); 		// reset the current modelview matrix

	glPushMatrix();
	{
		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);
		glBegin(GL_TRIANGLES);
			glColor3f(1.0f, 0.0f, 0.0f); 	// red
			glVertex3f(0.0f, 1.0f, 0.0f); 	// top of triangle (Front);

			glColor3f(0.0f, 1.0f, 0.0f); 	// green
			glVertex3f(-1.0f, -1.0f, 1.0f); // Left of triangle (Front)

			glColor3f(0.0, 0.0, 1.0); 		// blue
			glVertex3f(1.0f, -1.0f, 1.0f); 	// right of triangle (Front)

			glColor3f(1.0, 0.0, 0.0); 		// red
			glVertex3f(0.0f, 1.0f, 0.0f);   // top of triangle (right)

			glColor3f(0.0f, 0.0f, 1.0f); 	// blue
			glVertex3f(1.0f, -1.0f, 1.0f); 	// left of triangle (right)

			glColor3f(0.0, 1.0, 0.0); 		// green
			glVertex3f(1.0f, -1.0f, -1.0f); // right of triangle (right);

			glColor3f(1.0f, 0.0f, 0.0f); 	// red
			glVertex3f(0.0f, 1.0f, 0.0f);   // top of triangle (back);

			glColor3f(0.0f, 1.0f, 0.0f); 	// green
			glVertex3f(1.0f, -1.0f, -1.0f); // left of triangle (back)

			glColor3f(0.0f, 0.0f, 1.0f); 	// blue
			glVertex3f(-1.0f, -1.0f, -1.0f); // right of triangle (back)

			glColor3f(1.0f, 0.0f, 0.0f); 	// red
			glVertex3f(0.0f, 1.0f, 0.0f);	// top of triangle (left)

			glColor3f(0.0f, 0.0f, 1.0f); 	// blue
			glVertex3f(-1.0f, -1.0f, -1.0f); // left of triangle (left)

			glColor3f(0.0f, 1.0f, 0.0f); 	// green
			glVertex3f(-1.0f, -1.0f, 1.0f); // right of triangle (left);
		glEnd();

		glLoadIdentity();
		glTranslatef(1.5f, 0.0f, -6.0f);
		glRotatef(rquad, 1.0f, 0.0f, 0.0f);  // Rotate the quad on the x axis
		glColor3f(0.5f, 0.5f, 1.0f); 		// Set teh color to blue on time only
		glBegin(GL_QUADS);
			glColor3f(0.0f, 1.0f, 0.0f); 		// Set the color to blue
			glVertex3f( 1.0f,  1.0f, -1.0f); 		// top right of the quad (top)
			glVertex3f(-1.0f,  1.0f, -1.0f);  		// top left of the quad (top)
			glVertex3f(-1.0f,  1.0f,  1.0f); 		// bottom left of the quad (top)
			glVertex3f( 1.0f,  1.0f,  1.0f); 		// bottom right of the quad (top)

			glColor3f(1.0f, 0.5f, 0.0f); 		// Set the color to Orange
			glVertex3f( 1.0f, -1.0f, -1.0f); 	 	// top right of the quad (bottom)
			glVertex3f(-1.0f, -1.0f, -1.0f); 		// top left of the quad (bottom)
			glVertex3f(-1.0f, -1.0f,  1.0f); 		// bottom left of the quad (bottom)
			glVertex3f( 1.0f, -1.0f,  1.0f); 		// bottom right of the quad (bottom)

			glColor3f(1.0f, 0.0f, 0.0f); 		// Set the color to red
			glVertex3f( 1.0f,  1.0f,  1.0f); 		// top right of the quad (front)
			glVertex3f(-1.0f,  1.0f,  1.0f);		// top left of the quad (front)
			glVertex3f(-1.0f, -1.0f,  1.0f);		// bottom left of the quad (front)
			glVertex3f( 1.0f, -1.0f,  1.0f); 		// bottom right of the quad (front)

			glColor3f(1.0f, 1.0f, 0.0f);		// Set the color to Yellow
			glVertex3f( 1.0f,  1.0f, -1.0f);		// top right of the quad (back)
			glVertex3f(-1.0f,  1.0f, -1.0f);		// top left of the quad (back)
			glVertex3f(-1.0f, -1.0f, -1.0f); 		// bottom left of the quad (back)
			glVertex3f( 1.0f, -1.0f, -1.0f); 		// bottom right of the quad (back)

			glColor3f(0.0f, 0.0f, 1.0f); 		// Set the color to blue
			glVertex3f(-1.0f,  1.0f,  1.0f); 		// top right of the quad (left)
			glVertex3f(-1.0f,  1.0f, -1.0f); 		// top left of the quad (left)
			glVertex3f(-1.0f, -1.0f, -1.0f); 		// bottom left of the quad (left)
			glVertex3f(-1.0f, -1.0f,  1.0f); 		// bottom right of the quad (left)

			glColor3f(1.0f, 0.0f, 1.0f); 		// Set the color to Violet
			glVertex3f( 1.0f,  1.0f, -1.0f); 		// top right of the quad (right)
			glVertex3f( 1.0f,  1.0f,  1.0f); 		// top left of the quad (right)
			glVertex3f( 1.0f, -1.0f,  1.0f); 		// bottom left of the quad (right)
			glVertex3f( 1.0f, -1.0f, -1.0f); 		// bottom right of the quad (right)

		glEnd();
	}
	glPopMatrix();

	rtri += 0.2f; 		// increase the rotation variable for the triangle (NEW)
	rquad -= 0.2f; 		// decrease the rotation variable for the quad (NEW)

	glutSwapBuffers();  // Swap the buffers to not be left with a clear screen.
}

// Create the reshape function (the viewport)
void reshape(int width, int height)
{
	if (height == 0)
		height = 1;

	glViewport(0, 0, width, height); 		// Reset the current viewport

	glMatrixMode(GL_PROJECTION);			// Select the projection matrix
	glLoadIdentity();						// Rest the projection matrix

	// Calculate the aspect ratio of the window
	gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);				// Select the Modelview Matrix
	glLoadIdentity();						// Reset the Modelview Matrix
}

// Create keyboard function
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
		case 27:
			exit(0);
			break;
		default:
			break;
	}
}

// Create Special Function (required for arrow keys)
void arrow_keys(int a_keys, int x, int y)
{
	switch (a_keys) {
		case GLUT_KEY_UP:  		// When Up Arrow is pressed...
			glutFullScreen(); 	// Go into full screen mode
			break;
		case GLUT_KEY_DOWN: 	// When Down arrow is pressed....
			glutReshapeWindow(500, 500); 	// go into a 500 by 500 window
			break;
		default:
		break;
	}
}

// create main function for bringing it all together
int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutInitWindowSize(500, 500);
	glutCreateWindow("NeHe's OpenGL Framework");
	// glutFullScreen();
	initGL();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutSpecialFunc(arrow_keys);
	glutIdleFunc(display);
	glutMainLoop();

	return 0;
}

Mac 终端运行:

$:  clang -o lession lession5.c -Wno-deprecated -framework OpenGL -framework GLUT

$: ./lession

时间: 2024-10-12 03:33:25

neHe OpenGL lession 5的相关文章

NeHe OpenGL lession 2

#include <GLUT/GLUT.h> #include <OpenGL/OpenGL.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> // Header file for sleeping /* ASCII code for the escape key. */ #define ESCAPE 27 /* The number of our GLUT window

neHe OpenGL lession 3

#include <GLUT/GLUT.h> #include <OpenGL/OpenGL.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* ASCII code for the escape key. */ #define ESCAPE 27 /* The number of our GLUT window */ int window; /* A general

neHe OpenGL lession 6

加载纹理 // lession6 // Mac OS X / Linux // linux <GL/glut.h> <GL/gl.h> <GL/glu.h> #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* ascii code for t

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;

NeHe OpenGL lesson 7

HeHe OpenGL 第七节,纹理部分,光照初步.由于Mac 上面没有Page键,所以用G . H键替换 // NeHe OpenGL lession 7 // Texture Filters, Lighting & keyboard Control // /** * Linux * #include <GL/glut.h> * #include <GL/gl.h> * #include <GL/glu.h> */ #include <GLUT/GLUT

NeHe OpenGL教程 第三十八课:资源文件

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第三十八课:资源文件 从资源文件中载入图像: 如何把图像数据保存到*.exe程序中,使用Windows的资源文件吧,它既简单又实用. 欢迎来到NeHe教程第38课.离上节课的写作已经有些时日了,加上写了一整天的code,也许笔头已经

NeHe OpenGL教程 第四十七课:CG顶点脚本

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第四十七课:CG顶点脚本 CG 顶点脚本 nVidio的面向GPU的C语言,如果你相信它就好好学学吧,同样这里也只是个入门.记住,类似的语言还有微软的HLSL,OpenGL的GLSL,ATI的shaderMonker.不要选错哦:)

NeHe OpenGL教程 第二十三课:球面映射

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第二十三课:球面映射 球面映射: 这一个将教会你如何把环境纹理包裹在你的3D模型上,让它看起来象反射了周围的场景一样. 球体环境映射是一个创建快速金属反射效果的方法,但它并不像真实世界里那么精确!我们从18课的代码开始来创建这个教程

NeHe OpenGL教程 第三十五课:播放AVI

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第三十五课:播放AVI 在OpenGL中播放AVI: 在OpenGL中如何播放AVI呢?利用Windows的API把每一帧作为纹理绑定到OpenGL中,虽然很慢,但它的效果不错.你可以试试. 首先我得说我非常喜欢这一章节.Jonat