Advertisement
Guest User

painter.cpp

a guest
Jun 4th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.30 KB | None | 0 0
  1. #include "painter.h"
  2.  
  3.  
  4. namespace LIB {
  5.  
  6.     Painter::Painter() :
  7.         ResMan<sf::Texture>() {
  8.  
  9.  
  10.         g_renderer = nullptr;
  11. //        a_renderer = nullptr;
  12.  
  13.         tex_witch = nullptr;
  14.         tex_tiles = nullptr;
  15.  
  16.  
  17.     }
  18.  
  19.     Painter::~Painter() {
  20.         unloadAll();
  21.     }
  22.  
  23.     bool Painter::init(CORE::Renderer* ren) {
  24.  
  25.         g_renderer = ren;
  26.  
  27.         /*
  28.         if((a_renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED)) == nullptr) {
  29.             printf("Unable to create renderer");
  30.             return false;
  31.         }*/
  32.  
  33.         tex_witch = aquire("data/gfx/witch.png");
  34.         tex_tiles = aquire("tex.png");
  35.  
  36.  
  37.         return true;
  38.  
  39.     }
  40.  
  41.     void Painter::cleanup() {
  42. /*
  43.         if(a_renderer) {
  44.             SDL_DestroyRenderer(a_renderer);
  45.             a_renderer = NULL;
  46.         }
  47. */
  48.  
  49.     }
  50.  
  51.     sf::Texture* Painter::load(const std::string& path) {
  52.  
  53.         sf::Texture* tex = new sf::Texture;
  54.  
  55.         if(!tex->loadFromFile(path.c_str())) {
  56.             printf("Unable to load image(s)");
  57.             return nullptr;
  58.         }
  59.  
  60.         ///sf::Texture::bind(&texture);
  61.         ///sf::Texture::bind(NULL);
  62.  
  63.         return tex;
  64.  
  65.     }
  66.  
  67.     // Removes the textures
  68.     void Painter::unload(const std::string& path) {
  69.         sf::Texture* tex = find(path);
  70.         if(tex!=0) {
  71.             delete tex;
  72.             map_files.erase(map_files.find(path));
  73.         }
  74.     }
  75.  
  76.     //Removes all textures
  77.     void Painter::unloadAll() {
  78.         while(map_files.begin()!=map_files.end()) {
  79.             delete map_files.begin()->second;
  80.             map_files.erase(map_files.begin());
  81.         }
  82.     }
  83.  
  84.  
  85.     void Painter::paint(double ox, double oy, int w, int h, int mx, int my) {
  86.  
  87.             mx = sf::Mouse::getPosition().x;
  88.             my = sf::Mouse::getPosition().y;
  89.  
  90.             //glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  91.             //glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
  92.  
  93.             glViewport(0, 0, w, h);
  94.             glMatrixMode(GL_PROJECTION);
  95.             glLoadIdentity();
  96.             glOrtho(0, w, h, 0, -h, h);
  97.  
  98.             glMatrixMode(GL_MODELVIEW);
  99.             glLoadIdentity();
  100.  
  101.             ///xxxxxxxxxxxxxxxx
  102.             ///GLuint bufferID; //x
  103.             //GLuint VertexVBOID; //x
  104.             GLuint IndexVBOID; //x
  105.             glGenBuffers(1, &IndexVBOID);
  106.  
  107.             glm::vec3 right(1.0, 0.0, 0.0);
  108.             glm::vec3 up(0.0, 1.0, 0.0);
  109.  
  110.             glm::vec3 position = glm::vec3( 0, 0, 0 );
  111.             glm::vec3 plysize = glm::vec3( 20.0/32.0, 20.0/32.0, -28.0/24.0 );
  112.             glm::vec3 plypos = glm::vec3( 5.0, 5.0, plysize.z+2.0 );
  113.             float speed = 7.3f; // units / second
  114.  
  115.             float camdir = 0.0;
  116.             float camdir_ang = camdir*180.0/3.1415;
  117.             ///xxxxxxxxxxxxxxxx
  118.  
  119.             LinkedList<CORE::Draw> draw_list;
  120.             g_renderer->fill_draw_list(draw_list);
  121.  
  122.             for( auto it = draw_list.first; it != 0; it = it->next ) {
  123.                 switch(it->val->get_draw_kind()) {
  124.                 case CORE::Draw::D_MODEL:
  125.  
  126.                     CORE::Model* m = (CORE::Model*)it->val;
  127.  
  128.                     glMatrixMode(GL_MODELVIEW);
  129.                     glLoadIdentity();
  130.                     glTranslatef(-ox, -oy, 0);
  131.  
  132.  
  133.                     //MODEL BEGIN
  134.                     ///////////////////
  135.                     int dat_len;
  136.                     float* data = m->get_verts(dat_len);
  137.                     if(dat_len==0) break;
  138.  
  139.                     GLfloat* modeldata = new GLfloat[dat_len];
  140.                     for( int i=0; i<dat_len; ++i ) modeldata[i] = data[i];
  141.  
  142.  
  143.                     // Culling
  144.                     glEnable(GL_CULL_FACE);
  145.                     glCullFace(GL_BACK);
  146.                     // Tex repeat
  147.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  148.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  149.                     // Light
  150.                     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  151.                     ///if(m->shape!=nullptr) break;
  152.                     ///GLfloat mat_shininess[] = { 50.0, 50.0, 50.0, 1.0 };;
  153.                     ///GLfloat light_position[] = { GLfloat(fmax(0.0f, ox+512)), GLfloat(fmax(0.0f, oy+512)), 100.0f, 1.0f }; //ok
  154.                     GLfloat light_position[] = { GLfloat(fmax(0.0f, ox+mx)), GLfloat(fmax(0.0f, oy+my)), 60.0f, 1.0f };
  155.                     //glClearColor (0.0, 0.0, 0.0, 0.0);
  156.                     glShadeModel (GL_SMOOTH);
  157.  
  158.                     glLightfv(GL_LIGHT0, GL_SPECULAR, mat_specular);
  159.                     //glLightfv(GL_LIGHT0, GL_SHININESS, mat_shininess);
  160.                     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  161.  
  162.                     glEnable(GL_LIGHTING);
  163.                     glEnable(GL_COLOR_MATERIAL);
  164.                     glEnable(GL_LIGHT0);
  165.                     //glEnable(GL_DEPTH_TEST);
  166.  
  167.  
  168.                     // Enable vertex components
  169.                     glEnableClientState(GL_VERTEX_ARRAY);
  170.                     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  171.                     glEnableClientState(GL_COLOR_ARRAY);
  172.                     glEnableClientState(GL_NORMAL_ARRAY);
  173.  
  174.                     glVertexPointer(   3, GL_FLOAT, 11 * sizeof(GLfloat), modeldata);
  175.                     glTexCoordPointer( 2, GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 3);
  176.                     glColorPointer(    3, GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 5);
  177.                     glNormalPointer(      GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 8);
  178.  
  179.                     sf::Texture::bind(tex_tiles);
  180.                     //sf::Texture::bind(nullptr);
  181.                     glDrawArrays(GL_TRIANGLES, 0, dat_len/11);
  182.                     ///////////////////
  183.                     delete [] modeldata;
  184.                     //MODEL END
  185.  
  186.  
  187.                     //WIREFAME BEGIN
  188.                     ///////////////////
  189.                     int wire_len;
  190.                     float* wireframe = m->get_wire(wire_len);
  191.                     if(wire_len==0) break;
  192.  
  193.                     GLfloat* wireframedata = new GLfloat[wire_len];
  194.                     for( int i=0; i<wire_len; ++i ) wireframedata[i] = wireframe[i];
  195.  
  196.                     glDisable(GL_CULL_FACE);
  197.                     glDisable(GL_LIGHTING);
  198.                     glDisable(GL_COLOR_MATERIAL);
  199.                     glDisable(GL_LIGHT0);
  200.  
  201.  
  202.                     // Enable vertex components
  203.                     glEnableClientState(GL_VERTEX_ARRAY);
  204.                     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  205.                     glEnableClientState(GL_COLOR_ARRAY);
  206.                     glDisableClientState(GL_NORMAL_ARRAY);
  207.  
  208.                     glVertexPointer(   3, GL_FLOAT, 6 * sizeof(GLfloat), wireframedata);
  209.                     glColorPointer(    3, GL_FLOAT, 6 * sizeof(GLfloat), wireframedata + 3);
  210.  
  211.                     //sf::Texture::bind(nullptr);
  212.                     //sf::Texture::bind(nullptr);
  213.                     glDrawArrays(GL_LINES, 0, wire_len/6);
  214.                     ///////////////////
  215.                     delete [] wireframedata;
  216.                     //WIREFAME END
  217.  
  218.                     break;
  219.                 }
  220.             }
  221.  
  222.             //SDL_RenderPresent(a_renderer);
  223.  
  224.  
  225.  
  226.     }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement