设置基本着色器程序 - GLchar和file_contents未定义?

时间:2011-11-22 11:39:19

标签: c++ opengl shader

我正在尝试在我的程序中使用基本着色器进行实验,我遇到了a nice tutorial,通过编写一个基本着色器“util class”来告诉你,我想你会称之为?这应该允许我应用顶点和片段着色器......所以我将glew链接到我的项目(我还包括glu,glut和glaux)并将以下内容插入到头文件中

#include "include\gl\glew.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

static struct {
    /* ... fields for buffer and texture objects */
    GLuint vertex_shader, fragment_shader, program;

    struct {
        GLint fade_factor;
        GLint textures[2];
    } uniforms;

    struct {
        GLint position;
    } attributes;

    GLfloat fade_factor;
} g_resources;

static GLuint make_shader(GLenum type, const char *filename)
{
    GLint length;
    char *source = file_content(filename, &length);
    GLuint shader;
    GLint shader_ok;

    if (!source)
        return 0;
    shader = glCreateShader(type);
    glShaderSource(shader, 1, (const GLchar**)&source, &length);
    free(source);
    glCompileShader(shader);
    glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
    if (!shader_ok) {
        fprintf(stderr, "Failed to compile %s:\n", filename);
        show_info_log(shader, glGetShaderiv, glGetShaderInfoLog);
        glDeleteShader(shader);
        return 0;
    }
    return shader;
}

static void show_info_log(
    GLuint object,
    PFNGLGETSHADERIVPROC glGet__iv,
    PFNGLGETSHADERINFOLOGPROC glGet__InfoLog)
{
    GLint log_length;
    char *log;

    glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length);
    log = malloc(log_length);
    glGet__InfoLog(object, log_length, NULL, log);
    fprintf(stderr, "%s", log);
    free(log);
}

static GLuint make_program(GLuint vertex_shader, GLuint fragment_shader)
{
    GLint program_ok;

    GLuint program = glCreateProgram();
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);
     glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
    if (!program_ok) {
        fprintf(stderr, "Failed to link shader program:\n");
        show_info_log(program, glGetProgramiv, glGetProgramInfoLog);
        glDeleteProgram(program);
        return 0;
    }
    return program;
}

static int make_resources(void)
{
    /* make buffers and textures ... */
    g_resources.vertex_shader = make_shader(
        GL_VERTEX_SHADER,
        "hello-gl.v.glsl"
    );
    if (g_resources.vertex_shader == 0)
        return 0;

    g_resources.fragment_shader = make_shader(
        GL_FRAGMENT_SHADER,
        "hello-gl.f.glsl"
    );
    if (g_resources.fragment_shader == 0)
        return 0;

    g_resources.program = make_program(
        g_resources.vertex_shader,
        g_resources.fragment_shader
    );
    if (g_resources.program == 0)
        return 0;
    g_resources.uniforms.fade_factor
        = glGetUniformLocation(g_resources.program, "fade_factor");
    g_resources.uniforms.textures[0]
        = glGetUniformLocation(g_resources.program, "textures[0]");
    g_resources.uniforms.textures[1]
        = glGetUniformLocation(g_resources.program, "textures[1]");

    g_resources.attributes.position
        = glGetAttribLocation(g_resources.program, "position");

    return 1;
}

但我的编译器抱怨以下内容:

9   IntelliSense: "GLchar" is not a type name   

    11  IntelliSense: a value of type "void *" cannot be assigned to an entity of type "char *" 

    7   IntelliSense: identifier "file_contents" is undefined   

    5   IntelliSense: identifier "GLchar" is undefined

我错过了什么吗?我搜索了互联网,似乎GLchar和file_contents功能确实存在?

1 个答案:

答案 0 :(得分:3)

您提供的错误行是编译器告诉您,它不知道在您的情况下GL/gl.h - 或GL/glew.h中定义的GLchar类型,它也定义了它。但它似乎没有被恰当地包括在内。

你的第一行应该是

#include <GL/glew.h>

即。 uppercae GL,一个正斜杠(反斜杠是一个Microsoaft添加,但前向接受很好),整个东西在尖括号中,因为你想从标准包括。

接下来,您不应该与glaux相关联。那个过时了,它变得有毒。

如果你想要一个有效的OpenGL着色器示例程序,我在https://github.com/datenwolf/codesamples/tree/master/samples/OpenGL/minimal_glsl

准备了一个