// init programs here, this will be done only once: void SoTriangles::init (const char *file, GLuint *textures) { // Build program: _vsh.load_and_compile ( GL_VERTEX_SHADER, "../shaders/vsh_smtl_tex_gouraud.glsl" ); _fsh.load_and_compile ( GL_FRAGMENT_SHADER, "../shaders/fsh_tex_gouraud.glsl" ); _prog.init_and_link ( _vsh, _fsh ); // Define buffers needed: gen_vertex_arrays ( 1 ); // will use at least 1 vertex array gen_buffers ( 2 ); // will use at least 1 buffer _prog.uniform_locations (11); // declare here uniforms //_prog.uniform_location ( 0, "vTransf" ); // each name must appear in the shader //_prog.uniform_location ( 1, "vProj" ); //... _prog.uniform_location(0, "vTransf"); _prog.uniform_location(1, "vProj"); _prog.uniform_location(2, "lPos"); _prog.uniform_location(3, "la"); _prog.uniform_location(4, "ld"); _prog.uniform_location(5, "ls"); _prog.uniform_location(6, "ka"); _prog.uniform_location(7, "kd"); _prog.uniform_location(8, "ks"); _prog.uniform_location(9, "sh"); _prog.uniform_location(10, "Tex1"); GsImage I; if (!I.load(file)) std::cout << "COULD NOT LOAD IMAGE!\n"; else std::cout << "loaded\n"; //glGenTextures(1, &id); // ids start at 1 glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, *textures); glTexImage2D(GL_TEXTURE_2D, 0, 4, I.w(), I.h(), 0, GL_RGBA, GL_UNSIGNED_BYTE, I.data()); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); I.init(0, 0); // free image from CPU if (!I.load("../texture/sides.bmp")) std::cout << "COULD NOT LOAD IMAGE!\n"; else std::cout << "loaded\n"; glBindTexture(GL_TEXTURE_2D, textures[1]); glTexImage2D(GL_TEXTURE_2D, 0, 4, I.w(), I.h(), 0, GL_RGBA, GL_UNSIGNED_BYTE, I.data()); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); I.init(0, 0); // free image from CPU }
void SoModelWire::init(const GlProgram& prog, int part) { if (part == 0) { m.load("armmodel/rhand.m"); } else if (part == 1) { m.load("armmodel/rlowerarm.m"); } else if (part == 2) { m.load("armmodel/rupperarm.m"); } // Define buffers needed: set_program(prog); gen_vertex_arrays(1); // will use 1 vertex array gen_buffers(2); // will use 2 buffers: one for coordinates and one for colors uniform_locations(10); // will send 2 variables: the 2 matrices below uniform_location(0, "vTransf"); uniform_location(1, "vProj"); uniform_location(2, "lPos"); uniform_location(3, "la"); uniform_location(4, "ld"); uniform_location(5, "ls"); uniform_location(6, "ka"); uniform_location(7, "kd"); uniform_location(8, "ks"); uniform_location(9, "sh"); }
void SoCapsule::init(const GlProgram& prog) { // Define buffers needed: set_program(prog); gen_vertex_arrays(1); // will use 1 vertex array gen_buffers(2); // will use 2 buffers: one for coordinates and one for colors uniform_locations(2); // will send 2 variables: the 2 matrices below uniform_location(0, "vTransf"); uniform_location(1, "vProj"); }
void gen_player_buffers( GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer, float x, float y, float z, float rx, float ry) { GLfloat *position_data, *normal_data, *uv_data; malloc_buffers(3, 6, &position_data, &normal_data, &uv_data); make_player( position_data, normal_data, uv_data, x, y, z, rx, ry); gen_buffers( 3, 6, position_data, normal_data, uv_data, position_buffer, normal_buffer, uv_buffer); }
// init is called only once: void SoCurve::init() { // Build program: _vsh.load_and_compile(GL_VERTEX_SHADER, "../shaders/vsh_mcol_flat.glsl"); _fsh.load_and_compile(GL_FRAGMENT_SHADER, "../shaders/fsh_flat.glsl"); _prog.init_and_link(_vsh, _fsh); // Define buffers needed: gen_vertex_arrays(1); // will use 1 vertex array gen_buffers(2); // will use 2 buffers: one for coordinates and one for colors _prog.uniform_locations(2); // will send 2 variables: the 2 matrices below _prog.uniform_location(0, "vTransf"); _prog.uniform_location(1, "vProj"); }
void gen_plant_buffers( GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer, float x, float y, float z, float n, int w) { GLfloat *position_data, *normal_data, *uv_data; malloc_buffers(3, 4, &position_data, &normal_data, &uv_data); float rotation = simplex3(x, y, z, 4, 0.5, 2) * 360; make_plant( position_data, normal_data, uv_data, x, y, z, n, w, rotation); gen_buffers( 3, 4, position_data, normal_data, uv_data, position_buffer, normal_buffer, uv_buffer); }
void gen_cube_buffers( GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer, float x, float y, float z, float n, int w) { GLfloat *position_data, *normal_data, *uv_data; malloc_buffers(3, 6, &position_data, &normal_data, &uv_data); make_cube( position_data, normal_data, uv_data, 1, 1, 1, 1, 1, 1, x, y, z, n, w); gen_buffers( 3, 6, position_data, normal_data, uv_data, position_buffer, normal_buffer, uv_buffer); }
void SoPoly::init ( const GlProgram& prog, const GsColor& c, const GsColor& selc ) { _vsh.load_and_compile(GL_VERTEX_SHADER, "../vsh_mcol_flat.glsl"); _fsh.load_and_compile(GL_FRAGMENT_SHADER, "../fsh_flat.glsl"); _prog.init_and_link(_vsh, _fsh); // Define buffers needed: //set_program ( prog ); gen_vertex_arrays ( 1 ); // will use 1 vertex array gen_buffers ( 2 ); // will use 2 buffers: one for coordinates and one for colors _prog.uniform_locations ( 2 ); // will send 2 variables: the 2 matrices below _prog.uniform_location ( 0, "vTransf" ); _prog.uniform_location ( 1, "vProj" ); _color = c; _selcolor = selc; }
void gen_text_buffers( GLuint *position_buffer, GLuint *uv_buffer, float x, float y, float n, char *text) { int length = strlen(text); GLfloat *position_data, *uv_data; malloc_buffers(2, length, &position_data, 0, &uv_data); for (int i = 0; i < length; i++) { make_character( position_data + i * 12, uv_data + i * 12, x, y, n / 2, n, text[i]); x += n; } gen_buffers( 2, length, position_data, 0, uv_data, position_buffer, 0, uv_buffer); }
// init is called only once: void SoLines::init () { // Build program: // Note: the program below could be shared with SoAxis (no need to comppile both // because they are the same), but ok here to avoid adding code for sharing #if (defined WIN32) _vsh.load_and_compile ( GL_VERTEX_SHADER, "../vsh_mcol_flat.glsl" ); _fsh.load_and_compile ( GL_FRAGMENT_SHADER, "../fsh_flat.glsl" ); #else _vsh.load_and_compile ( GL_VERTEX_SHADER, "vsh_mcol_flat.glsl" ); _fsh.load_and_compile ( GL_FRAGMENT_SHADER, "fsh_flat.glsl" ); #endif _prog.init_and_link ( _vsh, _fsh ); // Define buffers needed: gen_vertex_arrays ( 1 ); // will use 1 vertex array gen_buffers ( 2 ); // will use 2 buffers: one for coordinates and one for colors _prog.uniform_locations ( 2 ); // will send 2 variables: the 2 matrices below _prog.uniform_location ( 0, "vTransf" ); _prog.uniform_location ( 1, "vProj" ); }
void SoModel::init () { // Load programs: _vshgou.load_and_compile ( GL_VERTEX_SHADER, "../shaders/vsh_mcol_gouraud.glsl" ); _fshgou.load_and_compile ( GL_FRAGMENT_SHADER, "../shaders/fsh_gouraud.glsl" ); _proggouraud.init_and_link ( _vshgou, _fshgou ); _vshphong.load_and_compile ( GL_VERTEX_SHADER, "../shaders/vsh_mcol_phong.glsl" ); _fshphong.load_and_compile ( GL_FRAGMENT_SHADER, "../shaders/fsh_mcol_phong.glsl" ); _progphong.init_and_link ( _vshphong, _fshphong ); // Define buffers needed: gen_vertex_arrays ( 1 ); // will use 1 vertex array gen_buffers ( 3 ); // will use 3 buffers _proggouraud.uniform_locations ( 9 ); // will send 9 variables _proggouraud.uniform_location ( 0, "vTransf" ); _proggouraud.uniform_location ( 1, "vProj" ); _proggouraud.uniform_location ( 2, "lPos" ); _proggouraud.uniform_location ( 3, "la" ); _proggouraud.uniform_location ( 4, "ld" ); _proggouraud.uniform_location ( 5, "ls" ); _proggouraud.uniform_location ( 6, "ka" ); _proggouraud.uniform_location ( 7, "ks" ); _proggouraud.uniform_location ( 8, "sh" ); _progphong.uniform_locations ( 9 ); // will send 9 variables _progphong.uniform_location ( 0, "vTransf" ); _progphong.uniform_location ( 1, "vProj" ); _progphong.uniform_location ( 2, "lPos" ); _progphong.uniform_location ( 3, "la" ); _progphong.uniform_location ( 4, "ld" ); _progphong.uniform_location ( 5, "ls" ); _progphong.uniform_location ( 6, "ka" ); _progphong.uniform_location ( 7, "ks" ); _progphong.uniform_location ( 8, "sh" ); }
t_obj *load_obj(const char *file) { t_obj *obj; int fd; char *line; char **tmp; t_object *object; char *mtllib; char last; int offset; int i; mtllib = NULL; last = 0; offset = 0; i = 0; object = new_object(); if ((obj = ft_memalloc(sizeof(t_obj))) == NULL) return (NULL); obj->transform = new_transform(); if ((fd = open(file, O_RDONLY)) == -1) { exit_error("Can't find .obj"); return (NULL); } while (get_next_line(fd, &line) != 0) { i++; tmp = ft_strsplit(line, ' '); if (ft_strlen(line) == 1 && last == 'f') { gen_buffers(object); offset += object->vertex.size; add_elem(&obj->objects, object); last = 0; object = new_object(); ft_freetab((void **)tmp); ft_memdel((void **)&line); continue ; } if (ft_tabsize((void **)tmp) < 2) { ft_freetab((void **)tmp); ft_memdel((void **)&line); continue ; } if (ft_strcmp(tmp[0], "usemtl") == 0) object->usemtl = ft_strdup(tmp[1]); if (!add_v_f(object, tmp, &last, offset) && ft_strcmp(tmp[0], "mtllib") == 0) mtllib = ft_strdup(line + 7); ft_freetab((void **)tmp); ft_memdel((void **)&line); } if (line) ft_memdel((void **)&line); gen_buffers(object); add_elem(&obj->objects, object); if (mtllib) { load_material_lib(obj, mtllib, (char *)file); ft_memdel((void **)&mtllib); } return (obj); }
void gen_chunk_buffers(Chunk *chunk) { Map *map = &chunk->map; int faces = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } faces += total; } END_MAP_FOR_EACH; GLfloat *position_data, *normal_data, *uv_data; malloc_buffers(3, faces, &position_data, &normal_data, &uv_data); int position_offset = 0; int uv_offset = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } if (total == 0) { continue; } if (is_plant(e->w)) { float rotation = simplex3(e->x, e->y, e->z, 4, 0.5, 2) * 360; make_plant( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, e->x, e->y, e->z, 0.5, e->w, rotation); } else { make_cube( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, f1, f2, f3, f4, f5, f6, e->x, e->y, e->z, 0.5, e->w); } position_offset += total * 18; uv_offset += total * 12; } END_MAP_FOR_EACH; gen_buffers( 3, faces, position_data, normal_data, uv_data, &chunk->position_buffer, &chunk->normal_buffer, &chunk->uv_buffer); chunk->faces = faces; chunk->dirty = 0; }