Vertex Shader编译失败,没有错误

时间:2016-04-29 18:01:00

标签: opengl glsl

我的着色器在编译时抛出错误,但它没有返回错误日志。知道为什么吗?

#version 430

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec2 vertex_texcoord;
layout(location = 2) in vec3 vertex_pos;
layout(location = 3) in vec3 vertex_rot;
layout(location = 4) in vec3 vertex_scl;
layout(location = 5) in int vertex_texnum;

varying vec2 texcoord;
varying float tex_num;

uniform float aspect_ratio;

mat4 view_frustum(
    float angle_of_view,
    float aspect_ratio,
    float z_near,
    float z_far
) {
    return mat4(
        vec4(1.0/tan(angle_of_view), 0.0, 0.0, 0.0),
        vec4(0.0, aspect_ratio/tan(angle_of_view),  0.0, 0.0),
        vec4(0.0, 0.0, (z_far+z_near)/(z_far-z_near), -2.0*z_far*z_near/(z_far-z_near)),
        vec4(0.0, 0.0, 1.0, 0.0)
    );
}

mat4 scale(float x, float y, float z)
{
    return mat4(
        vec4(x,   0.0, 0.0, 0.0),
        vec4(0.0, y,   0.0, 0.0),
        vec4(0.0, 0.0, z,   0.0),
        vec4(0.0, 0.0, 0.0, 1.0)
    );
}

mat4 translate(float x, float y, float z)
{
    return mat4(
        vec4(1.0, 0.0, 0.0, 0.0),
        vec4(0.0, 1.0, 0.0, 0.0),
        vec4(0.0, 0.0, 1.0, 0.0),
        vec4(x,   y,   z,   1.0)
    );
}

mat4 rotate_x(float theta)
{
    return mat4(
        vec4(1.0,         0.0,         0.0, 0.0),
        vec4(0.0,  cos(theta),  sin(theta), 0.0),
        vec4(0.0, -sin(theta),  cos(theta), 0.0),
        vec4(0.0,         0.0,         0.0, 1.0)
    );
}

mat4 rotate_y(float theta)
{
    return mat4(
        vec4( cos(theta), 0.0, sin(theta), 0.0),
        vec4(        0.0, 1.0,        0.0, 0.0),
        vec4(-sin(theta), 0.0, cos(theta), 0.0),
        vec4(        0.0, 0.0,        0.0, 1.0)
    );
}

mat4 rotate_z(float theta)
{
    return mat4(
        vec4(cos(theta), -sin(theta),  0.0,  0.0),
        vec4(sin(theta),  cos(theta),  0.0,  0.0),
        vec4(       0.0,         0.0,  1.0,  0.0),
        vec4(       0.0,         0.0,  0.0,  1.0)
    );
}

mat4 frustum = view_frustum(radians(45.0), 1, 0.5, 50.0);
mat4 translation = translate(vertex_pos.x, vertex_pos.y, vertex_pos.z);
mat4 rotation = rotate_x(vertex_rot.x) * rotate_y(vertex_rot.y) * rotate_z(vertex_rot.z);
mat4 scale = scale(vertex_scl.x, vertex_scl.y, vertex_scl.z);

void main () {
    gl_Position = frustum * (rotation * (scale * (translation * vec4(vertex_position, 1.0))));
    texcoord = vertex_texcoord;
    tex_num = vertex_texnum;
}
编辑:我为无知的问题道歉,我被剥夺了睡眠。 我的着色器加载代码在这里:

void initShaders(GLuint& shader_program) {
    std::ifstream file;
    std::string line;
    file.open("data/shaders/VertexShader.glsl", std::ios::in);
    if(!file.is_open()) Error("Failed to open Vertex Shader file.");

    std::string vs_str = "";

    int i = 0;
    while(getline(file,line)) {
        vs_str += line + "\n";
        ++i;
    }

    std::string fs_str = "";

    file.close();
    file.open("data/shaders/FragmentShader.glsl", std::ios::in);
    if(!file.is_open()) Error("Failed to open Fragment Shader file.");

    i = 0;
    while(getline(file,line)) {
        fs_str += line + "\n";
        ++i;
    }

    const char* vertex_shader = &vs_str[0];
    const char* fragment_shader = &fs_str[0];

    file.close();

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, &vertex_shader, NULL);
    glCompileShader(vs);

    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, &fragment_shader, NULL);
    glCompileShader(fs);

    GLint isCompiled = 0;
    glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
    if(isCompiled == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(vs, maxLength, &maxLength, &errorLog[0]);

        std::cout << "VERTEX SHADER ERROR:\n";
        for (std::vector<GLchar>::iterator i = errorLog.begin(); i != errorLog.end(); ++i)
        {
            std::cout << *i;
        }
        std::cout << "\n";
        glDeleteShader(vs); // Don't leak the shader.
        quit(EXIT_FAILURE);
    }

    isCompiled = 0;
    glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
    if(isCompiled == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(fs, maxLength, &maxLength, &errorLog[0]);

        std::cout << "FRAGMENT SHADER ERROR:\n";
        for (std::vector<GLchar>::iterator i = errorLog.begin(); i != errorLog.end(); ++i)
        {
            std::cout << *i;
        }
        std::cout << "\n";
        glDeleteShader(fs); // Don't leak the shader.
        quit(EXIT_FAILURE);
    }

    shader_program = glCreateProgram();
    glAttachShader(shader_program, fs);
    glAttachShader(shader_program, vs);
    glLinkProgram(shader_program);

    glUseProgram(shader_program);
}

1 个答案:

答案 0 :(得分:1)

对我来说,着色器在编译日志中有很多文本(在OpenGL 4.3 Core上测试):

0(10) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(11) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(82) : error C1002: the name "scale" is already defined at 0(29)
0(29) : error C1054: initialization of non-variable "scale"
0(85) : error C1020: invalid operands to "*"

前两个错误是因为430中不再使用关键字变化。您必须在顶点着色器中将其替换为out,并在片段着色器中将其替换为in

第三个错误是因为您命名的变量与函数同名(两者都命名为scale),而4和5错误是由于3导致的后续错误。

由于您的着色器加载器似乎效果不佳,我建议您提出一个包含着色器加载程序代码的新问题。