/*
 * Copyright (C) 1997-2005, R3vis Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
 * or visit http://www.gnu.org/copyleft/gpl.html.
 *
 * Contributor(s):
 *   Wes Bethel, R3vis Corporation, Marin County, California
 *
 * The OpenRM project is located at http://openrm.sourceforge.net/.
 */

/* 
 * $Id: lines2d.c,v 1.13 2005/06/08 18:34:16 wes Exp $
 * $Revision: 1.13 $
 * $Name: OpenRM-1-6-0-2-c $
 * $Log: lines2d.c,v $
 * Revision 1.13  2005/06/08 18:34:16  wes
 * Code cleanup to eliminate compiler warnings.
 *
 * Revision 1.12  2003/07/20 14:56:57  wes
 * Minor tweaks to fix compile warnings on Solaris.
 *
 * Revision 1.11  2003/04/13 18:13:23  wes
 * Updated copyright dates.
 *
 * Revision 1.10  2003/01/27 05:07:07  wes
 * Changes to RMpipe initialization sequence APIs. Tested for GLX, but not WGL.
 *
 * Revision 1.9  2003/01/16 22:22:45  wes
 * Updated all source files to reflect new organization of header files: all
 * headers that were formerly located in include/rmaux, include/rmv and
 * include/rmi are now located in include/rm.
 *
 * Revision 1.8  2002/06/17 00:39:40  wes
 * Updated copyright line.
 *
 * Revision 1.7  2001/07/15 22:33:19  wes
 * Added rmPipeDelete to the end of all demo progs. For those that use
 * an initfunc, added a new RMnode * parm (which is unused, except for rm2screen).
 *
 * Revision 1.6  2001/06/03 19:44:05  wes
 * Add calls to new rmaux routines to handle window resize events, and
 * for keyboard event handling.
 *
 * Revision 1.5  2001/03/31 16:55:18  wes
 * Added procmode.h, which defines an RMpipe processing mode used in
 * most demonstration programs. The default processing mode is
 * RM_PIPE_MULTISTAGE_VIEW_PARALLEL.
 *
 * Revision 1.4  2000/12/02 17:24:32  wes
 * Version 1.4.0-alpha-1 checkin. See the RELEASENOTES file for
 * a summary of changes. With this checkin, these demo programs
 * are no longer compatible with versions of the OpenRM API that
 * are pre-1.4.0.
 *
 * Revision 1.3  2000/08/28 01:38:18  wes
 * Updated rmaux* interfaces - rmauxEventLoop now takes two additional
 * parameters (keypress and window resize app callbacks); replaced
 * rmauxUI with rmauxSetGeomTransform and, where appropriate,
 * rmauxSetCamera3DTransform.
 *
 * Revision 1.2  2000/04/20 18:04:34  wes
 * Minor tweaks and reorg for OpenRM 1.2.1.
 *
 * Revision 1.1  2000/03/13 02:40:18  wes
 * Initial entry.
 *
 */

#include <rm/rm.h>   
#include <rm/rmaux.h>
#include "procmode.h"

#define DEFAULT_ASPECT_MULTIPLIER .6

static RMnode *MyRoot;
int img_width=800,img_height=480;

#define CZERO  0.1
#define CONE   0.3
#define CTWO   0.5
#define CTHREE 0.7
#define CFOUR  0.9

#define HHW 0.05

typedef struct
{
    RMvertex2D p[2];
    RMenum lineStyle, lineWidth;
} lineStruct;

#define NUM_LINE_SEGS 3*5	/* 3 weights * 5 styles */

lineStruct lines[NUM_LINE_SEGS] =
{
    /* lines of NARROW width, all aligned in a vertical column */
    {
	{{CONE-HHW, CFOUR}, {CONE+HHW, CFOUR}},
	RM_LINES_SOLID, RM_LINEWIDTH_NARROW /* narrow solid line */
    },
    {
	{{CONE-HHW, CTHREE}, {CONE+HHW, CTHREE}},
	RM_LINES_DASHED, RM_LINEWIDTH_NARROW /* narrow dashed line */
    },
    {
	{{CONE-HHW, CTWO}, {CONE+HHW, CTWO}},
	RM_LINES_DOTTED, RM_LINEWIDTH_NARROW /* narrow dotted line */
    },
    {
	{{CONE-HHW, CONE}, {CONE+HHW, CONE}},
	RM_LINES_DOT_DASH, RM_LINEWIDTH_NARROW /* narrow dot-dash line */
    },
    {
	{{CONE-HHW, CZERO}, {CONE+HHW, CZERO}},
	RM_LINES_DASH_DASH_DOT, RM_LINEWIDTH_NARROW /* narrow dash-dash-dot line */
    },
	    /* medium weight lines */
    {
	{{CTWO-HHW, CFOUR}, {CTWO+HHW, CFOUR}},
	RM_LINES_SOLID, RM_LINEWIDTH_MEDIUM /* MEDIUM solid line */
    },
    {
	{{CTWO-HHW, CTHREE}, {CTWO+HHW, CTHREE}},
	RM_LINES_DASHED, RM_LINEWIDTH_MEDIUM /* MEDIUM dashed line */
    },
    {
	{{CTWO-HHW, CTWO}, {CTWO+HHW, CTWO}},
	RM_LINES_DOTTED, RM_LINEWIDTH_MEDIUM /* MEDIUM dotted line */
    },
    {
	{{CTWO-HHW, CONE}, {CTWO+HHW, CONE}},
	RM_LINES_DOT_DASH, RM_LINEWIDTH_MEDIUM /* MEDIUM dot-dash line */
    },
    {
	{{CTWO-HHW, CZERO}, {CTWO+HHW, CZERO}},
	RM_LINES_DASH_DASH_DOT, RM_LINEWIDTH_MEDIUM /* MEDIUM dash-dash-dot line */
    },

	    /* heavy lines */
    {
	{{CTHREE-HHW, CFOUR}, {CTHREE+HHW, CFOUR}},
	RM_LINES_SOLID, RM_LINEWIDTH_HEAVY /* HEAVY solid line */
    },
    {
	{{CTHREE-HHW, CTHREE}, {CTHREE+HHW, CTHREE}},
	RM_LINES_DASHED, RM_LINEWIDTH_HEAVY /* HEAVY dashed line */
    },
    {
	{{CTHREE-HHW, CTWO}, {CTHREE+HHW, CTWO}},
	RM_LINES_DOTTED, RM_LINEWIDTH_HEAVY /* HEAVY dotted line */
    },
    {
	{{CTHREE-HHW, CONE}, {CTHREE+HHW, CONE}},
	RM_LINES_DOT_DASH, RM_LINEWIDTH_HEAVY /* HEAVY dot-dash line */
    },
    {
	{{CTHREE-HHW, CZERO}, {CTHREE+HHW, CZERO}},
	RM_LINES_DASH_DASH_DOT, RM_LINEWIDTH_HEAVY /* HEAVY dash-dash-dot line */
    }
};

typedef struct
{
    RMvertex2D p;
    char string[48];
} labelStruct;

#define NUM_COLUMN_LABELS 3
#define NUM_ROW_LABELS    5

labelStruct columnLabels[NUM_COLUMN_LABELS] =
{
    {
	{CONE, 1.F}, "RM_LINEWIDTH_NARROW"
    },
    {
	{CTWO, 1.F}, "RM_LINEWIDTH_MEDIUM"
    },
    {
	{CTHREE, 1.F}, "RM_LINEWIDTH_HEAVY"
    }
};

labelStruct rowLabels[NUM_ROW_LABELS] =
{
    {
	{CONE-1.8*HHW, CFOUR}, "RM_LINES_SOLID"
    },
    {
	{CONE-1.8*HHW, CTHREE}, "RM_LINES_DASHED"
    },
    {
	{CONE-1.8*HHW, CTWO}, "RM_LINES_DOTTED"
    },
    {
	{CONE-1.8*HHW, CONE}, "RM_LINES_DOT_DASH"
    },
    {
	{CONE-1.8*HHW, CZERO}, "RM_LINES_DASH_DASH_DOT"
    }
};


void
my_set_scene(RMnode *camNode,
	     int winWidth,
	     int winHeight)
{
    RMcamera2D *c = rmCamera2DNew();;

    rmDefaultCamera2D(c);	/* assign it some default values. */

    /* change the default scene camera from (0,0) to (1,1) */
    rmCamera2DSetExtents(c,0.0F, -0.2F, 1.0F, 1.2F);
    rmCamera2DSetAspectRatio(c, DEFAULT_ASPECT_MULTIPLIER *(float)(winWidth)/(float)(winHeight));

    rmNodeSetSceneCamera2D(camNode,c);
}

int
myResizeFunc(RMpipe *p,
	     RMnode *cameraNode,
	     int winWidth,
	     int winHeight)
{
    RMcamera2D *c2=NULL;
    RMnode *r = cameraNode;
    
    if (rmNodeGetSceneCamera2D (r, &c2) != RM_WHACKED)
    {
	float a;

	a = DEFAULT_ASPECT_MULTIPLIER * ((float)(winWidth) / (float)(winHeight));
	rmCamera2DSetAspectRatio (c2, a);
	rmNodeSetSceneCamera2D (r, c2);
    }
    if (c2 != NULL)
	rmCamera2DDelete(c2);

    /* foil compiler warning */
    p =  NULL;
    
    return(RM_CHILL);
}


void
my_build_objs(void)
{
    int i,limit;
    RMprimitive *tlist;
    RMnode *textRoot;
    RMnode *textNode;
    RMnode *linesRoot;

    MyRoot = rmNodeNew("MyRoot", RM_RENDERPASS_2D, RM_RENDERPASS_OPAQUE);
    textRoot = rmNodeNew("textRoot",RM_RENDERPASS_2D, RM_RENDERPASS_OPAQUE);
    linesRoot = rmNodeNew("linesRoot",RM_RENDERPASS_2D,
			  RM_RENDERPASS_OPAQUE);
    
    rmNodeAddChild(rmRootNode(),MyRoot); 
    rmNodeAddChild(MyRoot,textRoot);
    rmNodeAddChild(MyRoot,linesRoot);

    limit = NUM_LINE_SEGS;
    for (i=0;i<limit;i++)
    {
	RMnode *n;
	RMprimitive *p;
	char buf[32];

	sprintf(buf,"line-%d",i);
	n = rmNodeNew(buf,RM_RENDERPASS_2D,RM_RENDERPASS_OPAQUE);

	p = rmPrimitiveNew(RM_LINES);
	rmPrimitiveSetVertex2D(p,2,lines[i].p,RM_COPY_DATA,NULL);
	rmNodeAddPrimitive(n,p);
	rmNodeSetLineStyle(n,lines[i].lineStyle);
	rmNodeSetLineWidth(n,lines[i].lineWidth);
	rmNodeAddChild(linesRoot, n);
    }

    /*
     * for all the column headers, we have to set node-level text properties
     * to declare horizontal centering for justification, and use a smaller-
     * than-default font size.
     */
    limit = NUM_COLUMN_LABELS;
    for (i=0;i<limit;i++) 
    {
	char name[32];
	char *strings[1];
	RMtextProps *tp;
	RMcolor4D c={1.0F, 1.0F, 0.5F, 1.0F};

	sprintf(name,"text-%d",i);
	
	textNode = rmNodeNew(name,RM_RENDERPASS_2D,RM_RENDERPASS_OPAQUE);
	rmNodeSetUnlitColor(textNode, &c);
	tlist = rmPrimitiveNew(RM_TEXT);
	rmNodeAddPrimitive(textNode, tlist);
	rmNodeAddChild(textRoot, textNode);
	
	rmPrimitiveSetVertex2D(tlist,1,
			       &(columnLabels[i].p), /* pointer to stuff */
			       RM_COPY_DATA, /* let RM manage this data */
			       NULL); /* don't need a freefunc for RM_COPY_DATA */
	
	strings[0] = columnLabels[i].string;
	rmPrimitiveSetText(tlist,1,strings);

	tp = rmTextPropsNew();
	rmTextPropsSetAttribs(tp,
			      RM_FONT_SANS, /* font family */
			      RM_FONT_XXS, /* size */
			      RM_FALSE,	/* embolden? */
			      RM_FALSE,	/* italicize? */
			      RM_CENTER, /* horiz justification */
			      RM_BOTTOM); /* vertical justification */
	
	rmNodeSetSceneTextProps(textNode, tp);
	rmTextPropsDelete(tp);
    }
    
    /*
     * for all the column headers, we have to set node-level text properties
     * to declare horizontal centering for justification, and use a smaller-
     * than-default font size.
     */
    limit = NUM_ROW_LABELS;
    for (i=0;i<limit;i++) 
    {
	char name[32];
	char *strings[1];
	RMtextProps *tp;
	RMcolor4D c={0.5F, 1.0F, 0.5F, 1.0F};

	sprintf(name,"text-%d",i);
	
	textNode = rmNodeNew(name,RM_RENDERPASS_2D,RM_RENDERPASS_OPAQUE);
	rmNodeSetUnlitColor(textNode,&c);
	tlist = rmPrimitiveNew(RM_TEXT);
	rmNodeAddPrimitive(textNode, tlist);
	rmNodeAddChild(textRoot, textNode);
	
	rmPrimitiveSetVertex2D(tlist,1,
			       &(rowLabels[i].p), /* pointer to stuff */
			       RM_COPY_DATA, /* let RM manage this data */
			       NULL); /* don't need a freefunc for RM_COPY_DATA */
	
	strings[0] = rowLabels[i].string;
	rmPrimitiveSetText(tlist,1,strings);

	tp = rmTextPropsNew();
	rmTextPropsSetAttribs(tp,
			      RM_FONT_SANS, /* font family */
			      RM_FONT_XXS, /* size */
			      RM_FALSE,	/* embolden? */
			      RM_FALSE,	/* italicize? */
			      RM_RIGHT, /* horiz justification */
			      RM_BOTTOM); /* vertical justification */
	
	rmNodeSetSceneTextProps(textNode, tp);
	rmTextPropsDelete(tp);
    }
    
#if 0
    /*
     * set text justification parameter, controls justification for all
     * text prims at the given node.
     */

    {
        RMvertex3D bmin,bmax,center;

        rmNodeUnionAllBoxes(rmRootNode()); 

	rmNodeGetBoundingBox(rmRootNode(),&bmin,&bmax);
	center.x = bmin.x + 0.5F *(bmax.x - bmin.x);
	center.y = bmin.y + 0.5F *(bmax.y - bmin.y);
	center.z = bmin.z + 0.5F *(bmax.z - bmin.z);

	rmNodeSetCenter(rmRootNode(),&center);
    }
#endif
    {
	RMcolor4D bgcolor={0.2,0.2,0.3,1.0};
	/*
	 * assign a background color to take effect at "MyRoot"
	 */
	rmNodeSetSceneBackgroundColor(MyRoot,&bgcolor);
    }
}

void
myinitfunc(RMpipe *p,
	   RMnode *n)
{
    int winWidth, winHeight;
    
    my_build_objs();

    rmPipeGetWindowSize(p, &winWidth, &winHeight);
    my_set_scene(MyRoot, winWidth, winHeight);
    
    rmauxUI(n, p);

    /*
     * set handler to reset aspect ratio when the window is resized.
     */
    rmauxSetResizeFunc(p, MyRoot, myResizeFunc);

    if (rmPipeProcessingModeIsMultithreaded(p) == RM_TRUE)
	rmFrame(p, n);
    
    rmFrame(p, n);
}


#ifdef RM_WIN
int WINAPI WinMain (HINSTANCE hInstance,
		    HINSTANCE hPrevInstance,
                    LPSTR lpszCmdLine, int nCmdShow)
{
    MSG      msg; 
    HWND     hWnd; 
    void *fptr;
    RMenum targetPlatform = RM_PIPE_WGL;
#else  /* assume RM_X */
int
main()
{
    void *msg;			/* needed for rmauxEventLoop
				 win32/unix API consistency */
    RMenum targetPlatform = RM_PIPE_GLX;
#endif
    RMpipe *lone_pipe=NULL;
    RMenum processingMode = DEFAULT_PROCESSING_MODE; /* in procmode.h */

    /* 
     * first stage of RM initialization.
     */
    rmInit();

    /* 
     * create the rendering pipe. this step is required in both
     * Win32 and X.
     */
    lone_pipe = rmPipeNew(targetPlatform);
    
    rmPipeSetProcessingMode(lone_pipe, processingMode);
    
#ifdef RM_WIN
    {
        /*
	 * Win32: when a window is created, we have to tell windows the
	 * name of the "WndProc," the procedure that gets called by
	 * windows with events (the event loop) (contrast to the X model 
	 * where the name of the event loop is not part of the window). 
	 * Since we're using RMaux, we know about the event handling 
	 * procedure named "rmauxWndProc" and we provide that here. 
	 */

        fptr = (void *)(rmauxWndProc);
	hWnd = rmauxCreateW32Window(lone_pipe,
			       NULL, /* no parent window */
			       20,20,img_width,img_height,"OpenRM - Linestyles and widths",
			       hInstance,fptr);
	if (hWnd == 0)
	  exit(-1);

	/* 
	 * assign the new window handle to the rendering pipe.
	 */
	rmPipeSetWindow(lone_pipe,hWnd, img_width, img_height);
    }
#endif
    
#ifdef RM_X
    {
	Window w;

	w = rmauxCreateXWindow(lone_pipe,
			       (Window)NULL, /* parent window */
			       0,0,img_width,img_height,
			       "OpenRM - Linestyles and widths","icon-title",1);
	/* 
	 * assign the window to the rendering pipe.
	 */
	rmPipeSetWindow(lone_pipe,w,img_width,img_height);
    }
#endif
    /* 
     * specify the name of the "init" function. the "init" function is
     * mandatory in the Win32 world, and optional in the X world. 
     *
     * in Win32, we don't want to call RM services until OpenGL is
     * ready. we can be assured of readiness by using an init function
     * with RMaux. 
     *
     * in X, at this point, the window is mapped and OpenGL is ready,
     * and we could call our init function directly.
     */

    rmauxSetInitFunc(myinitfunc); 

    /* 
     * X-ism: once the window is created and assigned to the 
     * rendering pipe, rmUsePipe makes the OpenGL rendering context
     * current for the pipe+window combination. 
     *
     * this step is required for X. in these demo programs, it is not 
     * strictly required by Win32, as the newly created context is made
     * current as part of the OpenGL initialization sequence.
     */
    rmPipeMakeCurrent(lone_pipe);	

    /*
     * set key handler function so this prog will exit on "q" key
     */
    rmauxSetKeyFunc(lone_pipe, rmauxDefaultKeyFunc);
    
    rmauxEventLoop(lone_pipe,rmRootNode(), &msg);

    rmPipeDelete(lone_pipe);
    rmFinish();

#ifdef RM_WIN
    return( msg.wParam );
#else
    return(1);
#endif
}
