设置基本着色器程序 - GLchar 和 file_contents 未定义? [英] Setting up basic shader program - GLchar and file_contents undefined?

查看:115
本文介绍了设置基本着色器程序 - GLchar 和 file_contents 未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的程序中使用基本着色器进行实验,我遇到了 一个很好的教程,通过编写一个基本的着色器工具类"来告诉你,我猜你会叫它?这应该允许我应用顶点和片段着色器......所以我将glew链接到我的项目(我还包括glu,glut和glaux)并将以下内容插入到头文件中

#include "include\gl\glew.h"#include <math.h>#include #include 静态结构{/* ... 缓冲区和纹理对象的字段 */GLuint vertex_shader、fragment_shader、程序;结构{GLint 淡入淡出系数;闪光纹理[2];} 制服;结构{闪烁位置;} 属性;GLfloatfade_factor;g_resources;静态 GLuint make_shader(GLenum 类型,const char *filename){GLint 长度;char *source = file_content(filename, &length);GLuint 着色器;GLint shader_ok;如果(!来源)返回0;着色器 = glCreateShader(类型);glShaderSource(shader, 1, (const GLchar**)&source, &length);免费(来源);glCompileShader(着色器);glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);如果(!shader_ok){fprintf(stderr, "无法编译 %s:\n", 文件名);show_info_log(着色器,glGetShaderiv,glGetShaderInfoLog);glDeleteShader(着色器);返回0;}返回着色器;}静态无效 show_info_log(GLuint 对象,PFNGLGETSHADERIVPROC glGet__iv,PFNGLGETSHADERINFOLOGPROC glGet__InfoLog){GLint log_length;字符*日志;glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length);log = malloc(log_length);glGet__InfoLog(object, log_length, NULL, log);fprintf(stderr, "%s", log);免费(日志);}静态 GLuint make_program(GLuint vertex_shader, GLuint fragment_shader){GLint program_ok;GLuint 程序 = glCreateProgram();glAttachShader(程序,vertex_shader);glAttachShader(程序,片段着色器);glLinkProgram(程序);glGetProgramiv(program, GL_LINK_STATUS, &program_ok);如果(!program_ok){fprintf(stderr, "无法链接着色器程序:\n");show_info_log(程序,glGetProgramiv,glGetProgramInfoLog);glDeleteProgram(程序);返回0;}返回程序;}静态 int make_resources(void){/* 制作缓冲区和纹理... */g_resources.vertex_shader = make_shader(GL_VERTEX_SHADER,你好-gl.v.glsl");如果(g_resources.vertex_shader == 0)返回0;g_resources.fragment_shader = make_shader(GL_FRAGMENT_SHADER,你好-gl.f.glsl");如果(g_resources.fragment_shader == 0)返回0;g_resources.program = make_program(g_resources.vertex_shader,g_resources.fragment_shader);如果(g_resources.program == 0)返回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");返回 1;}

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

9 IntelliSense:GLchar"不是类型名称11 IntelliSense:不能将void *"类型的值分配给char *"类型的实体7 智能感知:标识符file_contents"未定义5 智能感知:标识符GLchar"未定义

我错过了什么吗?我在网上搜索了一下,好像 GLchar 和 file_contents 函数确实存在?

解决方案

您给出的错误行是编译器告诉您,它不知道 GL/gl 中定义的 GLchar 类型.h – 或者 GL/glew.h 在你的情况下,它也定义了它.但它似乎没有正确包含.<​​/p>

你的第一行应该是

#include 

即uppercae GL、一个正斜杠(反斜杠是 Microsoaf 的附加项,但也可以接受正斜杠),以及尖括号中的整个内容,因为您想从标准包含中包含.

接下来你不应该链接到 glaux.那个已经过时了,已经有毒了.

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

I am trying to experiment using basic shaders in my program, I came across a nice tutorial that talks you through writing a basic shader "util class" i guess you would call it? Which should allow me to apply a vertex and fragment shader...So I linked glew to my project (i have also glu, glut and glaux included) and inserted the following into a header file

#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;
}

But my compiler complains about the following:

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

Am I missing something? I searched the internet and it seems like GLchar and the file_contents function do exist?

解决方案

The error line you gave is the compiler telling you, that it doesn't know about the type GLchar, which is defined in GL/gl.h – or GL/glew.h in your case, which also defines it. But it seems to be not properly included.

Your first line should be

#include <GL/glew.h>

i.e. uppercae GL, a forward slash (backslashes are a Microsoaft addition, but forwards are accepted just fine), and the whole thing in angle brackets, as you want to include from the standard includes.

Next you should not link against glaux. That one is so outdated that it's become toxic.

If you want a working OpenGL shader example program, I prepared one at https://github.com/datenwolf/codesamples/tree/master/samples/OpenGL/minimal_glsl

这篇关于设置基本着色器程序 - GLchar 和 file_contents 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆