我对opengl着色器编译有问题。问题是当我运行程序时,我得到了这个错误:
顶点信息
0(1):错误c0000:语法错误,令牌''意外''
相同的消息是片段对象。最后,我获得了“未经验证的程序”错误。这是我的初始化着色器代码:
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);
}
答案 0 :(得分:2)
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;
}
答案 1 :(得分:0)
我解决了genpfault的问题。我不得不写gl位置并添加分号。谢谢大家!