view tree_controll.c @ 0:01387a2e419e

initial version
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 07 Dec 2010 15:39:45 +0900
parents
children 8afbbe129730
line wrap: on
line source

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#include "object.h"
#include "tree_controll.h"
#include "LoadSprite.h"
#include "xml.h"


static int power_of_two(int input)
{
  int value = 1;

  while ( value < input )
    {
      value <<= 1;
    }
  return value;
}


GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
{
  GLuint texture;
  int w, h;
  SDL_Surface *image;
  SDL_Rect area;
  Uint32 saved_flags;
  Uint8  saved_alpha;

  /* Use the surface width and height expanded to powers of 2 */
  w = power_of_two(surface->w);
  h = power_of_two(surface->h);
  texcoord[0] = 0.0f;                     /* Min X */
  texcoord[1] = 0.0f;                     /* Min Y */
  texcoord[2] = (GLfloat)surface->w / w;  /* Max X */
  texcoord[3] = (GLfloat)surface->h / h;  /* Max Y */

  image = SDL_CreateRGBSurface(
			       SDL_SWSURFACE,
			       w, h,
			       32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
			       0x000000FF, 
			       0x0000FF00, 
			       0x00FF0000, 
			       0xFF000000
#else
			       0xFF000000,
			       0x00FF0000, 
			       0x0000FF00, 
			       0x000000FF
#endif
			       );
  if ( image == NULL )
    {
      return 0;
    }

  /* Save the alpha blending attributes */
  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
  saved_alpha = surface->format->alpha;
  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    {
      SDL_SetAlpha(surface, 0, 0);
    }

  /* Copy the surface into the GL texture image */
  area.x = 0;
  area.y = 0;
  area.w = surface->w;
  area.h = surface->h;
  SDL_BlitSurface(surface, &area, image, &area);

  /* Restore the alpha blending attributes */
  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    {
      SDL_SetAlpha(surface, saved_flags, saved_alpha);
    }

  /* Create an OpenGL texture for the image */
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D,
	       0,
	       GL_RGBA,
	       w, h,
	       0,
	       GL_RGBA,
	       GL_UNSIGNED_BYTE,
	       image->pixels);
  SDL_FreeSurface(image); /* No longer needed */

  return texture;
}


float tes_angle[3] = {0,0,0};
/*
void obj_draw(SURFACE *surfaces)
{
  int n;

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, (GLuint)surfaces->texture);

  glTranslatef(surfaces->xyz[0],surfaces->xyz[1],surfaces->xyz[2]);

  glTranslatef(*(surfaces->data[2][0]), *(surfaces->data[2][0]+1), *(surfaces->data[2][0]+2));

  glRotatef(surfaces->angle[0], 1, 0, 0);
  glRotatef(surfaces->angle[1], 0, 1, 0);
  glRotatef(surfaces->angle[2], 0, 0, 1);
  
  glTranslatef(-*(surfaces->data[2][0]), -*(surfaces->data[2][0]+1), -*(surfaces->data[2][0]+2));
  
  glBegin( GL_TRIANGLES);
  for(n=0; n<surfaces->size; n++)
    {
      glTexCoord2f(*(surfaces->data[3][n]),*(surfaces->data[3][n]+1));
      glVertex3f(*(surfaces->data[0][n]),*(surfaces->data[0][n]+1),*(surfaces->data[0][n]+2));
      glNormal3f(*(surfaces->data[1][n]),*(surfaces->data[1][n]+1),*(surfaces->data[1][n]+2));
    }
  glEnd( );
  glDisable(GL_TEXTURE_2D);
}
*/


SURFACE *next_draw_node(SURFACE *surfaces)
{
  SURFACE *node = surfaces;
  if(node->child != NULL)
    {
      //glPushMatrix();
      return node->child;
    }
  else if(node->brother != NULL)
    {
      //glPopMatrix();
      //glPushMatrix();
      return node->brother;
    }
  else
    {
      node = node->parent;
      if(node != NULL)
	{
	  //glPopMatrix();
	}
      while(node != NULL)
        {
          if(node->brother != NULL)
            {
	      //glPopMatrix();
	      //glPushMatrix();
	      return node->brother;
            }
          else
            {
              node = node->parent;
	      /*
	      if(node != NULL)
		{
		  //glPopMatrix();
		}
	      */
            }
        }
      return NULL;
    }
}

SURFACE *search_node(OBJECT *top, char *name)
{
  SURFACE *node;
  SURFACE *ret_node = NULL;
  for(node = top->surfaces;node != NULL;node = next_node(node))
    {
      if(strcmp(node->name, name) == 0)
	{
	  ret_node = node;
	  return ret_node;
	}
    }
  printf("can't not find node\n");
  return ret_node;
}
/*
void all_obj_draw(OBJECT *top)
{
  SURFACE *node;
  node = top->surfaces;
  glPushMatrix();
  while(node != NULL)
    {
      obj_draw(node);
      //if(node->child != NULL)
	{
	  node = next_draw_node(node);
	}
	//
      else
	{
	  //node = node->next;
	}
      //
    }
  glPopMatrix();
}
*/

SURFACE *next_node(SURFACE *surfaces)
{
  SURFACE *node = surfaces;
  if(node->child != NULL)
    {
      return node->child;
    }
  else if(node->brother != NULL)
    {
      return node->brother;
    }
  else
    {
      node = node->parent;
      while(node != NULL)
        {
          if(node->brother != NULL)
            {
	      return node->brother;
            }
          else
            {
              node = node->parent;
            }
        }
      return NULL;
    }
}

/*
void node_draw(OBJECT *top, char *name)
{
  SURFACE *node;
  for(node=top->surfaces; node!=NULL; node=next_node(node))
    {
      if(!strcmp(node->name, name))
        {
	  glPushMatrix();
          obj_draw(node);
	  glPopMatrix();
        }
    }
}
*/


void node_prameter_change(OBJECT *top, char *name, float x, float y, float z, float ax, float ay, float az)
{
  SURFACE *node;
  for(node=top->surfaces; node!=NULL; node=next_node(node))
    {
      if(!strcmp(node->name, name))
      {
	node->xyz[0] = x;
	node->xyz[1] = y;
	node->xyz[2] = z;
	node->angle[0] = ax;
	node->angle[1] = ay;
	node->angle[2] = az;
      }
    }
}
	 
void all_object_load_texture(OBJECT *top)
{
  SURFACE *node;
  SDL_Surface *image;
  GLfloat texcoord[4];
  for(node=top->surfaces; node!=NULL; node=next_node(node))
    { 
      //image = SDL_LoadBMP(node->image_name);
      //image = IMG_Load(node->image_name);
      image = LoadSprite(node);
      node->texture = (int *)SDL_GL_LoadTexture(image, texcoord);
      SDL_FreeSurface(image);
    }
}