void proj_tests(void){ mat4_t m = mat4_look_at(vec4_new(0, 0, 5, 0), vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0)); printf("Look at matrix:\n"); mat4_print(m); m = mat4_ortho(-1, 1, -1, 1, 1, 100); printf("ortho matrix:\n"); mat4_print(m); m = mat4_perspective(90, 1, 1, 100); printf("perspective:\n"); mat4_print(m); }
static void save_noise_to_file(ui_button* b) { ui_spinner* save_spinner = ui_elem_get("save_spinner"); save_spinner->color = vec4_new(1,1,1,1); save_thread = SDL_CreateThread(save_noise_to_file_thread, NULL); }
static int save_noise_to_file_thread(void* unused) { image* noise = perlin_noise_generate(512, 512, 8); image_tga_save_file(noise, "./perlin_noise.tga"); debug("Noise saved as perlin_noise.tga"); image_delete(noise); ui_spinner* save_spinner = ui_elem_get("save_spinner"); ui_rectangle* spinner_box = ui_elem_get("spinner_box"); save_spinner->color = vec4_new(1,1,1,0); spinner_box->color = vec4_new(0,0,0,0); spinner_box->border_color = vec4_new(1,1,1,0); currently_saving = false; return 0; }
void basic_test(void){ float ALIGN_16 a_rows[16] = { 1, 5, 0, 0, 2, 1, 3, 5, 6, 9, 0, 2, 5, 3, 8, 9 }; float ALIGN_16 b_rows[16] = { 4, 0, 2, 0, 1, 2, 7, 1, 0, 0, 2, 0, 1, 2, 0, 1 }; mat4_t a = mat4_from_rows(a_rows); mat4_t b = mat4_from_rows(b_rows); printf("Multiplying a:\n"); mat4_print(a); printf("With b:\n"); mat4_print(b); printf("Multiplication result:\n"); a = mat4_mult(a, b); mat4_print(a); vec4_t v = vec4_new(1, 2, 3, 1); a = mat4_translate(v); printf("translation matrix for [1, 2, 3]:\n"); mat4_print(a); printf("Translated vector:\n"); v = mat4_vec_mult(a, v); vec4_print(v); a = mat4_rotate(90, vec4_new(1, 0, 0, 0)); printf("Rotation matrix:\n"); mat4_print(a); v = mat4_vec_mult(a, vec4_new(0, 1, 0, 0)); printf("+Y vec rotated 90 deg about +X:\n"); vec4_print(v); }
vec4 image_get(image* i, int u, int v) { u = image_wrap(u, i->width, i->repeat_type); v = image_wrap(v, i->height, i->repeat_type); if (u == -1) { return vec4_zero(); } if (v == -1) { return vec4_zero(); } float r = (float)i->data[u * 4 + v * i->width * 4 + 0] / 255; float g = (float)i->data[u * 4 + v * i->width * 4 + 1] / 255; float b = (float)i->data[u * 4 + v * i->width * 4 + 2] / 255; float a = (float)i->data[u * 4 + v * i->width * 4 + 3] / 255; return vec4_new(r, g, b, a); }
void marching_cubes_init() { const int full_size = width * height * depth; /* Point rendering data */ vec4* point_data = malloc(sizeof(vec4) * full_size); if(point_data == NULL) { error("Not enough memory!"); } int x, y, z; for(x = 0; x < width; x++) for(y = 0; y < height; y++) for(z = 0; z < depth; z++) { int id = x + y * width + z * width * height; vec4 position = vec4_new(x, y, z, 1); point_data[id] = position; } glGenBuffers(1, &point_positions); glBindBuffer(GL_ARRAY_BUFFER, point_positions); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * full_size, point_data, GL_STATIC_DRAW); free(point_data); vec4* point_color_data = malloc(sizeof(vec4) * full_size); memset(point_color_data, 0, sizeof(vec4) * full_size); glGenBuffers(1, &point_colors); glBindBuffer(GL_ARRAY_BUFFER, point_colors); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * full_size, point_color_data, GL_DYNAMIC_COPY); free(point_color_data); point_color_buffer = kernel_memory_from_glbuffer(point_colors); /* OpenCL volume */ volume = kernel_memory_allocate(sizeof(float) * full_size); /* Vertex stuff */ vec4* vertex_pos_data = malloc(sizeof(vec4) * MAX_VERTS); memset(vertex_pos_data, 0, sizeof(vec4) * MAX_VERTS); glGenBuffers(1, &vertex_positions); glBindBuffer(GL_ARRAY_BUFFER, vertex_positions); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * MAX_VERTS, vertex_pos_data, GL_DYNAMIC_COPY); free(vertex_pos_data); vertex_positions_buffer = kernel_memory_from_glbuffer(vertex_positions); vec4* vertex_norm_data = malloc(sizeof(vec4) * MAX_VERTS); memset(vertex_norm_data, 0, sizeof(vec4) * MAX_VERTS); glGenBuffers(1, &vertex_normals); glBindBuffer(GL_ARRAY_BUFFER, vertex_normals); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * MAX_VERTS, vertex_norm_data, GL_DYNAMIC_COPY); free(vertex_norm_data); vertex_normals_buffer = kernel_memory_from_glbuffer(vertex_normals); vertex_index = kernel_memory_allocate(sizeof(int)); /* Kernels */ kernel_program* marching_cubes = asset_get(P("./kernels/marching_cubes.cl")); write_point = kernel_program_get_kernel(marching_cubes, "write_point"); kernel_set_argument(write_point, 0, sizeof(kernel_memory), &volume); kernel_set_argument(write_point, 4, sizeof(int), (void*)&width); kernel_set_argument(write_point, 5, sizeof(int), (void*)&height); kernel_set_argument(write_point, 6, sizeof(int), (void*)&depth); write_metaball = kernel_program_get_kernel(marching_cubes, "write_metaball"); kernel_set_argument(write_metaball, 0, sizeof(kernel_memory), &volume); write_metaballs = kernel_program_get_kernel(marching_cubes, "write_metaballs"); kernel_set_argument(write_metaballs, 0, sizeof(kernel_memory), &volume); write_clear = kernel_program_get_kernel(marching_cubes, "write_clear"); kernel_set_argument(write_clear, 0, sizeof(kernel_memory), &volume); write_point_color_back = kernel_program_get_kernel(marching_cubes, "write_point_color_back"); kernel_set_argument(write_point_color_back, 0, sizeof(kernel_memory), &volume); kernel_set_argument(write_point_color_back, 1, sizeof(kernel_memory), &point_color_buffer); construct_surface = kernel_program_get_kernel(marching_cubes, "construct_surface"); kernel_set_argument(construct_surface, 0, sizeof(kernel_memory), &volume); generate_normals = kernel_program_get_kernel(marching_cubes, "generate_flat_normals"); generate_normals_smooth = kernel_program_get_kernel(marching_cubes, "generate_smooth_normals"); }
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_size(1280, 720); graphics_viewport_set_title("Noise"); folder_load(P("./")); file_load(P("$CORANGE/textures/random.dds")); glClearColor(1.0, 0.0, 0.0, 1.0); ui_button* info_button = ui_elem_new("info_button", ui_button); ui_button_move(info_button, vec2_new(10, 10)); ui_button_resize(info_button, vec2_new(460,25)); ui_button_set_label(info_button, "Procedural texture from perlin noise and feedback functions"); ui_button* save_button = ui_elem_new("save_button", ui_button); ui_button_move(save_button, vec2_new(480, 10)); ui_button_resize(save_button, vec2_new(380,25)); ui_button_set_label(save_button, "Click Here to save tileable perlin noise to file"); ui_button_set_onclick(save_button, save_noise_to_file); ui_button* spinner_box = ui_elem_new("spinner_box", ui_button); ui_button_resize(spinner_box, vec2_new(32, 32)); ui_button_move(spinner_box, vec2_new(870, 7)); ui_button_set_label(spinner_box, ""); ui_spinner* save_spinner = ui_elem_new("save_spinner", ui_spinner); save_spinner->color = vec4_new(1,1,1,0); save_spinner->top_left = vec2_new(874, 11); save_spinner->bottom_right = vec2_add(save_spinner->top_left, vec2_new(24,24)); srand(time(NULL)); shader_time = (float)rand() / (RAND_MAX / 1000); bool running = true; while(running) { frame_begin(); SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) { running = 0; } if (event.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); } break; case SDL_QUIT: running = 0; break; break; } ui_event(event); } shader_time += frame_time(); ui_update(); noise_render(); ui_render(); graphics_swap(); frame_end(); } SDL_WaitThread(save_thread, NULL); corange_finish(); return 0; }
int main(int argc, char **argv){ /* vertices for a triangle */ /* Why does triangle[] = { vec4_new(...) } result in a segfault when returning * from _mm_load_ps? */ /* Just want a sort of 3D object, but not a closed object otherwise it's hard * to tell what's going on w/ flat shading */ vec4_t object[18]; //+Z face object[0] = vec4_new(-1, -1, 1, 1); object[1] = vec4_new(1, -1, 1, 1); object[2] = vec4_new(1, 1, 1, 1); object[3] = vec4_new(1, 1, 1, 1); object[4] = vec4_new(-1, 1, 1, 1); object[5] = vec4_new(-1, -1, 1, 1); //+X face object[6] = vec4_new(1, -1, 1, 1); object[7] = vec4_new(1, -1, -1, 1); object[8] = vec4_new(1, 1, -1, 1); object[9] = vec4_new(1, 1, -1, 1); object[10] = vec4_new(1, 1, 1, 1); object[11] = vec4_new(1, -1, 1, 1); //-X face object[12] = vec4_new(-1, -1, 1, 1); object[13] = vec4_new(-1, -1, -1, 1); object[14] = vec4_new(-1, 1, -1, 1); object[15] = vec4_new(-1, 1, -1, 1); object[16] = vec4_new(-1, 1, 1, 1); object[17] = vec4_new(-1, -1, 1, 1); if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ fprintf(stderr, "SDL Init error: %s\n", SDL_GetError()); return 1; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); #ifdef DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); #endif SDL_Window *win = SDL_CreateWindow("SSE GL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL); SDL_GLContext context = SDL_GL_CreateContext(win); if (check_GL_error("Opened win + context")){ SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 1; } glewExperimental = GL_TRUE; GLenum err = glewInit(); if (err != GLEW_OK){ fprintf(stderr, "GLEW init error %d\n", err); SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 1; } check_GL_error("Post GLEW init"); #ifdef DEBUG glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); glDebugMessageCallbackARB(gl_debug_callback, NULL); glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); #endif glClearColor(0, 0, 0, 1); glClearDepth(1); glEnable(GL_DEPTH_TEST); //Model's vao and vbo GLuint model[2]; glGenVertexArrays(1, model); glBindVertexArray(model[0]); glGenBuffers(1, model + 1); glBindBuffer(GL_ARRAY_BUFFER, model[1]); glBufferData(GL_ARRAY_BUFFER, 4 * 6 * 3 * sizeof(GLfloat), object, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); if (check_GL_error("Setup buffers")){ return 1; } GLint vshader = make_shader(vert_shader_src, GL_VERTEX_SHADER); GLint fshader = make_shader(frag_shader_src, GL_FRAGMENT_SHADER); if (vshader == -1 || fshader == -1){ return 1; } GLint program = make_program(vshader, fshader); if (program == -1){ return 1; } glDeleteShader(vshader); glDeleteShader(fshader); mat4_t model_mat = mat4_mult(mat4_rotate(45, vec4_new(1, 1, 0, 0)), mat4_scale(2, 2, 2)); model_mat = mat4_mult(mat4_translate(vec4_new(0, 2, -5, 1)), model_mat); mat4_t view_mat = mat4_look_at(vec4_new(0, 0, 5, 0), vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0)); mat4_t proj_mat = mat4_perspective(75, ((float)WIN_WIDTH) / WIN_HEIGHT, 1, 100); glUseProgram(program); GLuint model_unif = glGetUniformLocation(program, "model"); GLuint view_unif = glGetUniformLocation(program, "view"); GLuint proj_unif = glGetUniformLocation(program, "proj"); glUniformMatrix4fv(model_unif, 1, GL_FALSE, (GLfloat*)&model_mat); glUniformMatrix4fv(view_unif, 1, GL_FALSE, (GLfloat*)&view_mat); glUniformMatrix4fv(proj_unif, 1, GL_FALSE, (GLfloat*)&proj_mat); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 18); SDL_GL_SwapWindow(win); check_GL_error("Post Draw"); SDL_Event e; int quit = 0; while (!quit){ while (SDL_PollEvent(&e)){ if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN){ quit = 1; } } } glDeleteProgram(program); glDeleteVertexArrays(1, model); glDeleteBuffers(1, model + 1); SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 0; }
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; }
void particles_init() { particle_positions = malloc(sizeof(vec4) * particle_count); particle_velocities = malloc(sizeof(vec4) * particle_count); particle_lifetimes = malloc(sizeof(float) * particle_count); particle_randoms = malloc(sizeof(vec4) * particle_count); srand(time(NULL)); for(int i = 0; i < particle_count; i++) { particle_lifetimes[i] = 999; particle_positions[i] = vec4_new(0,0,0,0); particle_velocities[i] = vec4_new(0,0,0,0); float rx = ((float)rand() / RAND_MAX) * 2 - 1; float ry = ((float)rand() / RAND_MAX) * 2 + 0.5; float rz = ((float)rand() / RAND_MAX) * 2 - 1; float rm = (float)rand() / RAND_MAX; vec3 rand = vec3_mul(vec3_normalize(vec3_new(rx, ry, rz)), rm * 2); particle_randoms[i] = vec4_new(rand.x, rand.y, rand.z, 0); } glGenBuffers(1, &positions_buffer); glBindBuffer(GL_ARRAY_BUFFER, positions_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * particle_count, particle_positions, GL_DYNAMIC_COPY); glGenBuffers(1, &velocities_buffer); glBindBuffer(GL_ARRAY_BUFFER, velocities_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * particle_count, particle_velocities, GL_DYNAMIC_COPY); glGenBuffers(1, &lifetimes_buffer); glBindBuffer(GL_ARRAY_BUFFER, lifetimes_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * particle_count, particle_lifetimes, GL_DYNAMIC_COPY); glGenBuffers(1, &randoms_buffer); glBindBuffer(GL_ARRAY_BUFFER, randoms_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vec4) * particle_count, particle_randoms, GL_DYNAMIC_COPY); #ifdef OPEN_GL_CPU #ifndef CPU_ONLY k_particle_positions = kernel_memory_allocate(sizeof(vec4) * particle_count); k_particle_velocities = kernel_memory_allocate(sizeof(vec4) * particle_count); k_particle_lifetimes = kernel_memory_allocate(sizeof(float) * particle_count); k_particle_randoms = kernel_memory_allocate(sizeof(vec4) * particle_count); kernel_memory_write(k_particle_positions, sizeof(vec4) * particle_count, particle_positions); kernel_memory_write(k_particle_velocities, sizeof(vec4) * particle_count, particle_velocities); kernel_memory_write(k_particle_lifetimes, sizeof(float) * particle_count, particle_lifetimes); kernel_memory_write(k_particle_randoms, sizeof(vec4) * particle_count, particle_randoms); #endif #else k_particle_positions = kernel_memory_from_glbuffer(positions_buffer); k_particle_velocities = kernel_memory_from_glbuffer(velocities_buffer); k_particle_lifetimes = kernel_memory_from_glbuffer(lifetimes_buffer); k_particle_randoms = kernel_memory_from_glbuffer(randoms_buffer); #endif kernel_program* program = asset_get(P("./kernels/particles.cl")); float max_life = 60.0; float min_velocity = 0.5; #ifndef CPU_ONLY k_update = kernel_program_get_kernel(program, "particle_update"); kernel_set_argument(k_update, 0, sizeof(kernel_memory), &k_particle_positions); kernel_set_argument(k_update, 1, sizeof(kernel_memory), &k_particle_velocities); kernel_set_argument(k_update, 2, sizeof(kernel_memory), &k_particle_lifetimes); kernel_set_argument(k_update, 3, sizeof(kernel_memory), &k_particle_randoms); kernel_set_argument(k_update, 4, sizeof(cl_float), &max_life); kernel_set_argument(k_update, 5, sizeof(cl_float), &min_velocity); kernel_set_argument(k_update, 9, sizeof(cl_int), &particle_count); #endif }