作为我项目的一部分,我必须在opengl窗口中显示动画3d模型
我在http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html
上找到了一个用于此目的的示例代码我成功编译了代码,但在运行时我遇到了以下错误
Status: VSmain shader compiled with errors
Compilation details for VSmain shader:
0:3(1): error: syntax error, unexpected VERSION
Status: FSmain shader compiled with errors
Compilation details for FSmain shader:
0:3(1): error: syntax error, unexpected VERSION
Status: Link failed
Linkage details:
linking with uncompiled shaderlinking with uncompiled shader
Errors in shader compilation
Error initializing the lighting technique
以及
printf("%s \n",glGetString(GL_SHADING_LANGUAGE_VERSION));
它返回null
我觉得错误在于skinning.glsl文件,如下所示
#version 120
#define VERTEX_IN attribute
const int MAX_POINT_LIGHTS = 2;
const int MAX_SPOT_LIGHTS = 2;
struct VSInput
{
vec3 Position;
vec2 TexCoord;
vec3 Normal;
ivec4 BoneIDs;
vec4 Weights;
};
interface VSOutput
{
vec2 TexCoord;
vec3 Normal;
vec3 WorldPos;
};
struct VSOutput1
{
vec2 TexCoord;
vec3 Normal;
vec3 WorldPos;
};
const int MAX_BONES = 100;
uniform mat4 gWVP;
uniform mat4 gWorld;
uniform mat4 gBones[MAX_BONES];
shader VSmain(in VSInput VSin:0, out VSOutput VSout)
{
mat4 BoneTransform = gBones[VSin.BoneIDs[0]] * VSin.Weights[0];
BoneTransform += gBones[VSin.BoneIDs[1]] * VSin.Weights[1];
BoneTransform += gBones[VSin.BoneIDs[2]] * VSin.Weights[2];
BoneTransform += gBones[VSin.BoneIDs[3]] * VSin.Weights[3];
vec4 PosL = BoneTransform * vec4(VSin.Position, 1.0);
gl_Position = gWVP * PosL;
VSout.TexCoord = VSin.TexCoord;
vec4 NormalL = BoneTransform * vec4(VSin.Normal, 0.0);
VSout.Normal = (gWorld * NormalL).xyz;
VSout.WorldPos = (gWorld * PosL).xyz;
}
const int MAX_POINT_LIGHTS = 2;
const int MAX_SPOT_LIGHTS = 2;
struct BaseLight
{
vec3 Color;
float AmbientIntensity;
float DiffuseIntensity;
};
struct DirectionalLight
{
struct BaseLight Base;
vec3 Direction;
};
struct Attenuation
{
float Constant;
float Linear;
float Exp;
};
struct PointLight
{
struct BaseLight Base;
vec3 Position;
Attenuation Atten;
};
struct SpotLight
{
struct 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(struct 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(struct 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(struct 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);
}
}
shader FSmain(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;
}
program Lighting
{
vs(120)=VSmain();
fs(120)=FSmain();
};
您可以通过上面给出的链接获取整个代码。
答案 0 :(得分:0)
适用于Nvidia显卡 问题出在OpenGL和glsl的版本上。此代码似乎仅支持版本#330或更高版本
使用Nvidia支持运行代码: optirun ./ {executable}