程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> NeHe OpenGL lession 2

NeHe OpenGL lession 2

編輯:C++入門知識

NeHe OpenGL lession 2


 

#include 
#include 
#include 
#include 
#include  // Header file for sleeping

/* ASCII code for the escape key. */
#define  ESCAPE 27

/* The number of our GLUT window */
int window;

/* A general OpenGL initialization function. Sets all of the initial parameters. */
/* We call this right after our OpenGL window is created. */
void initGL(int width, int height)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	// This will clear the background oclor to black
	glClearDepth(1.0); 			// Enable clearing of the depth buffer 
	glDepthFunc(GL_LESS); 		// The type depth test to do
	glEnable(GL_DEPTH_TEST); 	// Enables depth testing
	glShadeModel(GL_SMOOTH); 	// Enables 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 devide by zero if the window is too small
		height = 1;

	glViewport(0, 0, width, height); // Reset teh 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.
	glLoadIdentity(); 		// Reset the view

	glTranslatef(-1.5f, 0.0f, -6.0f); 	// Move left 1.5 units and into the screen 6.0

	// Draw a triangle
	glBegin(GL_POLYGON);
		glVertex3f( 0.0f,  1.0f, 0.0f); // Top
		glVertex3f( 1.0f, -1.0f, 0.0f); // Bottom Right
		glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
	glEnd();		// We're doen with the polygon

	glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 units (將下面的四邊形往右移動)

	// Draw a square (quadrilateral) 
	glBegin(GL_QUADS);						// Start drawing a polygon (4 sided)
		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

	// swap buffers to display, since we're doouble buffered.
	glutSwapBuffers();
}

/* the function claled whever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
	/* avoid thrashing this procedure */
	/* If escape is pressed. kill everything. */
	if (key == ESCAPE) {
		/* shut down our window */
		glutDestroyWindow(window);

		/* exit teh 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 the upper left corner of teh screen */
	glutInitWindowPosition(0, 0);

	/* Open a window */
	window = glutCreateWindow(Jeff Molefee's GL Code Tutorial .. NeHe '99);

	/* Register the function to do all our OpenGL drwing. */
	glutDisplayFunc(&drawGLScene);

	/* GO fullscreen. This is the soonest we could possibly go fullscreen. */
	// glutFullScreen();

	/* Even if there are no events, redraw our gl scene. */
	glutIdleFunc(&drawGLScene);

	/* Register the function called when our window is resized. */
	glutReshapeFunc(&resizeGLScene);

	/* Register teh function claled when the keyboard is pressed. */
	glutKeyboardFunc(&keyPressed);

	/* Initialzie our window */
	initGL(640, 480);

	/* Start event processing engine */
	glutMainLoop();

	return 0;
}




 

Mac OS X 運行命令:

 

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

 

$: ./lession

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved