/*
 * Copyright (c) 1995 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Revolving Texture Mapped Teapot - 15-bit
 */
#include <stddef.h>
#include <stdio.h>
#include "brender.h"
#include "dosio.h"


int main()
{
   br_pixelmap *screen_buffer, *back_buffer, *depth_buffer, *shade;
   br_pixelmap *tile_pm;
   br_actor *world, *observer, *teapot;
   int i;
   br_material *mats[10];
  
/*************** Initialize Renderer and Set Screen Resolution *************/
	BrBegin();
        screen_buffer = DOSGfxBegin("VESA,B:15");
	BrZbBegin(screen_buffer->type,BR_PMT_DEPTH_16);       
	back_buffer = BrPixelmapMatch(screen_buffer,BR_PMMATCH_OFFSCREEN);
        back_buffer->origin_x=back_buffer->width/2;
        back_buffer->origin_y=back_buffer->height/2;
	depth_buffer = BrPixelmapMatch(screen_buffer,BR_PMMATCH_DEPTH_16);
      /* Load Shade Table */
        shade = BrPixelmapLoad("shade15.tab");
        if (!shade)
          BR_ERROR0("Unable to load shade15.tab");
        BrTableAdd(shade);

/************************ Build the World Database *************************/

	world = BrActorAllocate(BR_ACTOR_NONE,NULL);
	BrLightEnable(BrActorAdd(world,BrActorAllocate(BR_ACTOR_LIGHT,NULL)));
    
/*Load and Position Camera*/        
	observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
	observer->t.type = BR_TRANSFORM_MATRIX34;        
	BrMatrix34Translate(&observer->t.t.mat,BR_SCALAR(0.0),BR_SCALAR(0.0),BR_SCALAR(5.0));

/*Load and Register Texture */
        tile_pm = BrPixelmapLoad("refmap.pix");
	if (tile_pm==NULL)
                BR_ERROR("Couldn't load refmap.pix");
	BrMapAdd(tile_pm);

/*Load and Register teapot Material */
        i = BrFmtScriptMaterialLoadMany("teapot.mat",mats,BR_ASIZE(mats));
	BrMaterialAddMany(mats,i);                                      
	
/*Load and Position teapot Actor*/
        teapot = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
        teapot->model = BrModelLoad("teapot.dat");
        BrModelApplyMap(teapot->model,BR_APPLYMAP_PLANE,NULL);
        BrModelAdd(teapot->model);
        teapot->t.type = BR_TRANSFORM_MATRIX34;

/*Assign teapot Material */
        teapot->material = BrMaterialFind("CHROME GIFMAP");
/******************************* Animation Loop ****************************/                                                              
	  
	for(i=0;i<200;i++) {
		
		BrPixelmapFill(back_buffer,0);
		BrPixelmapFill(depth_buffer,0xFFFFFFFF);
		BrZbSceneRender(world,observer,back_buffer,depth_buffer);
		BrPixelmapDoubleBuffer(screen_buffer,back_buffer);
		
                BrMatrix34PostRotateY(&teapot->t.t.mat,BR_ANGLE_DEG(2.0));
	       
			}                                           
/*Close down*/
	BrPixelmapFree(depth_buffer);
	BrPixelmapFree(back_buffer);
	BrZbEnd();
	DOSGfxEnd();
	BrEnd();

	return 0;
}


