Opengl错误C0000编译错误

Opengl error C0000 compiling error

本文关键字:错误 编译 C0000 Opengl      更新时间:2023-10-16

我在编译opengl着色器时遇到问题。问题是,当我运行程序时,我得到了这个错误:

顶点信息

0(1):错误c0000:语法错误,令牌"处出现意外""

对于fragment对象也是相同的消息。最后,我得到了一个"程序未验证"错误。这是我的初始化着色器代码:

struct ShadeState {
int gl_program_id = 0;          // OpenGL program handle
int gl_vertex_shader_id = 0;    // OpenGL vertex shader handle
int gl_fragment_shader_id = 0;  // OpenGL fragment shader handle
};
// initialize the shaders
void init_shaders(ShadeState* state) {
// load shader code from files
auto vertex_shader_code = load_text_file("shade_vertex.glsl");
auto fragment_shader_code = load_text_file("shade_fragment.glsl");
auto vertex_shader_codes = (char *)vertex_shader_code.c_str();
auto fragment_shader_codes = (char *)fragment_shader_code.c_str();
//devono essere costanti altrimenti glShaderSource non li accetta
//auto vertex_codes = (const GLchar*)vertex_shader_codes;
//auto fragment_codes = (const GLchar*)fragment_shader_codes;
//GLint const vert_size = vertex_shader_code.size();
//GLint const frag_size = fragment_shader_code.size();
// create shaders
state->gl_vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);      //come da documentazione
state->gl_fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);  //come da documentazione

// load shaders code onto the GPU
//glShaderCode non esiste!
glShaderSource(state->gl_vertex_shader_id, 1, (const GLchar**)&vertex_shader_codes, NULL);
glShaderSource(state->gl_fragment_shader_id, 1, (const GLchar**)&fragment_shader_codes, NULL);
// compile shaders
glCompileShader(state->gl_vertex_shader_id);
glCompileShader(state->gl_fragment_shader_id);
// check if shaders are valid
//funzione presente in glcommon.h
error_if_glerror();
error_if_shader_not_valid(state->gl_vertex_shader_id);
error_if_shader_not_valid(state->gl_fragment_shader_id);

// create program
state->gl_program_id = glCreateProgram();
// attach shaders
glAttachShader(state->gl_program_id, state->gl_vertex_shader_id);
glAttachShader(state->gl_program_id, state->gl_fragment_shader_id);

// bind vertex attributes locations
//faccio il bind delle variabili in input del vertex shader
glBindAttribLocation(state->gl_program_id, 0, "vertex_pos");    // primo attributo in shade_vertex
glBindAttribLocation(state->gl_program_id, 1, "vertex_norm");   //secondo attributo in shade_vertex
// link program
glLinkProgram(state->gl_program_id);
// check if program is valid
//funzione presente in glcommon.h
error_if_glerror();
error_if_program_not_valid(state->gl_program_id);
}

我该如何解决?

编辑

shade_vertex.glsl

#version 120
attribute vec3 vertex_pos;          // vertex position (in mesh coordinate frame)
attribute vec3 vertex_norm;         // vertex normal   (in mesh coordinate frame)
uniform mat4 mesh_frame;            // mesh frame (as a matrix)
uniform mat4 camera_frame_inverse;  // inverse of the camera frame (as a matrix)
uniform mat4 camera_projection;     // camera projection
varying vec3 pos;                   // [to fragment shader] vertex position (in world coordinate)
varying vec3 norm;                  // [to fragment shader] vertex normal (in world coordinate)
// main function
void main() {
// compute pos and normal in world space and set up variables for fragment shader (use     mesh_frame)
// project vertex position to gl_Position using mesh_frame, camera_frame_inverse and camera_projection
}

shade_fragment.glsl

#version 120
varying vec3 pos;                   // [from vertex shader] position in world space
varying vec3 norm;                  // [from vertex shader] normal in world space (need normalization)
uniform vec3 camera_pos;            // camera position (center of the camera frame)
uniform vec3 ambient;               // scene ambient
uniform int lights_num;             // number of lights
uniform vec3 light_pos[16];         // light positions
uniform vec3 light_intensity[16];   // light intensities
uniform vec3 material_kd;           // material kd
uniform vec3 material_ks;           // material ks
uniform float material_n;           // material n
// main
void main() {
// re-normalize normals
// use faceforward to ensure the normals points toward us
// accumulate ambient
vec3 c = vec3(0,0,0)
// foreach light
    // compute point light color at pos
    // compute light direction at pos
    // compute view direction using camera_pos and pos
    // compute h
    // accumulate blinn-phong model
// output final color by setting gl_FragColor
gl_FragColor = vec4(c,1);
}

GLSL要求换行符(n)跟在#version指令后面。

我怀疑您的load_text_file()函数没有在源文本文件中保留换行符,或者文本文件本身缺少换行符。

其他问题:

  • 顶点着色器需要写入gl_Position
  • 碎片着色器在vec3 c = vec3(0,0,0)之后缺少分号

这些(更新的)着色器在我的系统上编译:

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
struct Program
{
    static GLuint Load( const char* vert, const char* geom, const char* frag )
    {
        GLuint prog = glCreateProgram();
        if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
        if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
        if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
        glLinkProgram( prog );
        CheckStatus( prog );
        return prog;
    }
private:
    static void CheckStatus( GLuint obj )
    {
        GLint status = GL_FALSE;
        if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
        if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
        if( status == GL_TRUE ) return;
        GLchar log[ 1 << 15 ] = { 0 };
        if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
        if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
        std::cerr << log << std::endl;
        exit( -1 );
    }
    static void AttachShader( GLuint program, GLenum type, const char* src )
    {
        GLuint shader = glCreateShader( type );
        glShaderSource( shader, 1, &src, NULL );
        glCompileShader( shader );
        CheckStatus( shader );
        glAttachShader( program, shader );
        glDeleteShader( shader );
    }
};
#define GLSL(version, shader) "#version " #version "n" #shader
const char* vert = GLSL
(
    120,
    attribute vec3 vertex_pos;          // vertex position (in mesh coordinate frame)
    attribute vec3 vertex_norm;         // vertex normal   (in mesh coordinate frame)
    uniform mat4 mesh_frame;            // mesh frame (as a matrix)
    uniform mat4 camera_frame_inverse;  // inverse of the camera frame (as a matrix)
    uniform mat4 camera_projection;     // camera projection
    varying vec3 pos;                   // [to fragment shader] vertex position (in world coordinate)
    varying vec3 norm;                  // [to fragment shader] vertex normal (in world coordinate)
    // main function
    void main() {
    // compute pos and normal in world space and set up variables for fragment shader (use     mesh_frame)
    // project vertex position to gl_Position using mesh_frame, camera_frame_inverse and camera_projection
    gl_Position = vec4( 0, 0, 0, 1 );
    }
);
const char* frag = GLSL
(
    120,
    varying vec3 pos;                   // [from vertex shader] position in world space
    varying vec3 norm;                  // [from vertex shader] normal in world space (need normalization)
    uniform vec3 camera_pos;            // camera position (center of the camera frame)
    uniform vec3 ambient;               // scene ambient
    uniform int lights_num;             // number of lights
    uniform vec3 light_pos[16];         // light positions
    uniform vec3 light_intensity[16];   // light intensities
    uniform vec3 material_kd;           // material kd
    uniform vec3 material_ks;           // material ks
    uniform float material_n;           // material n
    // main
    void main() {
    // re-normalize normals
    // use faceforward to ensure the normals points toward us
    // accumulate ambient
    vec3 c = vec3(0,0,0);
    // foreach light
        // compute point light color at pos
        // compute light direction at pos
        // compute view direction using camera_pos and pos
        // compute h
        // accumulate blinn-phong model
    // output final color by setting gl_FragColor
    gl_FragColor = vec4(c,1);
    }
);
void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glutSwapBuffers();
}
int main(int argc, char **argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 600, 600 );
    glutCreateWindow( "GLUT" );
    glewInit();
    GLuint prog = Program::Load( vert, NULL, frag );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

我用genpfault的解决方案解决了问题。我不得不写gl位置并加上分号。谢谢大家!