光照只有叠加,没有覆盖

#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;

}

无意中写了上面的一段代码,在学长的教导下,终于解决了问题,就是光照根据深度检测,和模型glcolor进行深度检测,在深度比较浅时,采用的不同的机制。一种是覆盖,另一种是叠加。深度较深时,是直接抛弃。

如下图所示。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值