Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/resourcemanager.cpp
00001 /*
00002  * resourcemanager.cpp - Kukatz 3D
00003  * Copyright (c) 2010, 2011, 2012 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include <iostream>
00026 #include <fstream>
00027 #include <cstring> // for memcpy
00028 
00029 #include "opengl.hpp"
00030 
00031 #include <SFML/System/Randomizer.hpp>
00032 
00033 #include "snappy.h"
00034 
00035 #include "resourcemanager.hpp"
00036 #include "configmanager.hpp"
00037 
00038 ResourceManager::ResourceManager()
00039 {
00040         std::ifstream res_file("resources.pak", std::ios::binary | std::ios::in);
00041         
00042         if (!res_file.is_open())
00043         {
00044                 std::cerr << "ERROR! Couldn't open the resources file.\n";
00045         }
00046         else
00047         {
00048                 sf::Uint32 uncompressed_size;
00049                 res_file.read((char*)(&uncompressed_size), 4);
00050                 
00051                 res_file.seekg(0, std::ios::end);
00052                 size_t file_length = res_file.tellg();
00053                 res_file.seekg(4, std::ios::beg);
00054                 
00055                 unsigned char* source_buffer = new unsigned char[file_length - 4];
00056                 
00057                 res_file.read((char*)source_buffer, file_length - 4);
00058                 res_file.close();
00059                 
00060                 unsigned char* resources = new unsigned char[uncompressed_size];
00061                 
00062                 if (!snappy::RawUncompress((const char*)source_buffer, file_length - 4, (char*)resources))
00063                 {
00064                         delete[] source_buffer;
00065                         std::cerr << "ERROR! Couldn't decompress the resources.\n";
00066                 }
00067                 else
00068                 {
00069                         delete[] source_buffer;
00070                 
00071                         unsigned char* curr_res_pos = resources;
00072                         
00073                         //  -------- MESH LIST  --------
00074                         
00075                         sf::Uint16 number_of_meshes = *((sf::Uint16*)curr_res_pos);
00076                         curr_res_pos += 2;
00077                         
00078                         char* id_buffer = new char[256]; // max 255 length + zero
00079                         
00080                         for (size_t i = 0; i < number_of_meshes; ++i)
00081                         {
00082                                 sf::Uint8 mesh_id_length = *((sf::Uint8*)curr_res_pos);
00083                                 curr_res_pos += 1;
00084                                 
00085                                 memcpy(id_buffer, curr_res_pos, mesh_id_length);
00086                                 id_buffer[mesh_id_length] = 0; // we must terminate it ;)
00087                                 std::string mesh_id(id_buffer);
00088                                 curr_res_pos += mesh_id_length;
00089                                 
00090                                 sf::Uint32 first_index = *((sf::Uint32*)curr_res_pos);
00091                                 curr_res_pos += 4;
00092                                 
00093                                 sf::Uint16 num_indices = *((sf::Uint16*)curr_res_pos);
00094                                 curr_res_pos += 2;
00095                                 
00096                                 sf::Uint32 vertex_offset = *((sf::Uint32*)curr_res_pos);
00097                                 curr_res_pos += 4;
00098                                 
00099                                 sf::Vector3f center;
00100                                 
00101                                 center.x = *((float*)curr_res_pos);
00102                                 curr_res_pos += 4;
00103                                 center.y = *((float*)curr_res_pos);
00104                                 curr_res_pos += 4;
00105                                 center.z = *((float*)curr_res_pos);
00106                                 curr_res_pos += 4;
00107                                 
00108                                 float radius;
00109                                 
00110                                 radius = *((float*)curr_res_pos);
00111                                 curr_res_pos += 4;
00112                                 
00113                                 Mesh* mesh_ptr = new Mesh(mesh_id, first_index, num_indices,
00114                                         vertex_offset, center, radius);
00115                                 
00116                                 meshes.insert(std::make_pair(mesh_id, mesh_ptr));
00117                         }
00118                         
00119                         //  -------- VBO, IBO  --------
00120                         
00121                         vbo_size = *((sf::Uint32*)curr_res_pos);
00122                         curr_res_pos += 4;
00123                         
00124                         vbo_data = new char[vbo_size];
00125                         memcpy(vbo_data, curr_res_pos, vbo_size);
00126                         curr_res_pos += vbo_size;
00127                         
00128                         ibo_size = *((sf::Uint32*)curr_res_pos);
00129                         curr_res_pos += 4;
00130                         
00131                         ibo_data = new char[ibo_size];
00132                         memcpy(ibo_data, curr_res_pos, ibo_size);
00133                         curr_res_pos += ibo_size;
00134                         
00135                         //  -------- ANIMATIONS  --------
00136                         
00137                         sf::Uint16 number_of_animations = *((sf::Uint16*)curr_res_pos);
00138                         curr_res_pos += 2;
00139                         
00140                         for (size_t i = 0; i < number_of_animations; ++i)
00141                         {
00142                                 sf::Uint8 animation_id_length = *((sf::Uint8*)curr_res_pos);
00143                                 curr_res_pos += 1;
00144                                 
00145                                 memcpy(id_buffer, curr_res_pos, animation_id_length);
00146                                 id_buffer[animation_id_length] = 0; // we must terminate it ;)
00147                                 std::string animation_id(id_buffer);
00148                                 curr_res_pos += animation_id_length;
00149                                 
00150                                 sf::Uint8 num_meshes = *((sf::Uint8*)curr_res_pos);
00151                                 curr_res_pos += 1;
00152                                 
00153                                 std::vector< std::pair< float, std::string> > meshes;
00154                                 
00155                                 for (size_t j = 0; j < num_meshes; ++j)
00156                                 {
00157                                         float mesh_time = *((float*)curr_res_pos);
00158                                         curr_res_pos += 4;
00159                                         
00160                                         sf::Uint8 mesh_id_length = *((sf::Uint8*)curr_res_pos);
00161                                         curr_res_pos += 1;
00162                                         
00163                                         memcpy(id_buffer, curr_res_pos, mesh_id_length);
00164                                         id_buffer[mesh_id_length] = 0; // we must terminate it ;)
00165                                         std::string mesh_id(id_buffer);
00166                                         curr_res_pos += mesh_id_length;
00167                                         
00168                                         meshes.push_back(std::make_pair< float, std::string >(mesh_time, mesh_id));
00169                                 }
00170                                 
00171                                 Animation* animation_ptr = new Animation(animation_id, meshes);
00172                                 animations.insert(std::make_pair(animation_id, animation_ptr));
00173                         }
00174                         
00175                         //  -------- TEXTURES  --------
00176                         
00177                         sf::Uint16 number_of_textures = *((sf::Uint16*)curr_res_pos);
00178                         curr_res_pos += 2;
00179                         
00180                         for (size_t i = 0; i < number_of_textures; ++i)
00181                         {
00182                                 sf::Uint8 texture_id_length = *((sf::Uint8*)curr_res_pos);
00183                                 curr_res_pos += 1;
00184                                 
00185                                 memcpy(id_buffer, curr_res_pos, texture_id_length);
00186                                 id_buffer[texture_id_length] = 0; // we must terminate it ;)
00187                                 std::string texture_id(id_buffer);
00188                                 curr_res_pos += texture_id_length;
00189                                 
00190                                 bool rgba = ((*((sf::Uint8*)curr_res_pos)) != 0);
00191                                 curr_res_pos += 1;
00192                                 
00193                                 sf::Uint32 texture_size = *((sf::Uint32*)curr_res_pos);
00194                                 curr_res_pos += 4;
00195                                 
00196                                 Texture* texture_ptr = new Texture(texture_id, curr_res_pos, texture_size, rgba);
00197                                 textures.insert(std::make_pair(texture_id, texture_ptr));
00198                                 
00199                                 curr_res_pos += texture_size;
00200                         }
00201                         
00202                         //  -------- FONTS  --------
00203                         
00204                         sf::Uint16 number_of_fonts = *((sf::Uint16*)curr_res_pos);
00205                         curr_res_pos += 2;
00206                         
00207                         for (size_t i = 0; i < number_of_fonts; ++i)
00208                         {
00209                                 sf::Uint8 font_id_length = *((sf::Uint8*)curr_res_pos);
00210                                 curr_res_pos += 1;
00211                                 
00212                                 memcpy(id_buffer, curr_res_pos, font_id_length);
00213                                 id_buffer[font_id_length] = 0; // we must terminate it ;)
00214                                 std::string font_id(id_buffer);
00215                                 curr_res_pos += font_id_length;
00216                                 
00217                                 sf::Uint8 fontmap_id_length = *((sf::Uint8*)curr_res_pos);
00218                                 curr_res_pos += 1;
00219                                 
00220                                 memcpy(id_buffer, curr_res_pos, fontmap_id_length);
00221                                 id_buffer[fontmap_id_length] = 0; // we must terminate it ;)
00222                                 std::string fontmap_id(id_buffer);
00223                                 curr_res_pos += fontmap_id_length;
00224                                 
00225                                 sf::Uint32 font_size = *((sf::Uint32*)curr_res_pos);
00226                                 curr_res_pos += 4;
00227                                 
00228                                 Font* font_ptr = new Font(font_id, fontmap_id, curr_res_pos, font_size);
00229                                 fonts.insert(std::make_pair(font_id, font_ptr));
00230                                 
00231                                 curr_res_pos += font_size;
00232                         }
00233                         
00234                         //  -------- EDIBLETYPES  --------
00235                         
00236                         sf::Uint16 number_of_edibletypes = *((sf::Uint16*)curr_res_pos);
00237                         curr_res_pos += 2;
00238                         
00239                         for (size_t i = 0; i < number_of_edibletypes; ++i)
00240                         {
00241                                 sf::Uint8 edibletype_id_length = *((sf::Uint8*)curr_res_pos);
00242                                 curr_res_pos += 1;
00243                                 
00244                                 memcpy(id_buffer, curr_res_pos, edibletype_id_length);
00245                                 id_buffer[edibletype_id_length] = 0; // we must terminate it ;)
00246                                 std::string edibletype_id(id_buffer);
00247                                 curr_res_pos += edibletype_id_length;
00248                                 
00249                                 
00250                                 sf::Uint8 edibletype_mesh_id_length = *((sf::Uint8*)curr_res_pos);
00251                                 curr_res_pos += 1;
00252                                 
00253                                 memcpy(id_buffer, curr_res_pos, edibletype_mesh_id_length);
00254                                 id_buffer[edibletype_mesh_id_length] = 0; // we must terminate it ;)
00255                                 std::string edibletype_mesh_id(id_buffer);
00256                                 curr_res_pos += edibletype_mesh_id_length;
00257                                 
00258                                 
00259                                 sf::Uint8 edibletype_texture_id_length = *((sf::Uint8*)curr_res_pos);
00260                                 curr_res_pos += 1;
00261                                 
00262                                 memcpy(id_buffer, curr_res_pos, edibletype_texture_id_length);
00263                                 id_buffer[edibletype_texture_id_length] = 0; // we must terminate it ;)
00264                                 std::string edibletype_texture_id(id_buffer);
00265                                 curr_res_pos += edibletype_texture_id_length;
00266                                 
00267                                 float chance_from = *((float*)curr_res_pos);
00268                                 curr_res_pos += 4;
00269                                 
00270                                 float chance_to = *((float*)curr_res_pos);
00271                                 curr_res_pos += 4;
00272                                 
00273                                 float score = *((float*)curr_res_pos);
00274                                 curr_res_pos += 4;
00275                                 
00276                                 sf::Uint8 grows = *((sf::Uint8*)curr_res_pos);
00277                                 curr_res_pos += 1;
00278                                 
00279                                 float accelerates = *((float*)curr_res_pos);
00280                                 curr_res_pos += 4;
00281                                 
00282                                 bool reverses_others = *((sf::Uint8*)curr_res_pos);
00283                                 curr_res_pos += 1;
00284                                 
00285                                 EdibleType* edibletype_ptr = new EdibleType(edibletype_id,
00286                                         edibletype_mesh_id, edibletype_texture_id, chance_from, chance_to, score, grows,
00287                                         accelerates, reverses_others);
00288                                 edibletypes.insert(std::make_pair(edibletype_id, edibletype_ptr));
00289                         }
00290                         
00291                         
00292                         delete[] id_buffer;
00293                         
00294                         if (curr_res_pos != (resources + uncompressed_size))
00295                         {
00296                                 std::cerr << "ERROR! Resources size mismatch.\n";
00297                         }
00298                 }
00299                 
00300                 delete[] resources;
00301         }
00302         
00303         init_gl();
00304 }
00305 
00306 void ResourceManager::init_gl()
00307 {
00308         //  -------- GENERAL OPTIONS  --------
00309         
00310         glDisable(GL_DITHER);
00311         
00312         glEnable(GL_DEPTH_TEST);
00313         
00314         glEnable(GL_ALPHA_TEST);
00315         glEnable(GL_BLEND);
00316         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00317         
00318         glEnable(GL_TEXTURE_2D);
00319         glBindTexture(GL_TEXTURE_2D, 0);
00320         
00321         glEnable(GL_COLOR_MATERIAL);
00322         
00323         glEnable(GL_CULL_FACE);
00324         glCullFace(GL_BACK);
00325         glFrontFace(GL_CCW);
00326         
00327         glClearColor(63.f / 255.f, 34.f / 255.f, 20.f / 255.f, 1);
00328         // Yes. This is hard-coded. So what?!
00329         
00330         //  --------  BUFFER OBJECTS  --------
00331         
00332         // Thank you Sam!
00333         // http://sdickinson.com/wordpress/?p=122
00334         
00335         glGenBuffersARB(1, &vbo);
00336         glGenBuffersARB(1, &ibo);
00337         
00338         glBindBufferARB(GL_ARRAY_BUFFER, vbo);
00339         glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, ibo);
00340         
00341         glBufferDataARB(GL_ARRAY_BUFFER, vbo_size, vbo_data, GL_STATIC_DRAW);
00342         glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER, ibo_size, ibo_data, GL_STATIC_DRAW);
00343         
00344         glVertexPointer(3, GL_FLOAT, 32, (GLvoid*)0);
00345         //glNormalPointer(GL_FLOAT, 32, (GLvoid*)12);
00346         glTexCoordPointer(2, GL_FLOAT, 32, (GLvoid*)24);
00347         
00348         glEnableClientState(GL_VERTEX_ARRAY);
00349         //glEnableClientState(GL_NORMAL_ARRAY);
00350         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00351         
00352         // --------  VERTEX SHADER  --------
00353         
00354         vertex_shader = glCreateShader(GL_VERTEX_SHADER);
00355         
00356         const char* vertex_source =
00357                 "attribute vec3 pos2;"
00358                 //"attribute vec3 nor2;"
00359                 "attribute vec2 tex2;"
00360                 "uniform float ratio;"
00361                 "void main()"
00362                 "{"
00363                 "       gl_Position = gl_ModelViewProjectionMatrix * mix(gl_Vertex, vec4(pos2, 1.0), ratio);"
00364                 "       gl_TexCoord[0] = mix(gl_MultiTexCoord0, vec4(tex2, 1.0, 1.0), ratio);"
00365                 "       gl_FrontColor = gl_Color;"
00366                 "}";
00367         
00368         glShaderSource(vertex_shader, 1, &vertex_source, 0);
00369         glCompileShader(vertex_shader);
00370         
00371         char compile_log[1024];
00372         compile_log[0] = 0;
00373         glGetInfoLogARB(vertex_shader, sizeof(compile_log), 0, compile_log);
00374         
00375         if (compile_log[0])
00376         {
00377                 std::cout << "Vertex shader compilation log:\n" << compile_log << "\n";
00378         }
00379         
00380         vertex_program = glCreateProgram();
00381         glAttachShader(vertex_program, vertex_shader);
00382         glLinkProgram(vertex_program);
00383         
00384         // --------  FRAGMENT SHADER  --------
00385         
00386         fragment_shader_anaglyph = glCreateShader(GL_FRAGMENT_SHADER);
00387         
00388                 // COLORED
00389         const char* fragment_source_anaglyph =
00390                 "uniform sampler2D left;"
00391                 "uniform sampler2D right;"
00392                 "uniform vec3 left_mask;"
00393                 "uniform vec3 right_mask;"
00394                 "uniform float width;"
00395                 "uniform float height;"
00396                 
00397                 "void main()"
00398                 "{"
00399                 "       vec3 right_origin = texture2D(right, gl_FragCoord.xy / vec2(width, height)).rgb;"
00400                 "       vec3 left_origin  = texture2D(left,  gl_FragCoord.xy / vec2(width, height)).rgb;"
00401                 
00402                 "       gl_FragColor = vec4(left_origin  * left_mask + right_origin * right_mask, 1.0);"
00403                 //"     gl_FragColor = vec4(left_origin * mod(gl_FragCoord.y, 2.0) + right_origin * mod(gl_FragCoord.y + 1.0, 2.0), 1.0);"
00404                 //"     gl_FragColor = vec4(left_origin, 1.0);"
00405                 "}";
00406         
00407         /* // SIDE-BY-SIDE CROSS-EYED
00408         const char* fragment_source =
00409                 "uniform sampler2D left;"
00410                 "uniform sampler2D right;"
00411                 "uniform vec3 left_mask;"
00412                 "uniform vec3 right_mask;"
00413                 "uniform float width;"
00414                 "uniform float height;"
00415                 
00416                 "void main()"
00417                 "{"
00418                 " if (gl_FragCoord.x <= (width / 2.0))"
00419                 "       {"
00420                 "               vec3 right_origin = texture2D(right, gl_FragCoord.xy / vec2(width / 2.0, height)).rgb;"
00421                 "               gl_FragColor = vec4(right_origin, 1.0);"
00422                 "       }"
00423                 "       else"
00424                 "       {"
00425                 "               vec3 left_origin  = texture2D(left,  gl_FragCoord.xy / vec2(width / 2.0, height) - vec2(1.0, 0.0)).rgb;"
00426                 "               gl_FragColor = vec4(left_origin, 1.0);"
00427                 "       }"
00428                 "}";
00429         */
00430         
00431         glShaderSource(fragment_shader_anaglyph, 1, &fragment_source_anaglyph, 0);
00432         glCompileShader(fragment_shader_anaglyph);
00433         
00434         compile_log[0] = 0;
00435         glGetInfoLogARB(fragment_shader_anaglyph, sizeof(compile_log), 0, compile_log);
00436         
00437         if (compile_log[0])
00438         {
00439                 std::cout << "Anaglyph fragment shader compilation log:\n" << compile_log << "\n";
00440         }
00441         
00442         fragment_program_anaglyph = glCreateProgram();
00443         glAttachShader(fragment_program_anaglyph, fragment_shader_anaglyph);
00444         glLinkProgram(fragment_program_anaglyph);
00445         
00446         
00447         pos2_loc = glGetAttribLocation(vertex_program, "pos2");
00448         //nor2_loc = glGetAttribLocation(vertex_program, "nor2");
00449         tex2_loc = glGetAttribLocation(vertex_program, "tex2");
00450         ratio_loc = glGetUniformLocation(vertex_program, "ratio");
00451         
00452         
00453         fragment_shader_cross_eyed = glCreateShader(GL_FRAGMENT_SHADER);
00454         /*
00455         const char* fragment_source_anaglyph =
00456                 "uniform sampler2D left;"
00457                 "uniform sampler2D right;"
00458                 "uniform vec3 left_mask;"
00459                 "uniform vec3 right_mask;"
00460                 "uniform float width;"
00461                 "uniform float height;"
00462                 
00463                 "void main()"
00464                 "{"
00465                 "       vec3 right_origin = texture2D(right, gl_FragCoord.xy / vec2(width, height)).rgb;"
00466                 "       vec3 left_origin  = texture2D(left,  gl_FragCoord.xy / vec2(width, height)).rgb;"
00467                 
00468                 "       gl_FragColor = vec4(left_origin  * left_mask + right_origin * right_mask, 1.0);"
00469                 //"     gl_FragColor = vec4(left_origin * mod(gl_FragCoord.y, 2.0) + right_origin * mod(gl_FragCoord.y + 1.0, 2.0), 1.0);"
00470                 //"     gl_FragColor = vec4(left_origin, 1.0);"
00471                 "}";
00472         */
00473          // SIDE-BY-SIDE CROSS-EYED
00474         const char* fragment_source_cross_eyed =
00475                 "uniform sampler2D left;"
00476                 "uniform sampler2D right;"
00477                 "uniform vec3 left_mask;"
00478                 "uniform vec3 right_mask;"
00479                 "uniform float width;"
00480                 "uniform float height;"
00481                 
00482                 "void main()"
00483                 "{"
00484                 " if ((gl_FragCoord.y >= height / 4.0) && (gl_FragCoord.y <= height / 4.0 * 3.0))"
00485                 "       {"
00486                 "       if (gl_FragCoord.x <= (width / 2.0))"
00487                 "               {"
00488                 "                       vec3 right_origin = texture2D(right, gl_FragCoord.xy / vec2(width / 2.0, height / 2.0) - vec2(0.0, 0.5)).rgb;"
00489                 "                       gl_FragColor = vec4(right_origin, 1.0);"
00490                 "               }"
00491                 "               else"
00492                 "               {"
00493                 "                       vec3 left_origin  = texture2D(left,  gl_FragCoord.xy / vec2(width / 2.0, height / 2.0) - vec2(1.0, 0.5)).rgb;"
00494                 "                       gl_FragColor = vec4(left_origin, 1.0);"
00495                 "               }"
00496                 "       }"
00497                 " else"
00498                 "       {"
00499                 "               gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);"
00500                 "       }"
00501                 "}";
00502         
00503         glShaderSource(fragment_shader_cross_eyed, 1, &fragment_source_cross_eyed, 0);
00504         glCompileShader(fragment_shader_cross_eyed);
00505         
00506         compile_log[0] = 0;
00507         glGetInfoLogARB(fragment_shader_cross_eyed, sizeof(compile_log), 0, compile_log);
00508         
00509         if (compile_log[0])
00510         {
00511                 std::cout << "Cross-eyed fragment shader compilation log:\n" << compile_log << "\n";
00512         }
00513         
00514         fragment_program_cross_eyed = glCreateProgram();
00515         glAttachShader(fragment_program_cross_eyed, fragment_shader_cross_eyed);
00516         glLinkProgram(fragment_program_cross_eyed);
00517         
00518         
00519         glEnableVertexAttribArray(pos2_loc);
00520         //glEnableVertexAttribArray(nor2_loc);
00521         glEnableVertexAttribArray(tex2_loc);
00522         
00523         glVertexAttribPointerARB(pos2_loc, 3, GL_FLOAT, GL_FALSE, 32, (char*)0);
00524         //glVertexAttribPointerARB(nor2_loc, 3, GL_FLOAT, GL_FALSE, 32, (char*)0); // +12 otherwise
00525         glVertexAttribPointerARB(tex2_loc, 2, GL_FLOAT, GL_FALSE, 32, (char*)0); // +24 otherwise
00526         
00527         glUseProgram(vertex_program);
00528         glUniform1f(ratio_loc, 0.0);
00529         
00530         glUseProgram(fragment_program_anaglyph);
00531         
00532         left_loc = glGetUniformLocation(fragment_program_anaglyph, "left");
00533         left_mask_loc = glGetUniformLocation(fragment_program_anaglyph, "left_mask");
00534         right_loc = glGetUniformLocation(fragment_program_anaglyph, "right");
00535         right_mask_loc = glGetUniformLocation(fragment_program_anaglyph, "right_mask");
00536         width_loc = glGetUniformLocation(fragment_program_anaglyph, "width");
00537         height_loc = glGetUniformLocation(fragment_program_anaglyph, "height");
00538         
00539         glUniform1i(left_loc, 1);
00540         glUniform1i(right_loc, 2);
00541         glUniform3f(left_mask_loc, 0, 1, 1);
00542         glUniform3f(right_mask_loc, 1, 0, 0);
00543         
00544         
00545         glUseProgram(fragment_program_cross_eyed);
00546         
00547         left_loc = glGetUniformLocation(fragment_program_cross_eyed, "left");
00548         right_loc = glGetUniformLocation(fragment_program_cross_eyed, "right");
00549         width_loc = glGetUniformLocation(fragment_program_cross_eyed, "width");
00550         height_loc = glGetUniformLocation(fragment_program_cross_eyed, "height");
00551         
00552         glUniform1i(left_loc, 1);
00553         glUniform1i(right_loc, 2);
00554         
00555         
00556         glUseProgram(0);
00557         
00558         quad_displaylist = glGenLists(1);
00559         glNewList(quad_displaylist, GL_COMPILE);
00560         glBegin(GL_QUADS);
00561         glVertex2i(-1, -1);
00562         glVertex2i( 1, -1);
00563         glVertex2i( 1,  1);
00564         glVertex2i(-1,  1);
00565         glEnd();
00566         glEndList();
00567         
00568         glMatrixMode(GL_MODELVIEW); // just in case...
00569 }
00570 
00571 void ResourceManager::use_program(GLenum shader)
00572 {
00573         if (shader == GL_VERTEX_SHADER)
00574         {
00575                 glUseProgram(vertex_program);
00576                 return;
00577         }
00578         
00579         if (shader == GL_FRAGMENT_SHADER)
00580         {
00581                 std::string mode = ConfigManager::instance().get_string("3D");
00582                 
00583                 GLint program_to_use = 0;
00584                 
00585                 if (mode == "anaglyph")
00586                 {
00587                         program_to_use = fragment_program_anaglyph;
00588                 }
00589                 if (mode == "cross-eyed")
00590                 {
00591                         program_to_use = fragment_program_cross_eyed;
00592                 }
00593                 
00594                 glUseProgram(program_to_use);
00595                 
00596                 left_loc = glGetUniformLocation(program_to_use, "left");
00597                 left_mask_loc = glGetUniformLocation(program_to_use, "left_mask");
00598                 right_loc = glGetUniformLocation(program_to_use, "right");
00599                 right_mask_loc = glGetUniformLocation(program_to_use, "right_mask");
00600                 width_loc = glGetUniformLocation(program_to_use, "width");
00601                 height_loc = glGetUniformLocation(program_to_use, "height");
00602                 
00603                 return;
00604         }
00605         
00606         glUseProgram(0);
00607 }
00608 
00609 void ResourceManager::render_fullscreen_quad()
00610 {
00611         glMatrixMode(GL_PROJECTION);
00612         glPushMatrix();
00613         glLoadIdentity();
00614         
00615         glMatrixMode(GL_MODELVIEW);
00616         glPushMatrix();
00617         glLoadIdentity();
00618         
00619         glTranslatef(0, 0, -0.5);
00620         
00621         glCallList(quad_displaylist);
00622         
00623         glMatrixMode(GL_PROJECTION);
00624         glPopMatrix();
00625         
00626         glMatrixMode(GL_MODELVIEW);
00627         glPopMatrix();
00628 }
00629 
00630 void ResourceManager::uninit_gl()
00631 {
00632         Texture::unbind();
00633         
00634         glDisableClientState(GL_VERTEX_ARRAY);
00635         //glDisableClientState(GL_NORMAL_ARRAY);
00636         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00637         
00638         glBindBufferARB(GL_ARRAY_BUFFER, 0);
00639         glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
00640         
00641         glDeleteBuffersARB(1, &vbo);
00642         glDeleteBuffersARB(1, &ibo);
00643         
00644         
00645         glDisableVertexAttribArray(pos2_loc);
00646         //glDisableVertexAttribArray(nor2_loc);
00647         glDisableVertexAttribArray(tex2_loc);
00648         
00649         glUseProgram(0);
00650         
00651         glDetachShader(vertex_program, vertex_shader);
00652         glDeleteShader(vertex_shader);
00653         glDeleteProgram(vertex_program);
00654         
00655         glDetachShader(fragment_program_anaglyph, fragment_shader_anaglyph);
00656         glDeleteShader(fragment_shader_anaglyph);
00657         glDeleteProgram(fragment_program_anaglyph);
00658         
00659         glDetachShader(fragment_program_cross_eyed, fragment_shader_cross_eyed);
00660         glDeleteShader(fragment_shader_cross_eyed);
00661         glDeleteProgram(fragment_program_cross_eyed);
00662         
00663         glDeleteLists(quad_displaylist, 1);
00664 }
00665 
00666 EdibleType* ResourceManager::get_random_edibletype()
00667 {
00668         float number = sf::Randomizer::Random(0.0f, 1.0f);
00669         
00670         for (std::map< std::string, EdibleType* >::iterator it = edibletypes.begin(); it != edibletypes.end(); ++it)
00671         {
00672                 if ((it->second->chance_from <= number) && (it->second->chance_to >= number))
00673                 {
00674                         return it->second;
00675                 }
00676         }
00677         std::cout << "OOPS!\n";
00678         return 0;
00679 }
00680 
00681 ResourceManager::~ResourceManager()
00682 {
00683         uninit_gl();
00684         
00685         for (std::map< std::string, Mesh* >::iterator it = meshes.begin(); it != meshes.end(); ++it)
00686         {
00687                 delete it->second;
00688         }
00689         
00690         for (std::map< std::string, Texture* >::iterator it = textures.begin(); it != textures.end(); ++it)
00691         {
00692                 delete it->second;
00693         }
00694         
00695         for (std::map< std::string, Animation* >::iterator it = animations.begin(); it != animations.end(); ++it)
00696         {
00697                 delete it->second;
00698         }
00699         
00700         for (std::map< std::string, Font* >::iterator it = fonts.begin(); it != fonts.end(); ++it)
00701         {
00702                 delete it->second;
00703         }
00704         
00705         for (std::map< std::string, EdibleType* >::iterator it = edibletypes.begin(); it != edibletypes.end(); ++it)
00706         {
00707                 delete it->second;
00708         }
00709         
00710         delete[] vbo_data;
00711         delete[] ibo_data;
00712 }
 Összes Osztályok