/*
 * Copyright (c) 1996 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Prelit Revolving Illuminated Blue Teapot

Modified by Ady from brtutr5b.c (a tutorial demo) to show Niks prelighting.
Prelit demo 1.

Interesting bit - lines 62 - 87.
*/

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


void main()
{
   br_pixelmap *screen_buffer, *back_buffer, *depth_buffer, *palette;
   br_actor *world, *observer, *teapot;
   br_material *teapot_material;
   int i,count;

/*************** Initialize Renderer and Set Screen Resolution *************/
	BrBegin();
    screen_buffer = DOSGfxBegin("MCGA");
	palette = BrPixelmapLoad("std.pal");
	if(palette)
		DOSGfxPaletteSet(palette);
	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);
	 
/************************ 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 Position teapot Model
    teapot = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
        teapot->model=BrModelLoad("teapot.dat");
        teapot->model->flags|=BR_MODF_UPDATEABLE;
        BrModelAdd(teapot->model);

        teapot->t.type = BR_TRANSFORM_MATRIX34;
    BrMatrix34RotateY(&teapot->t.t.mat,BR_ANGLE_DEG(30));  
  
// Load and Apply teapot's Material
    teapot_material = BrFmtScriptMaterialLoad("teapot8.mat");
    BrMaterialAdd(teapot_material);
    teapot->material = BrMaterialFind("BLUE MATERIAL");

/*******************************************************************/
/**************************** prelit bit ***************************/                                                              
/*******************************************************************/                                                              

// This initally displays model

		BrPixelmapFill(back_buffer,0);
		BrPixelmapFill(depth_buffer,0xFFFFFFFF);
        BrZbSceneRender(world,observer,back_buffer,depth_buffer);
		BrPixelmapDoubleBuffer(screen_buffer,back_buffer);

// Sets prelit flag 
                teapot_material->flags|=BR_MATF_PRELIT;
// Unsets light flag
                teapot_material->flags&=~BR_MATF_LIGHT;

BrSceneModelLight(teapot->model,teapot_material,world,teapot);

// Updates material and model after flag change
                BrMaterialUpdate(teapot_material,BR_MATU_ALL);
                BrModelUpdate(teapot->model,BR_MODU_ALL);

// Start rotating
while (!kbhit()){}; getch();

/*******************************************************************/
// Redisplays prelite model

while (!kbhit())
        {
		BrPixelmapFill(back_buffer,0);
		BrPixelmapFill(depth_buffer,0xFFFFFFFF);
		BrZbSceneRender(world,observer,back_buffer,depth_buffer);
		BrPixelmapDoubleBuffer(screen_buffer,back_buffer);
        BrMatrix34PostRotateX(&teapot->t.t.mat,BR_ANGLE_DEG(10));
        }

while (!kbhit()) {} getch();

/*******************************************************************/

// Close down
	BrPixelmapFree(depth_buffer);
	BrPixelmapFree(back_buffer);
	BrZbEnd();
	DOSGfxEnd();
	BrEnd();
}


			      
