照明在opengl中不起作用[关闭]
Posted
技术标签:
【中文标题】照明在opengl中不起作用[关闭]【英文标题】:Lighting not working in opengl [closed] 【发布时间】:2017-06-26 17:43:05 【问题描述】:我无法让照明仅在我的地面的下降部分工作。以下是我的地面和下降代码(使其成为沟渠):
static void ground(double x, double y, double z, double dx, double dy,
double dz)
float white[] = 1,1,1,1;
float Emission[] = 0.0,0.0,0.01*emission,1.0;
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,Emission);
// Save transformation
glPushMatrix();
// Offset, scale and rotate
glTranslated(x,y,z);
glScaled(dx, dy, dz);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(0.5, 1.0, 0.5);
glNormal3f(0,0,0);
glTexCoord2f(0.0, 0.0); glVertex3f(-100, 0, -300);
glTexCoord2f(300.0,0.0); glVertex3f(-100,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-3,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(-3,0,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.5);
glNormal3f(0,0,-1);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.99,0,-300);
glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.5);
glNormal3f(0,0,1);
glTexCoord2f(0.0, 0.0); glVertex3f(0.99,-1,-300);
glTexCoord2f(300.0,0.0); glVertex3f(0.99,-1,300);
glTexCoord2f(300.0,300.0); glVertex3f(2.99,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(2.99,0,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(0.5, 1.0, 0.5);
glNormal3f(0,0,0);
glTexCoord2f(0.0, 0.0); glVertex3f(2.99, 0, -300);
glTexCoord2f(300.0,0.0); glVertex3f(2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(100,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(100,0,-300);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
所以没有照明正常工作的代码是“glBegin(GL_QUADS)”的中间两个sn-ps
【问题讨论】:
@BenSteffan 这是一个班级项目,所以我一直在使用我被告知要使用的东西。 在minimal reproducible example 中编辑。 @RyanFasching 您是否被明确告知要使用此版本的 OpenGL? Ben 并不是说 OpenGL 已经过时了,他是说这段代码特别适用于旧版本的 OpenGL。使用最新版本,除非您被明确告知使用某个版本。另外,请edit 你的问题包含minimal reproducible example,以及实际的问题描述——“我无法让它工作”是没用的。你想让它做什么?它在做什么呢?在这种情况下,屏幕截图可能会有所帮助。 【参考方案1】:这只是 GIGO 的一个实例(垃圾进,垃圾出):
glNormal3f(0,0,0);
不。这不是一个有效的法线向量,并且会完全破坏任何光照计算。
下一个
glNormal3f(0,0,-1); glTexCoord2f(0.0, 0.0); glVertex3f(-2.99,0,-300); glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300); glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300); glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
至少是一些非空向量,但它与您描述的面部不正常,所以照明会出错。
【讨论】:
以上是关于照明在opengl中不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章