c++ - 将光照应用于 gl_Quad 中的特定复选框

标签 c++ opengl

我想在这个棋盘中的白色和黑色盒子上应用不同的光照。到目前为止我已经完成了。

myDisplay 函数中的嵌套循环正在创建框。

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color(0.2, 0.2, 0.2)
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
void myInit(void)
{
glClearColor (1,1,0,0);
glPointSize(10);
glLineWidth(10.0);
glColor3f(1,1,1);
gluOrtho2D(0,640,0,480); 
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(45,2,-2,2);
gluLookAt(100.0,200.0,200.0,//eyeposition
    0.0,0.0,0.0,//ccenter
    0.0,1.0,0.0);//up direction 
}
void myDisplay(void)
{
int posx=10;
int posy=10;
int posWidth=60;
int posHeight=60;
bool whiteORBlack=true;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);


 // glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
 // glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
for(int j=1;j<=8;j++)
{
    for(int i=1;i<=8;i++)
    {
        if(whiteORBlack==true)
        {
            glBegin(GL_QUADS);
            glColor3f(0,0,0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

            glVertex2i(posx,posy);
            glVertex2i(posx+posWidth,posy);
            glVertex2i(posx+posWidth,posy+posHeight);
            glVertex2i(posx,posy+posHeight);
            whiteORBlack=false;

        }
        else if(whiteORBlack==false)
        {
            glBegin(GL_QUADS);
            glColor3f(1,1,1);

            glVertex2i(posx,posy);
            glVertex2i(posx+posWidth,posy);
            glVertex2i(posx+posWidth,posy+posHeight);
            glVertex2i(posx,posy+posHeight);
            whiteORBlack=true;
        }
            posx=posx+posWidth;
    }

    if(whiteORBlack==false)
    {
        whiteORBlack=true;
    }
    else
    {
        whiteORBlack=false;
    }
    posy=posy+posHeight;

    posx=10;
}


//glPopMatrix();
//glutSwapBuffers();
glEnd();
glFlush();
 }
  void main(int argc,char ** argv)
 {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize (640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("FINAL LAB");
myInit();
glutDisplayFunc(myDisplay);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
//glLightfv(GL_LIGHT0,GL_DIFFUSE,ligth0_diffuse);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
//glEnable(GL_BLEND);
//glEnable(GL_LINE_SMOOTH);
//glLineWidth(2);
//glMatrixMode(GL_PROJECTION);
/*gluPerspective(40,1,1,10);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);*/
glutMainLoop();
 }

最佳答案

我假设您必须将所有 glLightfv 设置为 lightColor1 等。

与其每次都交换它,我可能会将光照设置为黑色,绘制所有黑色,然后将光照设置为白色,并绘制所有白色。

如果我错了,那么截图可能有助于更好地回答你。

关于c++ - 将光照应用于 gl_Quad 中的特定复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10749008/

相关文章:

python - 使用 glDraw 数组对立方体进行纹理处理时出现问题

c++ - 如何在安全(C++)中存储加密 key ?

c++ - 我应该更喜欢迭代器而不是 const_iterators 吗?

c++ - 我在哪里可以找到 cppunit 的版本?

python - Pygame + OpenGL - 如何在 glBegin() 之后绘制文本?

opengl - 即使使用LoadLibrary和GetProcAddress,ChoosePixelFormat也会崩溃

c++ - strlen 函数总是返回 0

c++ - 遍历 const boost::graph 的边权重

c++ - 使用 C++ 使透明度不显示 opengl 中窗口后面的内容

c++ - 图像在 OSG 中随着旋转而消失