gpt4 book ai didi

opengl - GLSL - 奇怪的语法错误 "<"

转载 作者:行者123 更新时间:2023-12-02 09:08:09 29 4
gpt4 key购买 nike

我正在尝试使用着色器,但它在片段和顶点着色器上不断告诉我此错误:

error(#132) Syntax error: "<" parse error

顶点着色器

varying vec4 diffuse;
varying vec4 ambient;
varying vec3 normal;
varying vec3 halfVector;
 
void main()
{
    normal = normalize(gl_NormalMatrix * gl_Normal);
 
    halfVector = gl_LightSource[0].halfVector.xyz;
 
    diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
    ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
    ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;

    gl_Position = ftransform();
 
}

片段着色器

varying vec4 diffuse,ambient;
varying vec3 normal,halfVector;
 
void main()
{
    vec3 n,halfV,lightDir;
    float NdotL,NdotHV;
 
    lightDir = vec3(gl_LightSource[0].position);
 
    vec4 color = ambient;
 
    n = normalize(normal);
 
    NdotL = max(dot(n,lightDir),0.0);
    if (NdotL > 0.0) {
        color += diffuse * NdotL;
        halfV = normalize(halfVector);
        NdotHV = max(dot(n,halfV),0.0);
        color += gl_FrontMaterial.specular *
                gl_LightSource[0].specular *
                pow(NdotHV, gl_FrontMaterial.shininess);
    }
 
    gl_FragColor = color;
 
}

读取着色器的代码:

bool Shader::load(string vertex , string fragment)
{
// These will hold the shader's text file data
string vshader, fshader;

// Make sure the user passed in a vertex and fragment shader file
if(!vertex.length() || !fragment.length()) return false;

// If any of our shader pointers are set, let's free them first.
if(VertexShader || FragmentShader || ProgramObject) Release();

// Here we get a pointer to our vertex and fragment shaders
VertexShader = glCreateShader(GL_VERTEX_SHADER);
FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

// Now we load the shaders from the respective files and store it in a string.
vshader = LoadTextFile(vertex.c_str());
fshader = LoadTextFile(fragment.c_str());

if(vshader == "" || fshader == "") return false;

// Do a quick switch so we can do a double pointer below
const char *vvshader = vshader.c_str();
const char *ffshader = fshader.c_str();

// Now this assigns the shader text file to each shader pointer
glShaderSource(VertexShader, 1, &vvshader, NULL);
glShaderSource(FragmentShader, 1, &ffshader, NULL);

// Now we actually compile the shader's code
glCompileShader(VertexShader);
glCompileShader(FragmentShader);

printInfoLog(VertexShader);
printInfoLog(FragmentShader);

// Next we create a program object to represent our shaders
ProgramObject = glCreateProgram();

// We attach each shader we just loaded to our program object
glAttachShader(ProgramObject, VertexShader);
glAttachShader(ProgramObject, FragmentShader);

// Our last init function is to link our program object with OpenGL
glLinkProgram(ProgramObject);

printInfoLog(ProgramObject);

// Now, let's turn off our current shader.
glUseProgram(0);

return true;

}


string Shader::LoadTextFile(string file)
{
// Open the file passed in
ifstream fin(file.c_str());

// Make sure we opened the file correctly
if(!fin) return "";

string line = "";
string text = "";

// Go through and store each line in the text file within a "string" object
while(getline(fin, line))
{
text = text + "\n" + line;
}

// Close our file
fin.close();

// Return the text file's data
return text;
}

但是,如果我尝试另一个着色器就可以正常工作!我真的不知道为什么。

最佳答案

您的错误和缺少 <着色器源中的标记表明 glShaderSource 正在读取字符串末尾的尾随垃圾。

这听起来像是经验丰富的开发人员都知道的一个微妙问题,但可能会困扰新手。 glShaderSource 要么期望以 0 结尾的字符串和长度数组的 NULL 指针,要么传递一个具有长度的数组,以便字符串不需要以 0 结尾。从技术上讲, std::string::c_str 应该可以访问以零结尾的字符串,但在您的情况下似乎没有。

无论如何,简单的解决方案是提供一个长度数组,以便 glShaderSource 不会读入尾随垃圾:

// Do a quick switch so we can do a double pointer below
const char *vvshader = vshader.c_str();
const char *ffshader = fshader.c_str();
const GLint lenvshader = vshader.length();
const GLint lenfshader = fshader.length();

// Now this assigns the shader text file to each shader pointer
glShaderSource(VertexShader, 1, &vvshader, &lenvshader);
glShaderSource(FragmentShader, 1, &ffshader, &lenfshader);

关于opengl - GLSL - 奇怪的语法错误 "<",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624318/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com