static int shader_eq (lua_State *L) { shared_ptr<Shader> a = check_shader(L, 1); shared_ptr<Shader> b = check_shader(L, 2); lua_pushboolean(L, a.get() == b.get()); return 1; }
void Shader::LoadShader(const char* vertex_file_path,const char* fragment_file_path){ GLuint VertexShaderID=glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID=glCreateShader(GL_FRAGMENT_SHADER); std::string VertexShaderCode; read_shader(VertexShaderCode,vertex_file_path); std::string FragmentShaderCode; read_shader(FragmentShaderCode,fragment_file_path); GLint Result=GL_FALSE; int InfoLogLength; // Compile Vertex Shader compile_shader(VertexShaderID,VertexShaderCode); check_shader(VertexShaderID,Result,InfoLogLength);// Check Vertex Shader // Compile Fragment Shader compile_shader(FragmentShaderID,FragmentShaderCode); check_shader(FragmentShaderID,Result,InfoLogLength); // Link the program GLuint ProgramID=glCreateProgram(); glAttachShader(ProgramID,VertexShaderID); glAttachShader(ProgramID,FragmentShaderID); glLinkProgram(ProgramID); // Check the program glGetProgramiv(ProgramID,GL_LINK_STATUS,&Result); glGetProgramiv(ProgramID,GL_INFO_LOG_LENGTH,&InfoLogLength); check_program(ProgramID,InfoLogLength); glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); programID=ProgramID; }
static int shader_load(lua_State *L) { shared_ptr<Shader> shader = check_shader(L, 1); const char* filename = luaL_checkstring(L, 2); int shader_type = luaL_checkint(L, 3); lua_pushboolean(L, shader->Load_shader(filename, shader_type)); return 1; }
void gl_program::create_program() { vector<string> uniforms; program_id = glCreateProgram(); GLuint fragment_id = glCreateShader(GL_FRAGMENT_SHADER); GLuint vertex_id = glCreateShader(GL_VERTEX_SHADER); string vertex_shader = generate_vertex_shader(uniforms); string fragment_shader = generate_fragment_shader(uniforms); debug_flags(PRINT_SHADERS, "---------- Vertex Shader ----------\n%s\n", vertex_shader.c_str()); debug_flags(PRINT_SHADERS, "---------- Fragment Shader ----------\n%s\n", fragment_shader.c_str()); const char* _vertex_shader = vertex_shader.c_str(); const char* _fragment_shader = fragment_shader.c_str(); glShaderSource(fragment_id, 1, &(_fragment_shader), NULL); glCompileShader(fragment_id); glShaderSource(vertex_id, 1, &(_vertex_shader), NULL); glCompileShader(vertex_id); check_shader(fragment_id); check_shader(vertex_id); //Link all the things glAttachShader(program_id, fragment_id); glAttachShader(program_id, vertex_id); glLinkProgram(program_id); CHECK_GL_ERROR("Linking Program"); //Use the program glUseProgram(program_id); CHECK_GL_ERROR("Use Program"); //Add the IDs to array so they get deleted in cleanup shader_ids.push_back(fragment_id); shader_ids.push_back(vertex_id); for (const string & cur : uniforms) { GLuint uniform_loc = glGetUniformLocation(program_id, cur.c_str()); uniform_locations.insert(make_pair(cur, uniform_loc)); CHECK_GL_ERROR("Getting Uniform Location for " + cur); } }
void startup() { glGenVertexArrays(1, &vao); glBindVertexArray(vao); printf("create program\n"); program = glCreateProgram(); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, fs_source, NULL); glCompileShader(fs); check_shader(fs); printf("create shader\n"); GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, vs_source, NULL); glCompileShader(vs); check_shader(vs); printf("attach shaders\n"); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); printf("locate uniforms\n"); uniforms.zoom = glGetUniformLocation(program, "zoom"); uniforms.offset = glGetUniformLocation(program, "offset"); uniforms.C = glGetUniformLocation(program, "C"); uniforms.screen = glGetUniformLocation(program, "screen"); uniforms.iter = glGetUniformLocation(program, "max_iterations"); uniforms.mode = glGetUniformLocation(program, "mode"); uniforms.damp = glGetUniformLocation(program, "damp"); uniforms.log_a_min = glGetUniformLocation(program, "log_a_min"); uniforms.log_a_max = glGetUniformLocation(program, "log_a_max"); printf("delete shaders\n"); glDeleteShader(vs); glDeleteShader(fs); printf("gen texture\n"); glGenTextures(1, &palette_texture); glBindTexture(GL_TEXTURE_1D, palette_texture); glTexImage1D( GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_RGB, GL_UNSIGNED_BYTE_3_3_2, NULL); glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGB, GL_UNSIGNED_BYTE, palette); glGenerateMipmap(GL_TEXTURE_1D); }
GLuint GLSLProgram::compile_program( const char* vsource, const char* fsource, const char* gsource, GLenum gsInput, GLenum gsOutput) { GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vertexShader, 1, &vsource, 0); glShaderSource(fragShader, 1, &fsource, 0); glCompileShader(vertexShader); if ( _FAILED(check_shader(vertexShader)) ) { fprintf(stderr, "Fail to compile vertex shader:\n"); fprintf(stderr, "%s\n", vsource); return 0; } glCompileShader(fragShader); if ( _FAILED(check_shader(fragShader)) ) { fprintf(stderr, "Fail to compile fragment shader:\n"); fprintf(stderr, "%s\n", fsource); return 0; } GLuint program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragShader); //// create geometry shader if given if ( gsource ) { GLuint geometryShader = glCreateShader(GL_GEOMETRY_SHADER_EXT); glShaderSource(geometryShader, 1, &gsource, 0); glCompileShader(geometryShader); if ( _FAILED(check_shader(geometryShader)) ) { fprintf(stderr, "Fail to compile geometry shader:\n"); fprintf(stderr, "%s\n", gsource); return 0; } glAttachShader(program, geometryShader); glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, gsInput); glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, gsOutput); glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, 4); } glLinkProgram(program); GLint status; glGetProgramiv(program, GL_LINK_STATUS, &status); if ( status != GL_TRUE ) { char log[2048]; int len; glGetProgramInfoLog(program, 2048, (GLsizei*)&len, log); fprintf(stderr, "Error: program(%04d), Info Log: %s\n", (int)program, log); glDeleteProgram(program); return 0; } return program; }