2 sadmooncake sadmooncake 于 2016.01.20 15:24 提问

关于openGL立方体,只有一个面有反射,其他面没有 20C

#include "stdafx.h"
#include
#include
#include
#include
#include
static GLfloat angle=0.0f;

void setNormal(GLfloat* Point1,GLfloat* Point2,GLfloat* Point3){
GLfloat normal[3];
int i;
for(i=0;i<3;++i){
normal[i]=(Point1[i]+Point2[i]+Point3[i])/3;
glNormal3fv(normal);}
}
void setNormal2(GLfloat* Point1,GLfloat* Point2){
GLfloat normal[3];
int i;
for(i=0;i<3;++i){
normal[i]=(Point2[i]-Point1[i]);
glNormal3fv(normal);}
}

void myDisplay(void)

{
static int list = 0;
if (list==0){
GLfloat
Point1[]={0.107991,0.060569,-0.108414},
Point2[]={0.107991,0.060569,0.108414},
Point3[]={-0.107991,0.060569,0.108414},
Point4[]={-0.107991,0.060569,-0.108414},
Point5[]={0.107992,0.277397,-0.108414},
Point6[]={0.107991,0.277397,0.108414},
Point7[]={-0.107992,0.277397,0.108414},
Point8[]={-0.107991,0.277397,-0.108414},
Pointa[]={0.000000,-1.000000,-0.000000},
Pointb[]={0.000000,1.000000,0.000000},
Pointc[]={1.000000,-0.000000,0.000000},
Pointd[]={-0.000000,-0.000000,1.000000},
Pointe[]={-1.000000,-0.000000,-0.000000},
Pointf[]={0.000000,0.000000,-1.000000};

glMatrixMode(GL_PROJECTION);//操作投影矩阵,投影变换就是定义一个可视空间,空间之外的物体不会绘制到屏幕上
glLoadIdentity();//变换前把当前矩阵设置为单位矩阵
gluPerspective(60.0f,1.0f,0.1f,20.0f);//可视空间视角,高宽比,最近可视距离,最远可视距离
glMatrixMode(GL_MODELVIEW);//变换前,设置当前操作的矩阵为模型视图矩阵
glLoadIdentity();//变换前把当前矩阵设置为单位矩阵
gluLookAt(0.8,-0.8,0.0,0.0,0.0,0.0,0.0,1.0,0.0);

    list=glGenLists(1);
    glNewList(list,GL_COMPILE);
    {
GLfloat sun_light_position[]={0.0f,0.0f,0.5f,1.0f};//光源的位置
GLfloat sun_light_ambient[]={0.0f,0.5f,0.0f,1.0f};//光源发出的光经过多次反射后,最终遗留在整个光照环境的强度
GLfloat sun_light_diffuse[]={0.0f,0.0f,0.2f,1.0f};//光源发出的光经过漫反射后,得到的光强
GLfloat sun_light_specular[]={0.0f,1.0f,0.5f,1.0f};//光源发出的光,照到光滑表面经过镜面反射,得到的光强
glLightfv(GL_LIGHT0,GL_POSITION,sun_light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT,sun_light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sun_light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,sun_light_specular);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}



    {
 glPushMatrix();
 glBegin(GL_QUADS);
 //setNormal(Point1,Point2,Point3);
 //setNormal2(Point5,Point1);
 //glColor3f(1.0f,1.0f,1.0f);
 glNormal3fv(Pointa);
 glVertex3fv(Point1);
 glVertex3fv(Point2);
 glVertex3fv(Point3);
 glVertex3fv(Point4);
  glEnd();
  glPopMatrix();

//glTranslatef(50.0f,5.0f,0.0f);
glPushMatrix();
glBegin(GL_QUADS);
//setNormal(Point5,Point6,Point7);
//setNormal2(Point5,Point1);
//glColor3f(1.0f,0.5f,0.0f);
glNormal3fv(Pointb);
glVertex3fv(Point5);
glVertex3fv(Point8);
glVertex3fv(Point7);
glVertex3fv(Point6);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin(GL_QUADS);
//setNormal(Point5,Point8,Point4);
//setNormal2(Point1,Point2);
//glColor3f(1.0f,0.0f,0.0f);
glNormal3fv(Pointc);
glVertex3fv(Point1);
glVertex3fv(Point5);
glVertex3fv(Point6);
glVertex3fv(Point2);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin(GL_QUADS);
//setNormal(Point5,Point8,Point4);
//setNormal2(Point1,Point2);
//glColor3f(1.0f,0.0f,0.0f);
glNormal3fv(Pointd);
glVertex3fv(Point2);
glVertex3fv(Point6);
glVertex3fv(Point7);
glVertex3fv(Point3);
glEnd();
glPopMatrix();

 glPushMatrix();
 glBegin(GL_QUADS);
 //setNormal(Point7,Point6,Point2);
// setNormal2(Point2,Point1);
 //glColor3f(0.0f,0.0f,1.0f);
 glNormal3fv(Pointe);
 glVertex3fv(Point3);
 glVertex3fv(Point7);
 glVertex3fv(Point8);
 glVertex3fv(Point4);
  glEnd();
  glPopMatrix();
 glPushMatrix();
 glBegin(GL_QUADS);
 //setNormal(Point6,Point5,Point1);
 //setNormal2(Point1,Point4);
 //glColor3f(1.0f,0.0f,1.0f);
  glNormal3fv(Pointf);
 glVertex3fv(Point5);
 glVertex3fv(Point1);
 glVertex3fv(Point4);
 glVertex3fv(Point8);

glEnd();
glPopMatrix();
}
glEndList();
//glEnable(GL_DEPTH_TEST);

}
//glShadeModel(GL_SMOOTH);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除颜色/深度缓冲
glPushMatrix();
glRotatef(angle,1,0.5,1);
glCallList(list);
glPopMatrix();

glPushMatrix();
    GLfloat moon_mat_ambient[]={0.0f,0.0f,0.5f,1.0f};
GLfloat moon_mat_diffuse[]={0.0f,0.0f,0.5f,1.0f};
GLfloat moon_mat_specular[]={0.0f,0.0f,1.0f,1.0f};
GLfloat moon_mat_emission[]={0.5f,0.0f,0.1f,0.0f};
GLfloat moon_mat_shininess=30.0f;
glMaterialfv(GL_FRONT,GL_AMBIENT,moon_mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,moon_mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,moon_mat_specular);
glMaterialfv(GL_FRONT,GL_EMISSION,moon_mat_emission);
glMaterialf(GL_FRONT,GL_SHININESS,moon_mat_shininess);
glPopMatrix();
 glutSwapBuffers();//使用双缓冲

}
void myIdle(void)
{
angle+=1.0f;
if(angle >=360.0f)
angle=0.0f;
myDisplay();
}
int main(int argc, _TCHAR* argv[])

{

glutInit(&argc,(char**)argv); //初始化glut,必须调用,复制黏贴这句话即可

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); //设置显示方式,RGB、单缓冲。当然还有GLUT_INDEX索引颜色 GLUT_DOUBLE双缓冲(Qt中看到过双缓冲)

glutInitWindowPosition(200,200); //位置

glutInitWindowSize(400,400);//窗口大小
//glutInitWindowSize(WIDTH,HEIGHT);

glutCreateWindow("第一个OpenGL程序"); //创建窗口,设置标题

glutDisplayFunc(&myDisplay); // 当绘制窗口时调用myDisplay,像Cocos2d-x刷帧Draw中的操作

glutIdleFunc(&myIdle);

glutMainLoop(); //消息循环

return 0;

}

这是代码,六个面只有一个能反射光线,求解惑

2个回答

qq_28249373
qq_28249373   2016.01.20 17:36

法向量的方向是否设置正确?如果法向量方向与光源方向夹角小于90度,就会反射光照,如果大于90度,就不会反射光线。对于六面体,六个面的法向量都朝外,通过调整光源位置,才能看到反射光。

qingrenufo
qingrenufo   2016.01.22 11:51

http://wenku.baidu.com/link?url=Ts-d_ZtY1i-MIdOdUamW8cKe5JzL7MTiTnfgFZY0H9c2G95J8A5cOmamrtJt1NRjUV3_Qn_vIPCvMsPZ8luDS35GbMCjaFIpCyWhkKRdgsG 这个教程里面有你想要的,关于天体运动那一块的。再看看光源的方向和平面的法向量的位置是否设置正确

Csdn user default icon
上传中...
上传图片
插入图片