/* Copyright (c) 1996 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Texture Mapped Teapot (15-bit colour)
 * by Ady

Sample of a callback function. 
Counts number of primitives displayed.
*/

#include <stddef.h>
#include <stdio.h>
#include "brender.h"
#include "dosio.h"
#include "conio.h"

long prims=0;     // Number of primitives displayed.

//***************** Callback function see page 307 **********************

void BR_CALLBACK CBFnPrimitive(
br_primitive* primitive,
br_actor* actor,
const br_model* model,
const br_material* material,
br_order_table* order_table,
const br_scalar* z)
{
br_uint_16 bucket;

// Find bucket  page 309
bucket=BrZsPrimitiveBucketSelect(  
z,
primitive->type,
order_table->min_z,
order_table->max_z,
order_table->size,
order_table->type);

prims++;    // Count displayed primitives.

// Render - page 262
BrZsOrderTablePrimitiveInsert(order_table,primitive,bucket);
}

//**************************************************************************
void main()
{
   br_pixelmap  *screen_buffer, *back_buffer;
   br_actor *world, *observer, *planet;
   br_material *planet_material;
   br_primitive_cbfn *thiscallback,*oldcallback;
   void *primitives;

//************** Initialize Renderer and Set Screen Resolution *************

	BrBegin();
    screen_buffer = DOSGfxBegin("VESA,W:640,H:480,B:15");

    primitives = BrMemAllocate(200000,BR_MEMORY_APPLICATION);
    BrZsBegin(screen_buffer->type,primitives,500000);
    
//  Allocate other buffers
     
	back_buffer = BrPixelmapMatch(screen_buffer,BR_PMMATCH_OFFSCREEN);
	back_buffer->origin_x = back_buffer->width/2;
	back_buffer->origin_y = back_buffer->height/2;

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

	world = BrActorAllocate(BR_ACTOR_NONE,NULL);
	BrLightEnable(BrActorAdd(world,BrActorAllocate(BR_ACTOR_LIGHT,NULL)));


// Load and Position Camera Actor

        observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
        observer->t.type = BR_TRANSFORM_MATRIX34;        
        BrMatrix34PostTranslate(&observer->t.t.mat,BR_SCALAR(0.0),BR_SCALAR(0.0),BR_SCALAR(5.0));
      
// Load Teapot Actor

	planet = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
    planet->model = BrModelLoad("teapot.dat");
	BrModelAdd(planet->model);
	planet->t.type = BR_TRANSFORM_MATRIX34;
	BrMatrix34RotateX(&planet->t.t.mat,BR_ANGLE_DEG(90));  

//Load and Register "earth" Texture
	BrMapAdd(BrPixelmapLoad("earth15.pix"));

//Load and Apply Earth Material
	planet_material = BrFmtScriptMaterialLoad("earth.mat");
	BrMaterialAdd(planet_material);
	planet->material = BrMaterialFind("earth_map");

//************************** Set up callback *********************** 
 
thiscallback=(br_primitive_cbfn*) CBFnPrimitive; 
oldcallback=BrZsPrimitiveCallbackSet((br_primitive_cbfn*)thiscallback);

//************************** Animation Loop ************************                                                            
  while (!kbhit())
        {

        BrPixelmapFill(back_buffer,0);
        BrZsSceneRender(world,observer,back_buffer);

BrPixelmapTextF(back_buffer,70,140,65535,BrFontProp7x9,"primitives=%d",prims);
prims=0;

        BrPixelmapDoubleBuffer(screen_buffer,back_buffer);
        BrMatrix34PostRotateY(&planet->t.t.mat,BR_ANGLE_DEG(2.0));

	}

// Close down

	BrPixelmapFree(back_buffer);
    BrZsEnd();
	DOSGfxEnd();
	BrEnd();


}



