bjorn
bjorn

Reputation: 338

Opengl error C0000 compiling error

I've a problem with opengl shader compiling. The problem is that when I run the program, I obtain this error:

Vertex info

0(1) : error c0000: syntax error, unexpected '' at token ''

the same message is for the fragment object. At the end I obtain a 'program not validated' error. here's my initialization shader code:

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

How ca I resolve?

EDIT

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

Upvotes: 0

Views: 4754

Answers (2)

bjorn
bjorn

Reputation: 338

I resolved with the solution of genpfault. I had to write gl position and add the semicolon. Thanks to you all!

Upvotes: 0

genpfault
genpfault

Reputation: 52084

GLSL requires that a newline (\n) follow the #version directive.

I suspect your load_text_file() function is either not preserving newlines in the source text files or the text files themselves are missing newlines.

Other issues:

  • Your vertex shader needs to write to gl_Position.
  • Your fragment shader is missing a semicolon after vec3 c = vec3(0,0,0)

These (updated) shaders compile on my system:

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

Upvotes: 2

Related Questions