void text_renderer_new(text_renderer_p renderer, size_t texture_width, size_t texture_height) { FT_Error error = FT_Init_FreeType(&renderer->freetype); if (error) { printf("FT_Init_FreeType error\n"); return; } renderer->texture = texture_new(texture_width, texture_height, GL_R8); size_t black_data_size = texture_width * texture_height; void* black_data = malloc(black_data_size); memset(black_data, 0, black_data_size); texture_update(renderer->texture, GL_RED, black_data); free(black_data); glBindTexture(GL_TEXTURE_RECTANGLE, renderer->texture); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_RECTANGLE, 0); renderer->fonts = hash_of(text_renderer_font_t); renderer->lines = array_of(text_renderer_line_t); }
texture* bmp_load_file( char* filename ) { image* i = image_bmp_load_file(filename); texture* t = texture_new(); glBindTexture(GL_TEXTURE_2D, texture_handle(t)); glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0 ); texture_set_image(t, i); texture_set_filtering_anisotropic(t); image_delete(i); return t; }
texture* lut_load_file( char* filename ) { SDL_RWops* file = SDL_RWFromFile(filename, "r"); if(file == NULL) { error("Cannot load file %s", filename); } long size = SDL_RWseek(file,0,SEEK_END); unsigned char* contents = malloc(size+1); contents[size] = '\0'; SDL_RWseek(file, 0, SEEK_SET); SDL_RWread(file, contents, size, 1); SDL_RWclose(file); int head = sizeof("CORANGE-LUT")-1; int lut_size = (unsigned char)contents[head] | (unsigned char)contents[head + 1]; int offset = head + 3; texture* t = texture_new(); t->type = GL_TEXTURE_3D; glBindTexture(t->type, texture_handle(t)); glTexImage3D(t->type, 0, GL_RGB, lut_size, lut_size, lut_size, 0, GL_RGB, GL_UNSIGNED_BYTE, contents + offset); glTexParameteri(t->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(t->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(t->type, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(t->type, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexParameteri(t->type, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); free(contents); return t; }
void board_init(const GLuint aWidth, const GLuint aKnightRow, const GLuint aKnightCol) { board__width = aWidth; // number of cycles it takes to finish the computation: // // (3 cycles per knight) * (number of knights) // // the -1 is because the initial knight is not determined // at runtime, so we save one cycle of processing. // // see EXPORT_DELAY in the "naive.cg.erb" file for details. // board__numCycles = 3 * board__area; printf("\nThe tour should take %lu cycles to complete.\n", board__numCycles); // generate the board data if((board__data = (float*)malloc(4*board__area*sizeof(float))) == NULL) { printf("\n*** out of memory allocating board__data ***\n"); exit(2); } unsigned addr = 0; for(unsigned row = 0; row < board__width; row++) { for (unsigned col = 0; col < board__width; col++) { // red channel board__data[addr++] = ( // set initial position of knight row == aKnightRow && col == aKnightCol ) ? 1 : 0; // green channel board__data[addr++] = 0; // blue channel board__data[addr++] = 0; // alpha channel board__data[addr++] = 8; // 8 is MOVE_NONE (see naive.cg.erb for details); all cells should have next move = NONE initially, because only the knight makes the "next move" desicison at *runtime* } } // Generate, set up, and bind the texture texture_new(&board__computeTexture, board__width, board__width, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA32F_ARB, NULL); // initialize the FBO fbo_init(board__width, board__width); // Attach the texture to the framebuffer object glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, board__computeTexture, 0); // Check the FBO for completeness fbo_check(); // generate texture for board display texture_new(&board__displayTexture, board__width, board__width, GL_TEXTURE_2D, GL_RGBA, NULL); // generate OpenGL display lists to speed up rendering board__gridDisplayList = glGenLists(1); glNewList(board__gridDisplayList, GL_COMPILE); // draw board outline (grid) float step = 2.0 / board__width; float cell; glBegin(GL_LINES); // draw rows for (cell = -1 + step; cell < board__width; cell += step) { glVertex3f(-1, cell, -1); glVertex3f(1, cell, -1); } // draw columns for (cell = -1 + step; cell < board__width; cell += step) { glVertex3f(cell, -1, -1); glVertex3f(cell, 1, -1); } glEnd(); glEndList(); }
texture* dds_load_file( char* filename ) { DdsLoadInfo loadInfoDXT1 = { true, false, false, 4, 8, GL_COMPRESSED_RGBA_S3TC_DXT1 }; DdsLoadInfo loadInfoDXT3 = { true, false, false, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT3 }; DdsLoadInfo loadInfoDXT5 = { true, false, false, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT5 }; DdsLoadInfo loadInfoBGRA8 = { false, false, false, 1, 4, GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE }; DdsLoadInfo loadInfoBGR8 = { false, false, false, 1, 3, GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE }; DdsLoadInfo loadInfoBGR5A1 = { false, true, false, 1, 2, GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV }; DdsLoadInfo loadInfoBGR565 = { false, true, false, 1, 2, GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 }; DdsLoadInfo loadInfoIndex8 = { false, false, true, 1, 1, GL_RGB8, GL_BGRA, GL_UNSIGNED_BYTE }; SDL_RWops* f = SDL_RWFromFile(filename, "rb"); if (f == NULL) { error("Cannot load file %s", filename); } DDS_header hdr; SDL_RWread(f, &hdr, 1, sizeof(DDS_header)); if( hdr.dwMagic != DDS_MAGIC || hdr.dwSize != 124 || !(hdr.dwFlags & DDSD_PIXELFORMAT) || !(hdr.dwFlags & DDSD_CAPS) ) { error("Cannot Load File %s: Does not appear to be a .dds file.\n", filename); } int x = hdr.dwWidth; int y = hdr.dwHeight; int mip_map_num = (hdr.dwFlags & DDSD_MIPMAPCOUNT) ? hdr.dwMipMapCount : 1; if (!is_power_of_two(x)) { error("Texture %s with is %i pixels which is not a power of two!", filename, x); } if (!is_power_of_two(y)) { error("Texture %s height is %i pixels which is not a power of two!", filename, y); } DdsLoadInfo* li = &loadInfoDXT1; if (PF_IS_DXT1(hdr.sPixelFormat )) { li = &loadInfoDXT1; } else if (PF_IS_DXT3(hdr.sPixelFormat )) { li = &loadInfoDXT3; } else if (PF_IS_DXT5(hdr.sPixelFormat )) { li = &loadInfoDXT5; } else if (PF_IS_BGRA8(hdr.sPixelFormat )) { li = &loadInfoBGRA8; } else if (PF_IS_BGR8(hdr.sPixelFormat )) { li = &loadInfoBGR8; } else if (PF_IS_BGR5A1(hdr.sPixelFormat)) { li = &loadInfoBGR5A1; } else if (PF_IS_BGR565(hdr.sPixelFormat)) { li = &loadInfoBGR565; } else if (PF_IS_INDEX8(hdr.sPixelFormat)) { li = &loadInfoIndex8; } else { error("Cannot Load File %s: Unknown DDS File format type.", filename); } texture* t = texture_new(); if (hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP) { t->type = GL_TEXTURE_CUBE_MAP; glBindTexture(GL_TEXTURE_CUBE_MAP, texture_handle(t)); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_FALSE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mip_map_num-1); } else { t->type = GL_TEXTURE_2D; glBindTexture(GL_TEXTURE_2D, texture_handle(t)); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mip_map_num-1); texture_set_filtering_anisotropic(t); } for (int i = 0; i < (t->type == GL_TEXTURE_CUBE_MAP ? 6 : 1); i++) { GLenum target = t->type; if (t->type == GL_TEXTURE_CUBE_MAP) { target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i; } int x = hdr.dwWidth; int y = hdr.dwHeight; int mip_map_num = (hdr.dwFlags & DDSD_MIPMAPCOUNT) ? hdr.dwMipMapCount : 1; if ( li->compressed ) { size_t size = max(li->div_size, x) / li->div_size * max(li->div_size, y) / li->div_size * li->block_bytes; char* data = malloc(size); for(int ix = 0; ix < mip_map_num; ix++) { SDL_RWread(f, data, 1, size); glCompressedTexImage2D(target, ix, li->internal_format, x, y, 0, size, data); x = (x+1)>>1; y = (y+1)>>1; size = max(li->div_size, x) / li->div_size * max(li->div_size, y) / li->div_size * li->block_bytes; } free(data); } else if ( li->palette ) {