代码之家  ›  专栏  ›  技术社区  ›  Pirun Seng

如何启用两个对象的不同glu位置

  •  0
  • Pirun Seng  · 技术社区  · 6 年前

    enter image description here

    我把两个灯一个接一个地打开了。但是第二个总是和第一个/上面的一样。

        // light from top right
        GLfloat light_position_top_right[] = { 1.0, 1.0, 1.0, 0.0 };
        // light from bottom left
        GLfloat light_position_bottom_left[] = { -1.0, -1.0, 1.0, 0.0 };
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glLightfv(GL_LIGHT0, GL_POSITION, light_position_top_right);
        glLightfv(GL_LIGHT1, GL_POSITION, light_position_bottom_left);
    
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glEnable(GL_DEPTH_TEST);
    

    你可以在这里找到完整的代码 gist .

    我希望第二个物体在左下角有光线位置。但它从右上角显示为第一个对象。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Magma    6 年前

    你灯光位置的第四个坐标必须是 1.0 而不是 0.0

    第四个坐标是 0.0分

    Lights are infinitely far away

    第四个坐标是 1.0条 :

    Lights are properly located

    至于为什么只有两个亮点而不是一个,你忘了设置合适的 glLight* GL_LIGHT0 以白色漫反射和镜面反射颜色开始,但默认情况下,所有其他灯光都具有黑色漫反射和镜面反射颜色。


    编辑:

    如果您希望单独的球体使用单独的灯光,则需要在 init() . 但不是 glEnable

    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glPushMatrix();
        glTranslatef(1.0, 0.0, -1.0);
        glEnable(GL_LIGHT0);
        drawGraySphere();
        glutSolidSphere(0.5, 50, 50);
        glDisable(GL_LIGHT0);
        glPopMatrix();
    
        glPushMatrix();
        glTranslatef(-0.75, -0.8, 0.0);
        glEnable(GL_LIGHT1);
        drawGoldSphere();
        glutSolidSphere(0.5, 50, 50);
        glDisable(GL_LIGHT1);
        glPopMatrix();
    
        glFlush();
    }
    

    或者,您可以选择仅使用单个灯光,并使用 glLight公司* 每个帧,在渲染每个球体之前。如果你计划拥有一堆对象,每个对象都有自己的灯光,这是一个更好的选择。

        2
  •  0
  •   Magma    6 年前

    #include<windows.h>
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <GL/gl.h>
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    void drawGoldSphere() {
        GLfloat mat_specular[] = { 0.628281, 0.555802, 0.366065, 1.0 };
        GLfloat mat_shininess[] = { 51.2 };
        GLfloat mat_ambient[] = { 0.24725, 0.1995, 0.0745, 1.0 };
        GLfloat mat_diffuse[] = { 0.75164, 0.60648, 0.22648, 1.0 };
        //Material Properties
        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);
    }
    
    void drawGraySphere() {
        GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat mat_shininess[] = { 50.0 };
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    }
    
    void init(void)
    {
        // light from middle left
        //GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
        // light from top right
        GLfloat light_position_top_right[] = { 1.0, 1.0, 1.0, 0.0 };
        // light from bottom left
        GLfloat light_position_bottom_left[] = { -1.0, -1.0, 1.0, 0.0 };
        // light from bottom left
        //GLfloat light_position[] = { -0.1, -0.1, -0.1, 0.0 };
        // light from middle top
        //GLfloat light_position[] = { 0.0, 1.0, 0.0, 0.0 };
        // light from middle right
        //GLfloat light_position[] = { 1.0, 0.0, 0.0, 0.0 };
        // light from middle middle
        //GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
        // light from middle bottom
        //GLfloat light_position[] = { 0.0, -1.0, 1.0, 0.0 };
    
    
        GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
        GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
        GLfloat high_shininess[] = { 100.0 };
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position_top_right);
    
        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
        glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT1, GL_POSITION, light_position_bottom_left);
    
        glEnable(GL_LIGHTING);
        glEnable(GL_DEPTH_TEST);
    }
    
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glPushMatrix();
        glTranslatef(1.0, 0.0, -1.0);
        glEnable(GL_LIGHT0);
        drawGraySphere();
        glutSolidSphere(0.5, 50, 50);
        glDisable(GL_LIGHT0);
        glPopMatrix();
    
        glPushMatrix();
        glTranslatef(-0.75, -0.8, 0.0);
        glEnable(GL_LIGHT1);
        drawGoldSphere();
        glutSolidSphere(0.5, 50, 50);
        glDisable(GL_LIGHT1);
        glPopMatrix();
    
        glFlush();
    }
    void reshape(int w, int h)
    {
        glViewport(0, 0, (GLsizei) w, (GLsizei) h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        if (w <= h)
        glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
        1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
        else
        glOrtho(-1.5*(GLfloat)w/(GLfloat)h,
        1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(500, 500);
        glutInitWindowPosition(100, 100);
        glutCreateWindow(argv[0]);
        init();
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutMainLoop();
        return 0;
    }
    

    渲染结果:

    enter image description here

    推荐文章