Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why not display correctly?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 gder   Members   -  Reputation: 122

Like
0Likes
Like

Posted 23 March 2013 - 03:53 AM

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[i], In);                              
    }                                                                                       
                                                                                            
    for (int i = 0 ; i < gNumSpotLights ; i++) {                                            
        TotalLight += CalcSpotLight(gSpotLights[i], In);                                
    }                                                                                       
                                                                                            
    FragColor = texture(gColorMap, In.TexCoord.xy) * TotalLight;     
}
 

Attached Thumbnails

  • a.PNG


Sponsor:

#2 blueshogun96   Crossbones+   -  Reputation: 875

Like
0Likes
Like

Posted 25 March 2013 - 04:07 AM

What exactly did you modify inside the code?  Or did you only rewrite the shader?

 

Shogun.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS