view Renderer/Engine/polygon.cc @ 1319:31455d34e502 draft

collada file reader minor changes.
author Taiki TAIRA <e095767@ie.u-ryukyu.ac.jp>
date Sun, 18 Dec 2011 09:39:14 +0900
parents bb9c885c1cb5
children 3f95f61faef6
line wrap: on
line source

#include <iostream>
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#include "polygon.h"
#include "xml.h"
#include "matrix_calc.h"
#include "triangle.h"
#include "vertex.h"
#include "Span.h"
#include "SpanC.h"
#include "scene_graph_pack.h"
#include "error.h"
#include "viewer_types.h"
using namespace std;




Polygon::Polygon()
{
    position_init();
}

void
Polygon::position_init(void)
{

    xyz[0] = 0;
    xyz[1] = 0;
    xyz[2] = 0;
    xyz[3] = 1;
    c_xyz[0] = 0;
    c_xyz[1] = 0;
    c_xyz[2] = 0;
    c_xyz[3] = 1;
    angle[0] = 0;
    angle[1] = 0;
    angle[2] = 0;
    angle[3] = 1;
    scale[0] = 1;
    scale[1] = 1;
    scale[2] = 1;


}

void Polygon::pickup_coordinate(char *cont)
{

    // size は頂点の数, count は面の数
    char *tmp_cont = cont;
    int count = size / 3;

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      
      int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ;
      
      //それを構造体に登録
      pp[i].info.size = tri_size;
      

      for (int j = 0; j < tri_size; j++) {

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver1.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver1.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver1.z));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver2.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver2.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver2.z));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver3.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver3.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver3.z));

	if (tmp_cont == NULL)
	  {
            cout << "Analyzing obj data failed coordinate\n";
	  }

	count -= 1;
	
      }
          
    }

    if (count != 0) {
          printf("miss pickup_coordinate size. diff size = %d\n", count);
    }

}

void Polygon::pickup_normal(char *cont)
{


    // size は頂点の数, count は面の数
    char *tmp_cont = cont;
    int count = size / 3;

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ;
      pp[i].info.size = tri_size;

      for (int j = 0; j < tri_size; j++) {

	tmp_cont = pickup_float(tmp_cont, &(tri[j].normal1.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal1.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal1.z));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].normal2.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal2.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal2.z));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].normal3.x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal3.y));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].normal3.z));


	if (tmp_cont == NULL)
	  {
            cout << "Analyzing obj data failed coordinate\n";
	  }

	count -= 1;
	
      }
      
    
    }

    if (count != 0) {
          printf("miss pickup_normal size. diff size = %d\n", count);
    }

}

void Polygon::pickup_model(char *cont)
{
    cont = pickup_float(cont,c_xyz);
    cont = pickup_float(cont,c_xyz+1);
    cont = pickup_float(cont,c_xyz+2);

    if (cont == NULL)
    {
        cout << "Analyzing obj data failed model\n";
    }
}

void Polygon::pickup_texture(char *cont)
{

    char *tmp_cont = cont;
    int count = size / 3;

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ;
      pp[i].info.size = tri_size;

      for (int j = 0; j < tri_size; j++) {

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver1.tex_x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver1.tex_y));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver2.tex_x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver2.tex_y));

	tmp_cont = pickup_float(tmp_cont, &(tri[j].ver3.tex_x));
        tmp_cont = pickup_float(tmp_cont, &(tri[j].ver3.tex_y));


	if (tmp_cont == NULL)
	  {
            cout << "Analyzing obj data failed coordinate\n";
	  }

	count -= 1;
	
      }
      
    
    }

    if (count != 0) {
          printf("miss pickup_texture size. diff size = %d\n", count);
    }



}

char *get_pixel(int tx, int ty, SDL_Surface *texture_image)
{
    return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx));
}

unsigned my_ntohl(unsigned u) {
    //     rr gg bb 00
    //           rr
    //     bb gg rr
    //unsigned u1 =   ((u&0xff)<<24) +
    //         ((u&0xff00)<<8) +
    //         ((u&0xff0000)>>8) +
    //         ((u&0xff000000)>>24);
    unsigned u1;
    unsigned b = (u&0xff000000)>>24;
    unsigned g = (u&0xff0000)>>16;
    unsigned r = (u&0xff00)>>8;
    u1 = r + (g<<8) + (b<<16);
    //printf("pixel %x->%x\n",u,u1);
    return u1;
}

Uint32 Polygon::get_rgb(int tx, int ty)
{
    SDL_PixelFormat *fmt;
    //Uint32 temp, pixel;
    Uint8 red, green, blue;

    fmt = texture_info->texture_image->format;

    if (tx<0) tx = 0;
    if (texture_info->texture_image->w-1< tx) tx = texture_info->texture_image->w-1 ;
    if (ty<0) ty = 0;
    if (texture_info->texture_image->h-1< ty) ty = texture_info->texture_image->h-1 ;


    //SDL_LockSurface(texture_image);
    char *p = get_pixel(tx,ty,texture_info->texture_image);
    blue  = (Uint8) p[0];
    green = (Uint8) p[1];
    red   = (Uint8) p[2];

    //printf("tx = %d ty = %d ", tx,ty);
    //printf("pixel color =>  R: %d,  G: %d,  B: %d\n", red, green, blue);

    SDL_PixelFormat *pf = NULL;
    //pf = viewer->screen->format;

    //cout << SDL_MapRGB(pf, red, green, blue) << endl;
    return SDL_MapRGB(pf, red, green, blue);
}

void Polygon::font_coordinate(int baseline, int row,int width)
{

    //int count = size / 3;

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      int tri_size = 2;
      pp[i].info.size = tri_size;

      for (int j = 0; j < tri_size; j++) {

	tri[j].ver1.x = 0.0 ;
        tri[j].ver1.y = 0.0 + baseline;
	//tri[j].ver1.y = 0.0;
	tri[j].ver1.z = 0.0;
	
	if(j < 1)
	{
	  tri[j].ver2.x = 0.0 ;
	  tri[j].ver2.y = row +baseline;
	  //tri[j].ver2.y = row;
	  tri[j].ver2.z = 0.0;
	}else{
	  tri[j].ver2.x = width ;
          tri[j].ver2.y = 0.0 +baseline;
          //tri[j].ver2.y = 0.0;
	  tri[j].ver2.z = 0.0;
      }
	tri[j].ver3.x = width ;
        tri[j].ver3.y =row +baseline;
        //tri[j].ver3.y =row;
	tri[j].ver3.z = 0.0;

      }
          
    }
}

void Polygon::font_normal()
{

    //テスト用
    float polygon_point[2][9] = { {0,0,1, 0,0,1, 0,0,1},
				  {0,0,1, 0,0,1, 0,0,1} };

    //int count = size / 3;

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      int tri_size = 2;
      pp[i].info.size = tri_size;

      for (int j = 0; j < tri_size; j++) {

	tri[j].normal1.x = polygon_point[j][0];
        tri[j].normal1.y = polygon_point[j][1];
	tri[j].normal1.z = polygon_point[j][2];

	tri[j].normal2.x = polygon_point[j][3];
        tri[j].normal2.y = polygon_point[j][4];
	tri[j].normal2.z = polygon_point[j][5];

	tri[j].normal3.x = polygon_point[j][6];
        tri[j].normal3.y = polygon_point[j][7];
	tri[j].normal3.z = polygon_point[j][8];

      }
          
    }

}

void Polygon::font_model()
{
    //テスト用
    float polygon_point[3] = {0,0,0};
	
    c_xyz[0] = polygon_point[0];
    c_xyz[1] = polygon_point[1];  
    c_xyz[2] = polygon_point[2];  

}


void Polygon::font_texture(float row,float width)
{
  float texture_point[2][7] = { {0,0, 0,1, 1,1},
				{0,0, 1,0, 1,1} };

    for (int i = 0; i < pp_num; i++) {

      TrianglePackPtr tri =  pp[i].tri;
      // TrianglePack の size のチェック
      int tri_size = 2;
      pp[i].info.size = tri_size;

      for (int j = 0; j < tri_size; j++) {

	tri[j].ver1.tex_x = texture_point[j][0];
	tri[j].ver1.tex_y = texture_point[j][1];
	
	tri[j].ver2.tex_x = texture_point[j][2];
	tri[j].ver2.tex_y = texture_point[j][3];

	tri[j].ver3.tex_x = texture_point[j][4];
	tri[j].ver3.tex_y = texture_point[j][5];

	
      }
      
    
    }

    /*if (count != 0) {
          printf("miss pickup_texture size. diff size = %d\n", count);
	  }*/

}