这是我的照明代码
glewInit();glClearcolor(0.0,0.0,0.0);glClear(GL_color_BUFFER_BIT) glShadeModel(GL_SMOOTH);//light position and colourGLfloat light_position[] = { 0.0,20.0,0.0 };GLfloat white_light[] = {0.8,0.8,0.0};GLfloat diff_light[] = {1.0,1.0,0.0};GLfloat spec_light[] = {1.0,0.0};gllightfv(GL_liGHT0,GL_AMBIENT,white_light);gllightfv(GL_liGHT0,GL_DIFFUSE,diff_light);gllightfv(GL_liGHT0,GL_specular,spec_light);gllightfv(GL_liGHT0,GL_position,light_position);//ambIEnt lightGLfloat ambIEnt[] = {0.3,0.3,0.3};glMaterialfv(GL_FRONT,ambIEnt);//diffuse material componentGLfloat diff[] = {0.6,0.6,0.6};glMaterialfv(GL_FRONT,diff);//specular material componentGLfloat WhiteSpec[] = {1,1,1};glMaterialfv(GL_FRONT,WhiteSpec);GLfloat shininess = 50;glMaterialf(GL_FRONT,GL_SHINInesS,shininess);//ENABLE liGHTING AND DEPTH TESTglEnable(GL_liGHTING);glEnable(GL_liGHT0);glEnable(GL_DEPTH_TEST);
这是我创造我的海洋的类方法
glcolor3f(0,1);glPushmatrix();//enable texturingglEnable(GL_TEXTURE_2D); glEnable(GL_BLEND);glBlendFunc(GL_SRC_Alpha,GL_ONE_MINUS_SRC_Alpha);glBindTexture(GL_TEXTURE_2D,seaTex);glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);for(int i = 0,k = 0; i < (getWIDth()/10); i++){ for(int j = 0; j < (getLength()/10); j++){ if(i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength){ int nextK = k+1; if(nextK == Sea::sinArrayLength){ nextK = 0; } if(i == Sea::waveLoc1+Sea::sinArrayLength){ //front of wave glBegin(GL_polyGON); glnormal3f(0.0f,1.0f,0.0f); glTexCoord2f(0.0,1.0);glVertex3f(Sea::seaGrID[i][j].x,Sea::sinVals[nextK],Sea::seaGrID[i][j].z); glTexCoord2f(0.0,0.0);glVertex3f(Sea::seaGrID[i][j+1].x,Sea::seaGrID[i][j+1].z); glTexCoord2f(1.0,0.0);glVertex3f(Sea::seaGrID[i+1][j+1].x,Sea::seaGrID[i+1][j+1].y,Sea::seaGrID[i+1][j+1].z); glTexCoord2f(1.0,1.0);glVertex3f(Sea::seaGrID[i+1][j].x,Sea::seaGrID[i+1][j].y,Sea::seaGrID[i+1][j].z); glEnd(); }else{ //rest of wave glBegin(GL_polyGON); glnormal3f(0.0f,Sea::sinVals[k],Sea::seaGrID[i+1][j].z); glEnd(); } }else{ //draw flat sea glBegin(GL_polyGON); glnormal3f(0.0f,0.0f); glTexCoord2f(0.0,Sea::seaGrID[i+1][j].z); glTexCoord2f(0.0,Sea::seaGrID[i+1][j+1].z); glTexCoord2f(1.0,Sea::seaGrID[i][j+1].y,Sea::seaGrID[i][j+1].z); glTexCoord2f(1.0,Sea::seaGrID[i][j].y,Sea::seaGrID[i][j].z); glEnd(); } } //increment k if i is in the area of the wave if(k < Sea::sinArrayLength-1 && (i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength)){ k++; }else if(k == Sea::sinArrayLength){ k = 0; }}if(Sea::waveLoc1 < 100 && Sea::waveInc == Sea::waveSpeedlimiter){ Sea::waveLoc1 +=1;}else if(Sea::waveLoc1 >= 100){ Sea::waveLoc1 = 0;}//limits speed of waveif(Sea::waveInc < Sea::waveSpeedlimiter){ Sea::waveInc++;}else{ Sea::waveInc = 0;}//disable texturinggldisable(GL_BLEND);gldisable(GL_TEXTURE_2D);glPopMatrix();
然后在我的display()函数中调用它,如下所示
glClear(GL_color_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_PROJECTION);glLoadIDentity();gluPerspective(45.0f,(GLfloat)screenWIDth/(GLfloat)screenHeight,0.1f,1000.0f);glMatrixMode(GL_MODELVIEW);glLoadIDentity();camera.updateCameraposition(mouse_x,mouse_y,screenWIDth,screenHeight);sea.buildSeaPlane();scene.buildEdges();glPushmatrix(); glcolor3f(0,0);glTranslatef(0,20,200);model.speeddisplayFacenormals();glPopMatrix();glPushmatrix();plane.updatePlaneposition();glBegin(GL_polyGON); glnormal3f(0.0f,0.0f); glVertex3f(0,25,10); glVertex3f(2,20); glVertex3f(0,20);glEnd();glPopMatrix();glFlush();
知道为什么我看不到这个吗?
更新:
如果我禁用纹理,我可以看到我的大海.如何修复它以便我可以使用纹理和灯光?
更新2:
已将纹理更改为GL_MODulATE,但我还必须删除混合才能使其正常工作.我需要启用混合吗?
解决方法glTexEnvf(GL_TEXTURE_ENV,GL_REPLACE);
说要用纹理的结果替换照明计算.如果您希望光照影响纹理,请使用GL_MODulATE而不是GL_REPLACE.
编辑添加:
如果您希望几何体是半透明的(这通常是混合),您只需要混合.在您的情况下,代码中存在许多问题:
>你的浅色是完全透明的(所有都有0作为第4个组成部分).所以你的光让一切都看不见了.将其更改为1.>您的材料没有第4个组件.更糟糕的是,因为glMaterialfv预计有4个花车.这甚至可能导致您的应用程序崩溃或重新格式化您的磁盘.加入第四种组分,优选加入1.使其完全不透明.
总结以上是内存溢出为你收集整理的c – OpenGL照明问题全部内容,希望文章能够帮你解决c – OpenGL照明问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)