Opengl统一变量不起作用?

时间:2015-04-02 18:53:49

标签: c opengl shader

确定。我正在进入opengl 2.1(没有固定功能的东西)而且我遇到着色器问题。我在我的着色器上声明了我的统一变量,在我的程序中,我获得了统一的位置,并为其赋予了一个gluniform值,但它似乎不起作用。

这些是我的着色器

片段着色器:     #version 120

varying float color;

void
main ()
{
    gl_FragColor = vec4(color, 0, 0, 1);
}

和我的顶点着色器:     #version 120

attribute vec2 position;
varying float color;
uniform float pr_color;

void
main () 
{
    color = pr_color;
    gl_Position = vec4(position, 0.0, 1.0);
}

这就是我将数据传递给着色器的方式:

void 
display () 
{
     glClear(GL_COLOR_BUFFER_BIT);

     glUseProgram(g_program);

     pos_loc = glGetAttribLocation(g_program, "position");

     col_loc = glGetUniformLocation(g_program, "pr_color");

     glUniform1f(col_loc, 1.0);

     glBindBuffer(GL_ARRAY_BUFFER, vbo);
     glEnableVertexAttribArray(pos_loc);
     glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

     glDrawArrays(GL_TRIANGLES, 0, 3);

    glutPostRedisplay();
    glutSwapBuffers();
 }

我不认为这是必需的,但无论如何,我的initgl函数

void 
init () 
{
     // Set clear color to black
     glClearColor(0.0,0.0,0.0,0.0);

     vshader = createShader(GL_VERTEX_SHADER, "vertex.glsl");
     fshader = createShader(GL_FRAGMENT_SHADER, "fragment.glsl");

     g_program = createProgram(vshader, fshader);

     // Create vbo and send it the vertex data
     glGenBuffers(1, &vbo);
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
事实是,我的着色器正在工作,如果我将颜色硬编码到片段sahder中,则绘制三角形,但如果我将颜色传递给顶点着色器,则它不起作用,三角形被绘制为黑色,这很奇怪,因为我的位置属性有效,但我的颜色均匀设置为0.正如你所看到的,我在传递统一值之前设置了useprogram()。

编辑:我将gluniform1i(col_loc,1)更改为glUniformif(col_loc,1.0)。仍然无法正常工作

编辑:我将添加我的着色器加载函数以确保问题不在着色器中

GLuint
createShader (GLenum type, char* filename)
{
    GLuint shader;

    // Load file
    FILE* file;
    file = fopen(filename, "r");
    if (file == NULL)
    {
        printf("Error reading file \n");
    }

    // Get Length
    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);

    // Get source
    char* source;
    source = malloc( (size_t) length + 1);
    if (source == NULL)
    {
        printf("Error alocating space for shader\n");
    }

    // Read file
    fread(source, 1, length, file);

    // Close file
    fclose(file);
    source[length] = '\n';

    // Create shader, attach it's source and compile it
    shader = glCreateShader(type);
    glShaderSource(shader, 1, (const GLchar*)&source, &length);
    free(source); // Free shader source, once it's attached
    glCompileShader(shader);

    // Check for errors
    GLint shader_status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_status);
    if (!shader_status) {
        fprintf(stderr, "Failed to compile %s:\n", filename);

        GLint log_length;
        char *log;

        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
        log = malloc(log_length);

        glGetShaderInfoLog(shader, log_length, NULL, log);
        fprintf(stderr, "%s", log);
        free(log);

        glDeleteShader(shader);
        return 0;
    }
    return shader;
}

当着色器出现错误时,程序实际上会输出错误日志,所以我不认为错误在这里,但无论如何,这里是。

编辑:程序链接器代码

GLuint
createProgram (GLuint vertexs, GLuint fragments)
{
    GLint program_ok;

    // Create program and attach to shaders
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexs);
    glAttachShader(program, fragments);
    glLinkProgram(program);

    glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
    if (!program_ok) {
        fprintf(stderr, "Failed to link shader program:\n");
        GLint log_length;
        char *log;

        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
        log = malloc(log_length);
        glGetProgramInfoLog(program, log_length, NULL, log);
        fprintf(stderr, "%s", log);
        free(log);

        glDeleteProgram(program);
        return 0;
    }

    return program;
}

请告诉我,如果我的着色器/程序有什么问题,我刚刚开始获得可编程管道的基础知识,现在我甚至无法渲染三角形

2 个答案:

答案 0 :(得分:1)

您正在使用

glUniform1i(col_loc, 1);

设置

的值
uniform float pr_color;

1i代表1 int。使用1f进行1次浮动。

glUniform1f(col_loc, 1.0f);

答案 1 :(得分:0)

好的,我使用了glUnifrom4fv()。我现在将vec4传递给着色器的颜色变量,它可以工作!