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;
}

主要介绍了旋转

塞塞塞

时间: 2024-10-11 11:21:22

NeHe OpenGL lession 4的相关文章

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(voi

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 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