3D光照渲染小球

#include<GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<math.h>


void init(void)
{	
	printf("  init");
	GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
	GLfloat mat_shininess[]={50.0};
	GLfloat light_position[]={1.0,  1.0,  1.0,  0.0};  //r-l u-d f-b	
	GLfloat  diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};

	glClearColor(0.3,0.8,0.8,0.0); //bgc
	glColor3ub(23, 17, 215);
	glShadeModel(GL_SMOOTH);

	glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
	glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); 
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular); 
	glLightfv(GL_LIGHT0,GL_POSITION,light_position); 

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
}

void initDisplay(void)  
{

	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glutSolidSphere(0.4,40,50);
	glutSwapBuffers();
}


void reshape(int w,int h)
{
    printf("  reshape");
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
 	   glOrtho(-1.5,1.5,-1.5*(GLfloat)(h)/(GLfloat)(w),1.5*(GLfloat)(h)/(GLfloat)(w), -10.0,10.0);
    else 
       glOrtho(-1.5*(GLfloat)(w)/(GLfloat)(h),1.5*(GLfloat)(w)/(GLfloat)(h),-1.5,1.5,-10.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 
}


int main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(400,740);
	glutInitWindowPosition(300,20);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(initDisplay);
	glutReshapeFunc(reshape);

	glutMainLoop();
	return 0;
}

你可能感兴趣的:(3D光照渲染小球)