ui_textbox* ui_textbox_new() { ui_textbox* tb = malloc(sizeof(ui_textbox)); tb->inner = ui_rectangle_new(); ui_rectangle_set_texture(tb->inner, asset_hndl_new_load(ui_style_current->box_back_image), ui_style_current->box_back_width, ui_style_current->box_back_height, ui_style_current->box_back_tile); ui_rectangle_set_border(tb->inner, ui_style_current->box_back_border_size, ui_style_current->box_back_border_color); ui_rectangle_set_glitch(tb->inner, ui_style_current->box_glitch); ui_rectangle_set_color(tb->inner, ui_style_current->box_inset_color); tb->outer = ui_rectangle_new(); ui_rectangle_set_texture(tb->outer, asset_hndl_new_load(ui_style_current->box_back_image), ui_style_current->box_back_width, ui_style_current->box_back_height, ui_style_current->box_back_tile); ui_rectangle_set_border(tb->outer, ui_style_current->box_back_border_size, ui_style_current->box_back_border_color); ui_rectangle_set_glitch(tb->outer, ui_style_current->box_glitch); ui_rectangle_set_color(tb->outer, ui_style_current->box_up_color); tb->contents = ui_text_new(); ui_text_set_color(tb->contents, ui_style_current->box_text_color); ui_text_align(tb->contents, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER); ui_text_move(tb->contents, ui_rectangle_center(tb->inner)); ui_text_draw_string(tb->contents, ""); tb->label = ui_text_new(); ui_text_set_color(tb->label, ui_style_current->box_label_color); ui_text_align(tb->label, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER); ui_text_move(tb->label, ui_rectangle_center(tb->inner)); ui_text_draw_string(tb->label, ""); tb->password = false; tb->max_chars = 256; tb->selected = false; tb->active = true; tb->enabled = true; return tb; }
ui_browser* ui_browser_new() { int width = graphics_viewport_width(); int height = graphics_viewport_height(); ui_browser* b = malloc(sizeof(ui_browser)); b->outer = ui_rectangle_new(); ui_rectangle_move(b->outer, vec2_new(width - 300, 10)); ui_rectangle_resize(b->outer, vec2_new(280, height - 20)); ui_rectangle_set_texture(b->outer, asset_hndl_new_load(ui_style_current->box_back_image), ui_style_current->box_back_width, ui_style_current->box_back_height, ui_style_current->box_back_tile); ui_rectangle_set_border(b->outer, ui_style_current->box_back_border_size, ui_style_current->box_back_border_color); ui_rectangle_set_glitch(b->outer, ui_style_current->box_glitch); ui_rectangle_set_color(b->outer, vec4_grey()); b->inner = ui_listbox_new(); ui_listbox_move(b->inner, vec2_new(width - 290, 20)); ui_listbox_resize(b->inner, vec2_new(260, height - 40)); strcpy(b->directory.ptr, SDL_GetWorkingDir()); ui_browser_chdir(b, b->directory); b->active = true; return b; }
static int load_resources(void* unused) { folder_load(P("./resources/terrain/")); folder_load(P("./resources/vegetation/")); static_object* skydome = entity_new("skydome", static_object); skydome->renderable = asset_hndl_new_load(P("./resources/terrain/skydome.obj")); ((renderable*)asset_hndl_ptr(skydome->renderable))->material = asset_hndl_new_load(P("$CORANGE/shaders/skydome.mat")); skydome->position = vec3_new(512, 0, 512); skydome->scale = vec3_new(1024, 1024, 1024); landscape* world = entity_new("world", landscape); world->terrain = asset_hndl_new_load(P("./resources/terrain/heightmap.raw")); world->normalmap = asset_hndl_new_load(P("./resources/terrain/normalsmap.dds")); world->colormap = asset_hndl_new_load(P("./resources/terrain/colormap.dds")); world->attributemap = asset_hndl_new_load(P("./resources/terrain/attributemap.dds")); landscape_set_textures(world, asset_hndl_new_load(P("./resources/terrain/surface.dds")), asset_hndl_new_load(P("./resources/terrain/surface_bump.dds")), asset_hndl_new_load(P("./resources/terrain/surface_far.dds")), asset_hndl_new_load(P("./resources/terrain/surface_far_bump.dds"))); vegetation_init(); //vegetation_add_type(asset_get("./resources/terrain/heightmap.raw"), // asset_get("./resources/vegetation/grass.obj"), // 4.0); ui_button* loading = ui_elem_get("loading"); ui_spinner* load_spinner = ui_elem_get("load_spinner"); ui_button* framerate = ui_elem_get("framerate"); ui_button* wireframe = ui_elem_get("wireframe"); ui_button* freecam = ui_elem_get("freecam"); loading->active = false; load_spinner->active = false; framerate->active = true; wireframe->active = true; freecam->active = true; loading_resources = false; return 1; }
ui_button* ui_button_new() { ui_button* b = malloc(sizeof(ui_button)); b->back = ui_rectangle_new(); ui_rectangle_set_texture(b->back, asset_hndl_new_load(ui_style_current->box_back_image), ui_style_current->box_back_width, ui_style_current->box_back_height, ui_style_current->box_back_tile); ui_rectangle_set_border(b->back, ui_style_current->box_back_border_size, ui_style_current->box_back_border_color); ui_rectangle_set_glitch(b->back, ui_style_current->box_glitch); ui_rectangle_blend(b->back, ui_style_current->box_blend_src, ui_style_current->box_blend_dst); b->label = ui_text_new_string("Button1"); ui_text_move(b->label, ui_rectangle_center(b->back)); ui_text_set_color(b->label, ui_style_current->box_text_color); ui_text_align(b->label, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER); ui_text_draw(b->label); b->onclick = NULL; b->onclick_data = NULL; b->up_color = ui_style_current->box_up_color; b->down_color = ui_style_current->box_down_color; b->active = true; b->enabled = true; b->pressed = false; return b; }
font* font_load_file(char* filename) { font* f = malloc(sizeof(font)); f->width = 0; f->height = 0; /* Encodes ASCII */ f->locations = malloc( sizeof(vec2) * 256 ); f->sizes = malloc( sizeof(vec2) * 256 ); f->offsets = malloc( sizeof(vec2) * 256 ); SDL_RWops* file = SDL_RWFromFile(filename, "r"); if(file == NULL) { error("Could not load file %s", filename); } char line[1024]; while(SDL_RWreadline(file, line, 1024)) { int tex_id; char tex_file[MAX_PATH]; if (sscanf(line, "page id=%i file=%s", &tex_id, tex_file) > 0) { fpath location; SDL_PathFileLocation(location.ptr, filename); /* +1 to remove beginning quotation */ strcat(location.ptr, tex_file+1); /* remove ending quotation */ location.ptr[strlen(location.ptr)-1] = '\0'; f->texture_map = asset_hndl_new_load(location); } int lineheight, base, scalew, scaleh; int pages, packed, a_chan, r_chan, g_chan, b_chan; if (sscanf(line, "common lineHeight=%i base=%i scaleW=%i scaleH=%i " "pages=%i packed=%i alphaChnl=%i " "redChnl=%i greenChnl=%i blueChnl=%i", &lineheight, &base, &scalew, &scaleh, &pages, &packed, &a_chan, &r_chan, &g_chan, &b_chan) > 0) { f->width = scalew; f->height = scaleh; } int id, x, y, w, h, x_off, y_off, x_adv, page, chnl; if (sscanf(line, "char id=%i x=%i y=%i width=%i height=%i " "xoffset=%i yoffset=%i xadvance=%i page=%i chnl=%i", &id, &x, &y, &w, &h, &x_off, &y_off, &x_adv, &page, &chnl) > 0) { f->locations[id] = vec2_new((float)x / f->width, (float)y / f->height); f->sizes[id] = vec2_new((float)w / f->width, (float)h / f->height); f->offsets[id] = vec2_new((float)x_off / f->width, (float)y_off / f->height); } } SDL_RWclose(file); return f; }
int main(int argc, char **argv) { #ifdef _WIN32 FILE* ctt = fopen("CON", "w" ); FILE* fout = freopen( "CON", "w", stdout ); FILE* ferr = freopen( "CON", "w", stderr ); #endif corange_init("../../assets_core"); graphics_viewport_set_title("Teapot"); graphics_viewport_set_size(1280, 720); camera* cam = entity_new("camera", camera); cam->position = vec3_new(5, 5, 5); cam->target = vec3_new(0, 0, 0); teapot_shader = asset_hndl_new_load(P("./assets/teapot.mat")); teapot_object = asset_hndl_new_load(P("./assets/teapot.obj")); int running = 1; SDL_Event e = {0}; while(running) { frame_begin(); camera* cam = entity_get("camera"); while(SDL_PollEvent(&e)) { switch(e.type){ case SDL_KEYDOWN: case SDL_KEYUP: if (e.key.keysym.sym == SDLK_ESCAPE) { running = 0; } if (e.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); } if (e.key.keysym.sym == SDLK_r && e.key.keysym.mod == KMOD_LCTRL) { asset_reload_all(); } break; case SDL_QUIT: running = 0; break; } camera_control_orbit(cam, e); ui_event(e); } ui_update(); glClearColor(0.25, 0.25, 0.25, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); shader_program* shader = material_first_program(asset_hndl_ptr(&teapot_shader)); shader_program_enable(shader); shader_program_set_mat4(shader, "world", mat4_id()); shader_program_set_mat4(shader, "view", camera_view_matrix(cam)); shader_program_set_mat4(shader, "proj", camera_proj_matrix(cam)); shader_program_set_texture(shader, "cube_beach", 0, asset_hndl_new_load(P("$CORANGE/water/cube_sea.dds"))); shader_program_set_vec3(shader, "camera_direction", camera_direction(cam)); renderable* r = asset_hndl_ptr(&teapot_object); for(int i=0; i < r->num_surfaces; i++) { renderable_surface* s = r->surfaces[i]; int mentry_id = min(i, ((material*)asset_hndl_ptr(&r->material))->num_entries-1); material_entry* me = material_get_entry(asset_hndl_ptr(&r->material), mentry_id); glBindBuffer(GL_ARRAY_BUFFER, s->vertex_vbo); shader_program_enable_attribute(shader, "vPosition", 3, 18, (void*)0); shader_program_enable_attribute(shader, "vNormal", 3, 18, (void*)(sizeof(float) * 3)); //shader_program_enable_attribute(shader, "vTangent", 3, 18, (void*)(sizeof(float) * 6)); //shader_program_enable_attribute(shader, "vBinormal", 3, 18, (void*)(sizeof(float) * 9)); //shader_program_enable_attribute(shader, "vTexcoord", 2, 18, (void*)(sizeof(float) * 12)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->triangle_vbo); glDrawElements(GL_TRIANGLES, s->num_triangles * 3, GL_UNSIGNED_INT, (void*)0); shader_program_disable_attribute(shader, "vPosition"); shader_program_disable_attribute(shader, "vNormal"); //shader_program_disable_attribute(shader, "vTangent"); //shader_program_disable_attribute(shader, "vBinormal"); //shader_program_disable_attribute(shader, "vTexcoord"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); } shader_program_disable(shader); glDisable(GL_DEPTH_TEST); ui_render(); graphics_swap(); frame_end(); } corange_finish(); return 0; }
void ui_rectangle_render(ui_rectangle* r) { if(!r->active) { return; } int width = graphics_viewport_width(); int height = graphics_viewport_height(); asset_hndl mat = asset_hndl_new_load(P("$CORANGE/shaders/ui.mat")); shader_program* program_ui = material_get_entry(asset_hndl_ptr(&mat), 0)->program; shader_program_enable(program_ui); shader_program_set_mat4(program_ui, "world", mat4_id()); shader_program_set_mat4(program_ui, "view", mat4_id()); shader_program_set_mat4(program_ui, "proj", mat4_orthographic(0, width, height, 0, -1, 1)); glEnable(GL_BLEND); glBlendFunc(r->blend_src, r->blend_dst); if (asset_hndl_isnull(&r->texture)) { shader_program_set_texture(program_ui, "diffuse", 0, asset_hndl_new_load(P("$CORANGE/textures/white.dds"))); } else { shader_program_set_texture(program_ui, "diffuse", 0, r->texture); } shader_program_set_texture(program_ui, "random", 1, asset_hndl_new_load(P("$CORANGE/textures/random.dds"))); shader_program_set_float(program_ui, "time", r->time); shader_program_set_float(program_ui, "glitch", r->glitch); float rect_colors[] = { r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w }; float rect_positions[] = { r->top_left.x, r->top_left.y, r->top_left.x, r->bottom_right.y, r->bottom_right.x, r->bottom_right.y, r->top_left.x, r->top_left.y, r->bottom_right.x, r->top_left.y, r->bottom_right.x, r->bottom_right.y, }; float rect_texcoords[12]; if (r->texture_tile) { float width = r->bottom_right.x - r->top_left.x; float height = r->bottom_right.y - r->top_left.y; rect_texcoords[0] = 0; rect_texcoords[1] = height / r->texture_height; rect_texcoords[2] = width / r->texture_width; rect_texcoords[3] = height / r->texture_height; rect_texcoords[4] = width / r->texture_width; rect_texcoords[5] = 0; rect_texcoords[6] = 0; rect_texcoords[7] = height / r->texture_height; rect_texcoords[8] = 0; rect_texcoords[9] = 0; rect_texcoords[10] = width / r->texture_width; rect_texcoords[11] = 0; } else { rect_texcoords[0] = 0; rect_texcoords[1] = 0; rect_texcoords[2] = 1; rect_texcoords[3] = 0; rect_texcoords[4] = 1; rect_texcoords[5] = 1; rect_texcoords[6] = 0; rect_texcoords[7] = 0; rect_texcoords[8] = 0; rect_texcoords[9] = 1; rect_texcoords[10] = 1; rect_texcoords[11] = 1; } shader_program_enable_attribute(program_ui, "vPosition", 2, 2, rect_positions); shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, rect_texcoords); shader_program_enable_attribute(program_ui, "vColor", 4, 4, rect_colors); glDrawArrays(GL_TRIANGLES, 0, 6); shader_program_disable_attribute(program_ui, "vPosition"); shader_program_disable_attribute(program_ui, "vTexcoord"); shader_program_disable_attribute(program_ui, "vColor"); if (r->border_size > 0) { glLineWidth(r->border_size); float border_colors[] = { r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w }; float border_positions[] = { r->top_left.x, r->top_left.y, r->bottom_right.x, r->top_left.y, r->bottom_right.x, r->bottom_right.y, r->top_left.x, r->bottom_right.y, r->top_left.x, r->top_left.y }; float border_texcoord[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; shader_program_enable_attribute(program_ui, "vPosition", 2, 2, border_positions); shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, border_texcoord); shader_program_enable_attribute(program_ui, "vColor", 4, 4, border_colors); glDrawArrays(GL_LINE_STRIP, 0, 5); shader_program_disable_attribute(program_ui, "vPosition"); shader_program_disable_attribute(program_ui, "vTexcoord"); shader_program_disable_attribute(program_ui, "vColor"); glLineWidth(1); } glDisable(GL_BLEND); shader_program_disable(program_ui); }
material* mat_load_file(char* filename) { SDL_RWops* file = SDL_RWFromFile(filename, "r"); if(file == NULL) { error("Cannot load file %s", filename); } material* m = material_new(); material_entry* me = material_add_entry(m); char line[1024]; while(SDL_RWreadline(file, line, 1024)) { if (line[0] == '#') { continue; } if (line[0] == '\r') { continue; } if (line[0] == '\n') { continue; } if (strstr(line, "submaterial")) { /* Skip Empty Submaterials */ if (me->num_items == 0) { continue; } else { me = material_add_entry(m); continue; } } char type[512]; char name[512]; char value[512]; int matches = sscanf(line, "%511s %511s = %511s", type, name, value); if (matches != 3) continue; material_item mi; int type_id; char* end; float f0, f1, f2, f3; if (strcmp(type, "shader") == 0) { mi.as_asset = asset_hndl_new_load(P(value)); type_id = mat_item_shader; } else if (strcmp(type, "texture") == 0) { mi.as_asset = asset_hndl_new_load(P(value)); type_id = mat_item_texture; } else if (strcmp(type, "int") == 0) { mi.as_int = atoi(value); type_id = mat_item_int; } else if (strcmp(type, "float") == 0) { mi.as_float = atof(value); type_id = mat_item_float; } else if (strcmp(type, "vec2") == 0) { f0 = strtod(value, &end); f1 = strtod(end, NULL); mi.as_vec2 = vec2_new(f0, f1); type_id = mat_item_vec2; } else if (strcmp(type, "vec3") == 0) { f0 = strtod(value, &end); f1 = strtod(end, &end); f2 = strtod(end, NULL); mi.as_vec3 = vec3_new(f0, f1, f2); type_id = mat_item_vec3; } else if (strcmp(type, "vec4") == 0) { f0 = strtod(value, &end); f1 = strtod(end, &end); f2 = strtod(end, &end); f3 = strtod(end, NULL); mi.as_vec4 = vec4_new(f0, f1, f2, f3); type_id = mat_item_vec4; } else { error("Unknown material item type '%s'", type); return NULL; } material_entry_add_item(me, name, type_id, mi); } SDL_RWclose(file); material_generate_programs(m); SDL_GL_CheckError(); return m; }
asset* asset_get_load(fpath path) { asset_hndl ah = asset_hndl_new_load(path); return asset_hndl_ptr(&ah); }
void sea_init() { graphics_viewport_set_dimensions(1280, 720); graphics_viewport_set_title("Sea"); camera* cam = entity_new("camera", camera); cam->position = vec3_new(50.0, 50.0, 50.0); cam->target = vec3_new(0, 5, 0); cam->near_clip = 0.1; light* sun = entity_new("sun", light); light_set_type(sun, light_type_spot); sun->position = vec3_new(20,23,16); sun->ambient_color = vec3_new(0.5, 0.5, 0.5); sun->diffuse_color = vec3_new(1.0, 0.894, 0.811); sun->specular_color = vec3_mul(vec3_new(1.0, 0.894, 0.811), 4); sun->power = 5; light* backlight = entity_new("backlight", light); light_set_type(backlight, light_type_point); backlight->position = vec3_new(-22,10,-13); backlight->ambient_color = vec3_new(0.2, 0.2, 0.2); backlight->diffuse_color = vec3_new(0.729, 0.729, 1.0); backlight->specular_color = vec3_mul(vec3_new(0.729, 0.729, 1.0), 1); backlight->power = 2; shadow_mapper_init(sun); forward_renderer_init(); forward_renderer_set_camera(cam); forward_renderer_set_shadow_light(sun); forward_renderer_set_shadow_texture( shadow_mapper_depth_texture() ); forward_renderer_add_light(sun); forward_renderer_add_light(backlight); folder_load(P("./resources/")); renderable* r_seaplane = asset_get(P("./resources/seaplane.obj")); r_seaplane->material = asset_hndl_new(P("./resources/seaplane.mat")); static_object* s_seaplane = entity_new("seaplane", static_object); s_seaplane->renderable = asset_hndl_new_ptr(r_seaplane); s_seaplane->scale = vec3_new(3,1,3); renderable* r_skydome = asset_get(P("./resources/skydome.obj")); r_skydome->material = asset_hndl_new_load(P("$CORANGE/shaders/skydome.mat")); static_object* skydome = entity_new("skydome", static_object); skydome->renderable = asset_hndl_new_ptr(r_skydome); skydome->position = vec3_new(0, -512, 0); skydome->scale = vec3_new(1024, 1024, 1024); folder_load(P("./resources/corvette/")); renderable* r_corvette = asset_get(P("./resources/corvette/corvette.obj")); r_corvette->material = asset_hndl_new_load(P("./resources/corvette/corvette.mat")); static_object* s_corvette = entity_new("corvette", static_object); s_corvette->renderable = asset_hndl_new_ptr(r_corvette); //s_corvette->collision_body = collision_body_new_mesh(asset_get("./resources/corvette/corvette.col")); s_corvette->scale = vec3_new(1.5, 1.5, 1.5); s_corvette->position = vec3_new(0, 0.5, 0); static_object* center_sphere = entity_new("center_sphere", static_object); center_sphere->position = vec3_new(0, 5, 0); //center_sphere->renderable = asset_get("./resources/ball.obj"); //center_sphere->collision_body = collision_body_new_sphere(sphere_new(v3_zero(), 1.0f)); ui_button* framerate = ui_elem_new("framerate", ui_button); ui_button_move(framerate, vec2_new(10,10)); ui_button_resize(framerate, vec2_new(30,25)); ui_button_set_label(framerate, "FRAMERATE"); ui_button_disable(framerate); }