NeHe OpenGL lession 1

// 
//  This code was created by Jeff Molofee '99 (ported to
//   Linux/GLUT by Richard Campbell '99)
// If you've found this code userful, please let me know.
// Visit me at www.demonews.com/hosted/nehe
// (email Richard Camepbell at ulmont@bellsouth.net)
//
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* ascii code fo 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 color to black
	glClearDepth(1.0); // Enables Clearing of the depth buffer
	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 deivce by zero if the window is too small
		height = 1;

	glViewport(0, 0, width, height); // Reset the curent 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 functions. */
void drawGLScene(void) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer 
	glLoadIdentity(); // Reset the view

	// since this is double buffered, swap the buffers to display what just got drawn.
	glutSwapBuffers();
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y) {	
    /* avoid threashing this procedure */
	usleep(100);

	/* If escape is pressed, kill everthing. */
	if (key == ESCAPE) {
		glutDestroyWindow(window);

		/* exit the program ... normal termination. */
		exit(0);
	}
}

int main(int argc, char **argv) {
	/* Initialize GLUT state -glut will take any command line argumetns taht pertain to it or
	X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
	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 the screen */
	glutInitWindowPosition(0, 0);

	glutCreateWindow("Jeff molofee's GL Code Tutorial .. NEH3 '99'");

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

	/* Go fullscreen. This is as soon as possible. */
	//glutFullScreen();

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

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

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

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

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



测试在Mac OS 运行OK

gcc -o lession lession1.c -Wno-deprecated -framework OpenGL -framework GLUT


运行之后是一个黑色的window




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值