Jump to content
  • Advertisement
Sign in to follow this  
gder

Why not display correctly?

This topic is 1945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems mixed together in some vertex, see the attachement!

 

I modifiedsource code from  http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html

Because they use glsl4.1,  can't run my computer!

 

Thanks!

 

code snippet in mesh.cpp  Mesh::InitFromScene:

 

      glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[POS_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Positions[0]) * Positions.size(), &Positions[0], GL_STATIC_DRAW);
int VSInput_Position = glGetAttribLocationARB (g_shaderProg, "VSInput_Position");
glEnableVertexAttribArray(VSInput_Position);
    glVertexAttribPointer(VSInput_Position, 3, GL_FLOAT, GL_FALSE, 0, 0);    

    glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[TEXCOORD_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords[0]) * TexCoords.size(), &TexCoords[0], GL_STATIC_DRAW);
int VSInput_TexCoord = glGetAttribLocationARB (g_shaderProg, "VSInput_TexCoord");
    glEnableVertexAttribArray(VSInput_TexCoord);
    glVertexAttribPointer(VSInput_TexCoord, 2, GL_FLOAT, GL_FALSE, 0, 0);

       glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[NORMAL_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Normals[0]) * Normals.size(), &Normals[0], GL_STATIC_DRAW);
int VSInput_Normal = glGetAttribLocationARB (g_shaderProg, "VSInput_Normal");
    glEnableVertexAttribArray(VSInput_Normal);
    glVertexAttribPointer(VSInput_Normal, 3, GL_FLOAT, GL_FALSE, 0, 0);

       glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[BONE_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Bones[0]) * Bones.size(), &Bones[0], GL_STATIC_DRAW);
int VSInput_BoneIDs = glGetAttribLocationARB (g_shaderProg, "VSInput_BoneIDs");
    glEnableVertexAttribArray(VSInput_BoneIDs);
    glVertexAttribPointer(VSInput_BoneIDs, 4, GL_INT, GL_FALSE,  sizeof(VertexBoneData), (const GLvoid*)0);
int VSInput_Weights = glGetAttribLocationARB (g_shaderProg, "VSInput_Weights");
    glEnableVertexAttribArray(VSInput_Weights);    
    glVertexAttribPointer(VSInput_Weights, 4, GL_FLOAT, GL_FALSE, sizeof(VertexBoneData), (const GLvoid*)16);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[INDEX_BUFFER]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices[0]) * Indices.size(), &Indices[0], GL_STATIC_DRAW);
 

 

vertex shader:

 

#version 110
attribute  vec3  VSInput_Position;                                             
attribute  vec2  VSInput_TexCoord;                                             
attribute  vec3  VSInput_Normal;    
attribute  vec4 VSInput_BoneIDs;
attribute  vec4  VSInput_Weights;
varying    vec2 VSOutput_TexCoord;     
varying    vec3 VSOutput_Normal;
varying    vec3 VSOutput_WorldPos;     
const int MAX_BONES = 100;
uniform mat4 gWVP;
uniform mat4 gWorld;
uniform mat4 gBones[MAX_BONES];
void main()
{       
    mat4 BoneTransform = gBones[int(floor(VSInput_BoneIDs[0]))] * VSInput_Weights[0];
    BoneTransform     += gBones[int(floor(VSInput_BoneIDs[1]))] * VSInput_Weights[1];
    BoneTransform     += gBones[int(floor(VSInput_BoneIDs[2]))] * VSInput_Weights[2];
    BoneTransform     += gBones[int(floor(VSInput_BoneIDs[3]))] * VSInput_Weights[3];
    vec4 PosL      = BoneTransform * vec4(VSInput_Position, 1.0);
    gl_Position    = gWVP * PosL;
    VSOutput_TexCoord = VSInput_TexCoord;
    vec4 NormalL   = BoneTransform * vec4(VSInput_Normal, 0.0);
    VSOutput_Normal   = (gWorld * NormalL).xyz;
    VSOutput_WorldPos = (gWorld * PosL).xyz;                                
}
 

fragment shader:

 

#version 120
struct VSOutput
{                                                                                    
    vec2 TexCoord;                                                                 
    vec3 Normal;                                                                   
    vec3 WorldPos;                                                                 
};

struct VSOutput1
{                                                                                    
    vec2 TexCoord;                                                                 
    vec3 Normal;                                                                   
    vec3 WorldPos;                                                                 
};



const int MAX_POINT_LIGHTS = 2;
const int MAX_SPOT_LIGHTS = 2;

struct BaseLight
{
    vec3 Color;
    float AmbientIntensity;
    float DiffuseIntensity;
};

struct DirectionalLight
{
    BaseLight Base;
    vec3 Direction;
};
                                                                                    
struct Attenuation                                                                  
{                                                                                   
    float Constant;                                                                 
    float Linear;                                                                   
    float Exp;                                                                      
};                                                                                  
                                                                                    
struct PointLight                                                                           
{                                                                                           
    BaseLight Base;                                                                  
    vec3 Position;                                                                          
    Attenuation Atten;                                                                      
};                                                                                          
                                                                                            
struct SpotLight                                                                            
{                                                                                           
    PointLight Base;                                                                 
    vec3 Direction;                                                                         
    float Cutoff;                                                                           
};                                                                                          
                                                                                            
uniform int gNumPointLights;                                                                
uniform int gNumSpotLights;                                                                 
uniform DirectionalLight gDirectionalLight;                                                 
uniform PointLight gPointLights[MAX_POINT_LIGHTS];                                          
uniform SpotLight gSpotLights[MAX_SPOT_LIGHTS];                                             
uniform sampler2D gColorMap;                                                                
uniform vec3 gEyeWorldPos;                                                                  
uniform float gMatSpecularIntensity;                                                        
uniform float gSpecularPower;


vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, VSOutput1 In)            
{                                                                                           
    vec4 AmbientColor = vec4(Light.Color, 1.0f) * Light.AmbientIntensity;                   
    float DiffuseFactor = dot(In.Normal, -LightDirection);                                     
                                                                                            
    vec4 DiffuseColor  = vec4(0, 0, 0, 0);                                                  
    vec4 SpecularColor = vec4(0, 0, 0, 0);                                                  
                                                                                            
    if (DiffuseFactor > 0) {                                                                
        DiffuseColor = vec4(Light.Color, 1.0f) * Light.DiffuseIntensity * DiffuseFactor;    
                                                                                            
        vec3 VertexToEye = normalize(gEyeWorldPos - In.WorldPos);                             
        vec3 LightReflect = normalize(reflect(LightDirection, In.Normal));                     
        float SpecularFactor = dot(VertexToEye, LightReflect);                              
        SpecularFactor = pow(SpecularFactor, gSpecularPower);                               
        if (SpecularFactor > 0) {                                                           
            SpecularColor = vec4(Light.Color, 1.0f) *                                       
                            gMatSpecularIntensity * SpecularFactor;                         
        }                                                                                   
    }                                                                                       
                                                                                            
    return (AmbientColor + DiffuseColor + SpecularColor);                                   
}                                                                                           
                                                                                            
vec4 CalcDirectionalLight(VSOutput1 In)                                                      
{                                                                                           
    return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, In);  
}                                                                                           
                                                                                            
vec4 CalcPointLight(PointLight l, VSOutput1 In)                                       
{                                                                                           
    vec3 LightDirection = In.WorldPos - l.Position;                                           
    float Distance = length(LightDirection);                                                
    LightDirection = normalize(LightDirection);                                             
                                                                                            
    vec4 Color = CalcLightInternal(l.Base, LightDirection, In);                         
    float Attenuation =  l.Atten.Constant +                                                 
                         l.Atten.Linear * Distance +                                        
                         l.Atten.Exp * Distance * Distance;                                 
                                                                                            
    return Color / Attenuation;                                                             
}                                                                                           
                                                                                            
vec4 CalcSpotLight(SpotLight l, VSOutput1 In)                                         
{                                                                                           
    vec3 LightToPixel = normalize(In.WorldPos - l.Base.Position);                             
    float SpotFactor = dot(LightToPixel, l.Direction);                                      
                                                                                            
    if (SpotFactor > l.Cutoff) {                                                            
        vec4 Color = CalcPointLight(l.Base, In);                                        
        return Color * (1.0 - (1.0 - SpotFactor) * 1.0/(1.0 - l.Cutoff));                   
    }                                                                                       
    else {                                                                                  
        return vec4(0,0,0,0);                                                               
    }                                                                                       
}                                                                                           
                                                                                            
void main(in VSOutput FSin, out vec4 FragColor)
{                                    
    VSOutput1 In;
    In.TexCoord = FSin.TexCoord;
    In.Normal = normalize(FSin.Normal);
    In.WorldPos = FSin.WorldPos;                                                                 
 
    vec4 TotalLight = CalcDirectionalLight(In);                                         
                                                                                            
    for (int i = 0 ; i < gNumPointLights ; i++) {                                           
        TotalLight += CalcPointLight(gPointLights, In);                              
    }                                                                                       
                                                                                            
    for (int i = 0 ; i < gNumSpotLights ; i++) {                                            
        TotalLight += CalcSpotLight(gSpotLights, In);                                
    }                                                                                       
                                                                                            
    FragColor = texture(gColorMap, In.TexCoord.xy) * TotalLight;     
}
 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!