本文介绍了QT中的Phong着色编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在QT 5.2中使用着色器。我正在关注phong着色模型。

当我编译着色代码时,它显示了一些错误,例如

Hi,
I am using shaders in QT 5.2. I am following phong shading model.
When I compile my shading code, it is showing some errors like

ERROR: 0:70: '<' : syntax error syntax error\n\n
ERROR: 0:35: '<' : syntax error syntax error\n\n
ERROR: 0:25: '<' : syntax error syntax error\n\n
ERROR: 0:24: '<' : syntax error syntax error\n\n



但我的部分颜色和背景颜色变成深紫色。



任何人都可以帮我解决这个问题。



着色器代码



顶点着色器代码


but my part color and background color are becoming dark purple.

can anyone help me in fixing this.

shaders code

vertex shader code

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   float f1 = 1.0;
   vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex;
   vec4 eyeLightPos = gl_LightSource[0].position;
   vec3 N = -normalize(gl_NormalMatrix *gl_Normal);
   vec3 L = -normalize(eyeLightPos.xyz - eyePosition.xyz );
   vec3 E = -normalize(eyePosition.xyz);
   vec3 H = normalize(L + E);
   float kd = max(dot(L, N), 0.0);
   float ks = pow(max(dot(N,H),0.0), gl_FrontMaterial.shininess);
    if(dot(L,N)<0.0) f1=0.0;
   vec4 ambient = gl_FrontLightProduct[0].ambient;
   vec4 diffuse = 2.0*kd * gl_FrontLightProduct[0].diffuse;
   vec4 specular = 2.0*f1 * ks * gl_FrontLightProduct[0].specular;
    gl_FrontColor = 2.0*(ambient + diffuse + specular);
}





片段着色器代码





fragment shader code

void main(void)
{
    gl_FragColor =  gl_Color;
}







和InitializeGL()






And InitializeGL()

void DisplayControlBase::initializeGL()
{
    InitializeGLSettings();
    setFocus();
    setFocusPolicy(Qt::WheelFocus);
    GLchar vShaderFile[] = "myVertexShader.glsl";
    GLchar fShaderFile[] = "myFragmentShader.glsl";

    GLchar *vSource, *fSource;
    vSource = readShaderSource(vShaderFile);
    fSource = readShaderSource(fShaderFile);

    initializeOpenGLFunctions();

    GLuint myProgObj;

    myProgObj = glCreateProgram();
    GLuint vShader, fShader;
    vShader = glCreateShader(GL_VERTEX_SHADER);
    fShader = glCreateShader(GL_FRAGMENT_SHADER);
    glAttachShader(myProgObj, vShader);
    glAttachShader(myProgObj, fShader);
    glShaderSource(vShader, 1, (const GLchar**) &vSource, NULL);
    glShaderSource(fShader, 1, (const GLchar**) &fSource, NULL);

    glCompileShader(vShader);
    glCompileShader(fShader);
    GLint status;
    glGetShaderiv(fShader, GL_COMPILE_STATUS, &status);

    CheckError(status, fShader);
    glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);
    CheckError(status, vShader);
    glLinkProgram(myProgObj);
    glUseProgram(myProgObj);
    glGetProgramiv(myProgObj, GL_LINK_STATUS, &status);
    CheckError(status, myProgObj);

}







void DisplayControlBase::CheckError(GLint CompileStatus, GLuint Shader)
{
    if (!CompileStatus)
    {
        int iLength, iChars;
         glGetShaderiv(Shader, GL_INFO_LOG_LENGTH, &iLength);

        char* pInfoLog = new char[iLength];
        glGetShaderInfoLog(Shader, iLength, &iChars, pInfoLog);

        delete [] pInfoLog;
    }
}




char* DisplayControlBase::readShaderSource(const char *shaderFile)
{
    FILE *fp = fopen(shaderFile, "r");
    char* buffer;
    long size;
    if( fp == NULL) return(NULL );
    fseek(fp, 0L, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    buffer = (char*)malloc((size + 1)* sizeof(char));
    fread(buffer, 1, size, fp);
    buffer[size] = ' ';
    fclose(fp);
    return buffer;
}

推荐答案

这篇关于QT中的Phong着色编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:41