1

I have a model made up of five cubes, the whole model is textured, I added lighting and it sort of works for the top, bottom,font and back of the cubes but not the sides, there is also an issue with the shading because there is light where it should be shaded. I am using a Matlab .NET opengl wrapper based on Tao framework for this which is a unusual I admit.

I can provide the whole code if asked but for now,here is my lighting code

%% Lighting 

% Enable Light
Gl.glEnable(Gl.GL_LIGHTING);
% Vector for light position (directional light - try a positional one)

% Get Projection matrix in array of length 16
projmatrix = NET.createArray('System.Single', 16);
Gl.glGetFloatv(Gl.GL_PROJECTION_MATRIX,projmatrix);
% Reshape to 4x4 matrix
projmatrix=reshape(double(projmatrix),[4 4]);
% Use the inverse projection matrix to get the light fixed with the camera
LightPosCam=(projmatrix)\[0.3 0.5 -0.6 0]';

LightPos= NET.convertArray(LightPosCam, 'System.Single', 4);
Ambient =  NET.convertArray([0.000001 0.000001 0.000001 .1], 
'System.Single', 4); 
Diffuse=  NET.convertArray([0.7 0.7 0.7 1], 'System.Single', 4); 
Specular=  NET.convertArray([0.6 0.6 0.6 1], 'System.Single', 4); 

% Turn on Lighting
Gl.glEnable(Gl.GL_LIGHTING);
Gl.glEnable(Gl.GL_LIGHT0);

Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_POSITION,LightPos);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_AMBIENT,Ambient);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_DIFFUSE,Diffuse);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_SPECULAR,Specular);

% Constant attenuation (for distance, etc.)
% Only works for fixed light locations!  Otherwise disabled
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_CONSTANT_ATTENUATION, 1.0);
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_LINEAR_ATTENUATION, 0.0);
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_QUADRATIC_ATTENUATION, 0.0);
Gl.glLightModeli(Gl.GL_LIGHT_MODEL_LOCAL_VIEWER, Gl.GL_TRUE);

% Normalize vectors
Gl.glEnable(Gl.GL_NORMALIZE);

% Enable ColorMaterial
% Gl.glEnable (Gl.GL_COLOR_MATERIAL ) ;
% Set the Material Properties
floor_ambient = NET.convertArray([0, 0, 0, 0 ], 'System.Single', 4); 
floor_diffuse = NET.convertArray([0.8, 0.8, 0.8, 1.0 ], 'System.Single', 4); 

floor_specular= NET.convertArray([0.7,0.7,0.7, 1 ], 'System.Single', 4);
floor_emission= NET.convertArray([0.2,0.0,0.0, 1 ], 'System.Single', 4);
floor_shininess = 120;
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, floor_ambient);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, floor_diffuse);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, floor_specular);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SHININESS, floor_shininess);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_EMISSION, floor_emission);

% Gourang shanding
Gl.glShadeModel(Gl.GL_SMOOTH);

% Specular color enable

Gl.glLightModeli(Gl.GL_LIGHT_MODEL_COLOR_CONTROL,
Gl.GL_SEPARATE_SPECULAR_COLOR);
%  Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, 
Gl.GL_MODULATE);
% Gl.glEnable(Gl.GL_BLEND); 

And here is my code for only one of the cubes

     %% Draw Sats
        % Select our texture

        Gl.glEnable(Gl.GL_NORMALIZE)

        %     Bottom Face  
        Gl.glEnable(Gl.GL_TEXTURE_2D);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TextureID8);  %%Metalic Texture
        Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3f(0,0,-1);
            Gl.glTexCoord2f(0,0);
            Gl.glVertex3f(cornersx(1), cornersy(1), cornersz(1));  
            Gl.glNormal3f(0,0,-1);
            Gl.glTexCoord2f(1,0);
            Gl.glVertex3f(cornersx(2), cornersy(2), cornersz(2));   
            Gl.glNormal3f(0,0,-1);
            Gl.glTexCoord2f(1,1);
            Gl.glVertex3f(cornersx(4), cornersy(4), cornersz(4));   
            Gl.glNormal3f(0,0,-1);
            Gl.glTexCoord2f(0,1);
            Gl.glVertex3f(cornersx(3), cornersy(3), cornersz(3));   

        %     Top Face
           Gl.glEnd()

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TextureID3);  %%Glyph1 Texture
        Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3f(0,0,1);
            Gl.glTexCoord2f(0,0);
            Gl.glVertex3f(cornersx(7), cornersy(7), cornersz(7));               

        Gl.glNormal3f(0,0,1);
        Gl.glTexCoord2f(1,0);
        Gl.glVertex3f(cornersx(8), cornersy(8), cornersz(8));               

        Gl.glNormal3f(0,0,1);
         Gl.glTexCoord2f(1,1);
         Gl.glVertex3f(cornersx(6), cornersy(6), cornersz(6));              

        Gl.glNormal3f(0,0,1);
        Gl.glTexCoord2f(0,1);
        Gl.glVertex3f(cornersx(5), cornersy(5), cornersz(5));
        Gl.glEnd()

        % Front Face
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TextureID);  
        Gl.glEnable(Gl.GL_TEXTURE_2D);  
        Gl.glBegin(Gl.GL_QUADS);
        Gl.glNormal3f(0,-1,0);
        Gl.glTexCoord2f(0,0);
        Gl.glVertex3f(cornersx(1), cornersy(1), cornersz(1));               

         Gl.glNormal3f(0,-1,0);
         Gl.glTexCoord2f(1,0);
         Gl.glVertex3f(cornersx(5), cornersy(5), cornersz(5));              

        Gl.glNormal3f(0,-1,0);
        Gl.glTexCoord2f(1,1);
        Gl.glVertex3f(cornersx(6), cornersy(6), cornersz(6));               

        Gl.glNormal3f(0,-1,0);
        Gl.glTexCoord2f(0,1);   
        Gl.glVertex3f(cornersx(2), cornersy(2), cornersz(2));

        %     Back Face

        Gl.glNormal3f(0,1,0);
        Gl.glTexCoord2f(0,0);
        Gl.glVertex3f(cornersx(3), cornersy(3), cornersz(3));               

        Gl.glNormal3f(0,1,0);
        Gl.glTexCoord2f(1,0);
        Gl.glVertex3f(cornersx(4), cornersy(4), cornersz(4));               

        Gl.glNormal3f(0,1,0);
        Gl.glTexCoord2f(1,1);
        Gl.glVertex3f(cornersx(8), cornersy(8), cornersz(8));               

        Gl.glNormal3f(0,1,0);
        Gl.glTexCoord2f(0,1);
        Gl.glVertex3f(cornersx(7), cornersy(7), cornersz(7));

        %     Right Face

         Gl.glNormal3f(-1,0,0);
         Gl.glTexCoord2f(0,0);
         Gl.glVertex3f(cornersx(4), cornersy(4), cornersz(4));              

         Gl.glNormal3f(-1,0,0);
         Gl.glTexCoord2f(1,0);
         Gl.glVertex3f(cornersx(2), cornersy(2), cornersz(2));              

         Gl.glNormal3f(-1,0,0);
         Gl.glTexCoord2f(1,1);
         Gl.glVertex3f(cornersx(6), cornersy(6), cornersz(6));              

        Gl.glNormal3f(-1,0,0);
        Gl.glTexCoord2f(0,1);
        Gl.glVertex3f(cornersx(8), cornersy(8), cornersz(8));

        %     Left Face

         Gl.glNormal3f(1,0,0);
         Gl.glTexCoord2f(0,0);
         Gl.glVertex3f(cornersx(3), cornersy(3), cornersz(3));              
         Gl.glNormal3f(1,0,0);
         Gl.glTexCoord2f(1,0);
         Gl.glVertex3f(cornersx(1), cornersy(1), cornersz(1));                  
         Gl.glNormal3f(1,0,0);
         Gl.glTexCoord2f(1,1);
         Gl.glVertex3f(cornersx(5), cornersy(5), cornersz(5));                  
         Gl.glNormal3f(1,0,0);
         Gl.glTexCoord2f(0,1);
         Gl.glVertex3f(cornersx(7), cornersy(7), cornersz(7));
        Gl.glEnd()

Finally a gif of my model and the issue in action.

4

1 回答 1

0

我可以看到两个潜在的问题:

  1. 如果您只是在左右面方面遇到问题,则可能只是您不小心翻转了它们的表面法线。我会试试这个:

    Gl.glNormal3f(1,0,0);   % For all of the right face vertices
    Gl.glNormal3f(-1,0,0);  % For all of the left face vertices
    

    当表面法线指向错误的方向(即进入立方体)时,这意味着多边形的背面朝外。当您调用 时Gl.glMaterialfv,您指定Gl.GL_FRONTand not Gl.GL_FRONT_AND_BACK,这意味着背面没有被照亮。我猜这就是为什么你看到侧面没有照明的原因。

  2. 我不知道您的顶点的顺序是否重要,因此当从立方体的外部查看时,它们应该始终以顺时针或逆时针顺序围绕面部指定。由于我不知道您的cornersxcornersycornersz数据是什么样的,我无法判断您是将它们定义为顺时针还是逆时针。但是,我可以告诉您,您的其中一张脸的定义顺时针顺序与其他脸不同:左脸,顶点顺序为3 -> 1 -> 5 -> 7。我对您的照明框架了解得不够多,无法说明这是否会成为问题,但为了保持一致性,您可能希望按顺序定义左面的顶点1 -> 3 -> 7 -> 5

于 2017-06-21T14:04:24.820 回答