gouraudhdl::gouraudhdl() { type = "gouraud"; emission = vec3f(0.0, 0.0, 0.0); ambient = vec3f(0.1, 0.1, 0.1); diffuse = vec3f(1.0, 1.0, 1.0); specular = vec3f(1.0, 1.0, 1.0); shininess = 1.0; if (vertex == 0 && fragment == 0 && program == 0) { /* TODO Assignment 3: Load and link the shaders. Keep in mind that vertex, fragment, * and program are static variables meaning they are *shared across all instances of * this class. So you only have to initialize them once when the first instance of * the class is created. */ glEnable(GL_DEPTH_TEST); vertex = load_shader_file("res/gouraud.vx", GL_VERTEX_SHADER); fragment = load_shader_file("res/gouraud.ft", GL_FRAGMENT_SHADER); program = glCreateProgram(); progmap.insert(pair<string, int>("gouraud", program)); //cout << "Program number:" << program <<endl; printProgramInfoLog(program); glAttachShader(program, vertex); glAttachShader(program, fragment); GLint pok = 0; glGetProgramiv(program, GL_LINK_STATUS, &pok); if (pok == GL_FALSE) cout << "program bad" << endl; if (pok == GL_TRUE) cout << "program good" << endl; glAttachShader(program, vertex); glAttachShader(program, fragment); GLint vok = 0; GLint fok = 0; glGetShaderiv(vertex, GL_COMPILE_STATUS, &vok); glGetShaderiv(fragment, GL_COMPILE_STATUS, &fok); if (vok == GL_FALSE) cout << "Something in the vertex shader fed up"; if (vok == GL_TRUE) cout << "gouraud V shader is good"; if (fok == GL_FALSE) cout << "Something in the fragment shader fed up"; if (vok == GL_TRUE) cout << "F shader is good"; printShaderInfoLog(vertex); printShaderInfoLog(fragment); glLinkProgram(program); pok = 0; glGetProgramiv(program, GL_LINK_STATUS, &pok); if (pok == GL_FALSE) cout << "program bad" << endl; if (pok == GL_TRUE) cout << "program good" << endl; } }
void canvashdl::load_shader() { GLuint vertex = load_shader_file(working_directory + "res/canvas.vx", GL_VERTEX_SHADER); GLuint fragment = load_shader_file(working_directory + "res/canvas.ft", GL_FRAGMENT_SHADER); screen_shader = glCreateProgram(); glAttachShader(screen_shader, vertex); glAttachShader(screen_shader, fragment); glLinkProgram(screen_shader); }
void recurse_dir(char *path) { DIR *dir = opendir(path); if(dir == NULL) errx(-1, "Can't open directory '%s'", path); struct dirent *dp; while((dp = readdir(dir)) != NULL) { char *buf = malloc(strlen(path) + strlen(dp->d_name)+2); strcpy(buf, path); strcat(buf, "/"); strcat(buf, dp->d_name); struct stat statbuf; stat(buf, &statbuf); if(S_ISDIR(statbuf.st_mode) && dp->d_name[0] != '.') { recurse_dir(buf); } else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".png") == 0) { if(strncmp(dp->d_name, "ani_", 4) == 0) init_animation(buf); else load_texture(buf); } else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".wav") == 0) { load_sound(buf); } else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".sha") == 0) { load_shader_file(buf); } else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".obj") == 0) { load_model(buf); } free(buf); } closedir(dir); }
int create_shader(const char* filename, int type) { assert(filename != NULL); const char* shader_string = load_shader_file(filename); if(shader_string == NULL) { print_log("ERROR: Failed to load %s\n", filename); return -1; } int params = GL_TRUE; GLuint s = glCreateShader(type); glShaderSource(s, 1, &shader_string, NULL); glCompileShader(s); glGetShaderiv(s, GL_COMPILE_STATUS, ¶ms); if(GL_TRUE != params) { print_log("ERROR: GL shader index %i did not compile.\n", s); print_log_shader_info(s); return -2; } return s; }
int ShaderProgram::add_shader(const GLenum shader_type, const string& file_name) { GLuint shader = glCreateShader(shader_type); //Load in the source code from the file string shader_source; if (load_shader_file(file_name, shader_source) != 0) { std::cout << "Source for " << file_name << " could not be loaded" << std::endl; return 1; } // std::cout << "Shader source: " << shader_source.size() << std::endl << shader_source << std::endl; //Set the source and compile the shader const char * src = shader_source.data(); glShaderSource(shader, 1, (const GLchar**)&src, NULL); glCompileShader(shader); GLint compile_success; glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_success); if (compile_success == GL_FALSE) { //Check for error specifics GLint error_len = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &error_len); string error_msg; error_msg.reserve(error_len); glGetShaderInfoLog(shader, error_len, &error_len, (GLchar*)error_msg.data()); //Log the error (currently just print) std::cout << "Compilation error in " << file_name << std::endl; std::cout << error_msg.data() << std::endl; glDeleteShader(shader); return 2; } //Attach the shader to the program glAttachShader(program, shader); GLint currprog; glGetProgramiv(program, GL_ATTACHED_SHADERS, &currprog); std::cout << program << "Attached progs: " << currprog << std::endl; //Add the shader to the list of included shaders shaders.push_back(pair<GLuint, GLenum>(shader, shader_type)); std::cout << "SHADER SUCCESSFULLY ADDED: " << file_name << std::endl; return 0; }
shader* tes_load_file(char* filename) { return load_shader_file(filename, GL_TESS_EVALUATION_SHADER); }
shader* tcs_load_file(char* filename) { return load_shader_file(filename, GL_TESS_CONTROL_SHADER); }
shader* gs_load_file(char* filename) { return load_shader_file(filename, GL_GEOMETRY_SHADER); }
shader* fs_load_file(char* filename) { return load_shader_file(filename, GL_FRAGMENT_SHADER); }
shader* vs_load_file(char* filename) { return load_shader_file(filename, GL_VERTEX_SHADER); }
bumpmaphdl::bumpmaphdl() { type = "bumpmap"; shininess = 1.0; if (vertex == 0 && fragment == 0 && program == 0) { /* TODO Assignment 3: Load and link the shaders and load the texture Keep in mind that vertex, fragment, * and program are static variables meaning they are *shared across all instances of * this class. So you only have to initialize them once when the first instance of * the class is created. */ glEnable(GL_DEPTH_TEST); vertex = load_shader_file("res/bumpmap.vx", GL_VERTEX_SHADER); fragment = load_shader_file("res/bumpmap.ft", GL_FRAGMENT_SHADER); program = glCreateProgram(); progmap.insert(pair<string, int>("bumpmap", program)); printProgramInfoLog(program); glAttachShader(program, vertex); glAttachShader(program, fragment); GLint pok = 0; glGetProgramiv(program, GL_LINK_STATUS, &pok); if (pok == GL_FALSE) cout << "program bad" << endl; if (pok == GL_TRUE) cout << "program good" << endl; glAttachShader(program, vertex); glAttachShader(program, fragment); GLint vok = 0; GLint fok = 0; glGetShaderiv(vertex, GL_COMPILE_STATUS, &vok); glGetShaderiv(fragment, GL_COMPILE_STATUS, &fok); if (vok == GL_FALSE) cout << "Something in the vertex shader fed up"; if (vok == GL_TRUE) cout << "normmap V shader is good"; if (fok == GL_FALSE) cout << "Something in the fragment shader fed up"; if (vok == GL_TRUE) cout << "F shader is good"; printShaderInfoLog(vertex); printShaderInfoLog(fragment); glLinkProgram(program); pok = 0; glGetProgramiv(program, GL_LINK_STATUS, &pok); if (pok == GL_FALSE) cout << "program bad" << endl; if (pok == GL_TRUE) cout << "program good" << endl; unsigned width, height ,w2, h2; vector<unsigned char> image,other; loadtexture(width, height, image, "res/img/chest.png"); loadtexture(w2, h2, other, "res/img/chest_nm.png"); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.data()); glGenerateMipmap(GL_TEXTURE_2D); glGenTextures(1, &normalmap); glBindTexture(GL_TEXTURE_2D, normalmap); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, other.data()); glGenerateMipmap(GL_TEXTURE_2D); } }