ITeye博客

使用深度缓冲区进行三维混合代码

sunlong · 2010-08-31 20:10 · OPENGL · 移动开发 · Markdown

#include <GLUT.H>
#include <stdio.h>
#include <tchar.h>

GLuint sphereList, cubeList;

static float solidZ = 8.0;
static float transparentZ = -8.0;
#define ZINC 0.4

void init()
{
	GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 0.15f};
	GLfloat mat_shininess[] = {100.0};
	
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

	GLfloat position[] = {0.5f, 0.5f, 1.0f, 0.0};
	glLightfv(GL_LIGHT0, GL_POSITION, position);

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

	sphereList = glGenLists(1);
	glNewList(sphereList, GL_COMPILE);
	glutSolidSphere(0.4, 16, 16);
	glEndList();

	cubeList = glGenLists(1);
	glNewList(cubeList, GL_COMPILE);
	glutSolidCube(0.6);
	glEndList();
	//glEnable(GL_POLYGON_SMOOTH);
}

void display()
{	
	GLfloat mat_solid[] = {0.75, 0.75, 0.0, 1.0};
	GLfloat mat_zero[] = {0.0, 0.0, 0.0, 1.0};
	GLfloat mat_transparent[] = {0.0, 0.8, 0.8, 0.6};
	GLfloat mat_emission[] = {0.0, 0.3, 0.3, 0.6};

	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
		glTranslatef(-0.15, -0.15, solidZ);
		glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
		glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
		glCallList(sphereList);
	glPopMatrix();

	glPushMatrix();
		glTranslatef(0.15, 0.15, transparentZ);
		glRotatef(15.0, 1.0, 1.0, 0.0);
		glRotatef(30.0, 0.0, 1.0, 0.0);
		glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
		glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
		glEnable(GL_BLEND);
		glDepthMask(GL_FALSE);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		glCallList(cubeList);
		glDepthMask(GL_TRUE);
		glDisable(GL_BLEND);
	glPopMatrix();
	
	glutSwapBuffers();
	glFlush();
}
void idleFunc()
{
	if(solidZ<=-8.0 || transparentZ>=8.0)
	{
		glutIdleFunc(NULL);
	}
	else
	{
		solidZ -= ZINC;
		transparentZ += ZINC;
		glutPostRedisplay();
	}	
}
void reshape(int width, int height)
{
	glViewport(0, 0, width, height);
	float ratio = (float)width/(float)height;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glFrustum(-ratio, ratio, -1, 1, 1, 100);
	if(width<= height)
		glOrtho(-1.5, 1.5, -1.5*(GLfloat)height/(GLfloat)width,
			1.5*(GLfloat)height/(GLfloat)width, -10.0, 10.0);
	else
		glOrtho(-1.5*(GLfloat)width/(GLfloat)height, 1.5*(GLfloat)width/(GLfloat)height, -1.5, 1.5, -10.0, 10.0);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
	case 'r':
		solidZ = 8.0;
		transparentZ = -8.0;
		glutPostRedisplay();
		break;
	case 'a':
		solidZ = 8.0;
		transparentZ = -8.0;
		glutIdleFunc(idleFunc);
		break;
	case 27: exit(0);
	default: break;
	}
	
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(600, 600);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	//glutIdleFunc(idleFunc);
	glutKeyboardFunc(keyboard);
	glutMainLoop();
	return 0;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。