view Renderer/Engine/polygon.cc @ 1087:20f09564c586 draft

fix (not yet tested)
author root@localhost.localdomain
date Fri, 17 Dec 2010 18:34:29 +0900
parents 9ce0480cde63
children b733c7903d07
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(void)
{
    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;


#if !SPE_CREATE_POLYGON

    for (int i = 0; i < 16; i++) {
        matrix[i] = 0;
    }

#endif

}

void Polygon::pickup_coordinate(char *cont)
{

    char *tmp_cont = cont;
    int bound = 8; // coord_vertex size is 8byte.
    int offset = 0;

    for(int n=0; n<size*3; n+=3)
    {
        tmp_cont = pickup_float(tmp_cont, coord_xyz+n);
        tmp_cont = pickup_float(tmp_cont, coord_xyz+n+1);
        tmp_cont = pickup_float(tmp_cont, coord_xyz+n+2);

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

    tmp_cont = cont;

    for(int n=0; n<size; n++)
    {
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset);
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset+1);
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset+2);

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

#if SPE_CREATE_POLYGON_CHECK

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

      if (*(coord_xyz+i*3) != *(coord_pack+i*bound+offset)) {
	printf("hoge\n");
      }

      if (*(coord_xyz+i*3+1) != *(coord_pack+i*bound+offset+1)) {
	printf("hoge\n");
      }

      if (*(coord_xyz+i*3+2) != *(coord_pack+i*bound+offset+2)) {
	printf("hoge\n");
      }

    }

#endif

}

void Polygon::pickup_normal(char *cont)
{

    char *tmp_cont = cont;
    int bound = 8; // coord_pack size is 8byte.
    int offset = 5; // coord_pack (xyz(3) + tex_x,tex_y(2) + n_xyz(3))

    for (int n = 0; n<size*3; n += 3)
    {
        tmp_cont = pickup_float(tmp_cont, normal+n);
        tmp_cont = pickup_float(tmp_cont, normal+n+1);
        tmp_cont = pickup_float(tmp_cont, normal+n+2);

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

    tmp_cont = cont;

    for(int n=0; n<size; n++)
    {
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset);
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset+1);
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset+2);

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

#if SPE_CREATE_POLYGON_CHECK

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

      if (*(normal+i*3) != *(coord_pack+i*bound+offset)) {
	printf("hoge\n");
      }

      if (*(normal+i*3+1) != *(coord_pack+i*bound+offset+1)) {
	printf("hoge\n");
      }

      if (*(normal+i*3+2) != *(coord_pack+i*bound+offset+2)) {
	printf("hoge\n");
      }

    }

#endif

}

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 bound = 8; // coord_pack size is 8byte.
    int offset = 3; // coord_pack (xyz(3) + tex_x,tex_y(2) + n_xyz(3))

    for (int n = 0; n < size*3; n += 3)
    {
        tmp_cont = pickup_float(tmp_cont, coord_tex+n);
        tmp_cont = pickup_float(tmp_cont, coord_tex+n+1);
        coord_tex[n+2] = 1.0;

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

    tmp_cont = cont;

    for(int n=0; n<size; n++)
    {
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset);
        tmp_cont = pickup_float(tmp_cont, coord_pack+n*bound+offset+1);

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

#if SPE_CREATE_POLYGON_CHECK

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

      if (*(coord_tex+i*3) != *(coord_pack+i*bound+offset)) {
	printf("hoge\n");
      }

      if (*(coord_tex+i*3+1) != *(coord_pack+i*bound+offset+1)) {
	printf("hoge\n");
      }


    }

#endif

}

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);
}