#include "stdafx.h"
#include<gl/glew.h>
#include <stdlib.h>
#include<iostream>
#include <GL/glut.h>
#include<gl/gl.h>
#include <windows.h>
void init(void)
{
// all parameters of lighting attrubutes
float light_specular[]={0.8, 0.8, 0.8, 1.0};
float light_ambient[]={0.8, 0.8, 0.8, 1.0};
float light_diffuse[]={0.8, 0.8, 0.8, 1.0};
float light_position[]={0.0, 0.0, 3.0,0.0};
float spot_direction[]={0.0, 0.0, 0.0,0.0};
glLight0 gets the parameters
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
/*
// attenuation parameters are set
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);
// spot cutoff and direction and eponent
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);
*/
// all parameters of material attributes
//float mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//float mat_shininess[] = { 50.0 };
//float mat_ambient[]={1.0, 1.0, 1.0, 1.0};
//float mat_diffuse[]={1.0, 1.0, 1.0, 1.0};
// material is set
//glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
// glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR,mat_specular);
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// ligthtmodel information
//GLfloat lightmodel_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightmodel_ambient);
// fundanmental information
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
// enable lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glColorMask(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//left teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColorMask(0.0,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(-0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
//right teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColorMask(1.0,0.0,0.0,0.0);
glTranslatef(0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glFlush();
//left teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColorMask(0.0,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(-0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
//right teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColorMask(1.0,0.0,0.0,0.0);
glTranslatef(0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-5, +5, -5, +5, -5.0, 5.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
#include<gl/glew.h>
#include <stdlib.h>
#include<iostream>
#include <GL/glut.h>
#include<gl/gl.h>
#include <windows.h>
void init(void)
{
// all parameters of lighting attrubutes
float light_specular[]={0.8, 0.8, 0.8, 1.0};
float light_ambient[]={0.8, 0.8, 0.8, 1.0};
float light_diffuse[]={0.8, 0.8, 0.8, 1.0};
float light_position[]={0.0, 0.0, 3.0,0.0};
float spot_direction[]={0.0, 0.0, 0.0,0.0};
glLight0 gets the parameters
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
/*
// attenuation parameters are set
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);
// spot cutoff and direction and eponent
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);
*/
// all parameters of material attributes
//float mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//float mat_shininess[] = { 50.0 };
//float mat_ambient[]={1.0, 1.0, 1.0, 1.0};
//float mat_diffuse[]={1.0, 1.0, 1.0, 1.0};
// material is set
//glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
// glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR,mat_specular);
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// ligthtmodel information
//GLfloat lightmodel_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightmodel_ambient);
// fundanmental information
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
// enable lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glColorMask(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//left teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColorMask(0.0,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(-0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
//right teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColorMask(1.0,0.0,0.0,0.0);
glTranslatef(0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glFlush();
//left teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColorMask(0.0,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(-0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
//right teapot
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColorMask(1.0,0.0,0.0,0.0);
glTranslatef(0.2,0.0,0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-5, +5, -5, +5, -5.0, 5.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
无意中写了上面的一段代码,在学长的教导下,终于解决了问题,就是光照根据深度检测,和模型glcolor进行深度检测,在深度比较浅时,采用的不同的机制。一种是覆盖,另一种是叠加。深度较深时,是直接抛弃。
如下图所示。