/*   EXTRAITS DE LA LICENCE
	Copyright CEA, contributeurs : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)
  
	Adresse mèl :
	BILLARD, non joignable par mèl ;
	CALISTE, damien P caliste AT cea P fr.

	Ce logiciel est un programme informatique servant à visualiser des
	structures atomiques dans un rendu pseudo-3D. 

	Ce logiciel est régi par la licence CeCILL soumise au droit français et
	respectant les principes de diffusion des logiciels libres. Vous pouvez
	utiliser, modifier et/ou redistribuer ce programme sous les conditions
	de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA 
	sur le site "http://www.cecill.info".

	Le fait que vous puissiez accéder à cet en-tête signifie que vous avez 
	pris connaissance de la licence CeCILL, et que vous en avez accepté les
	termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
*/

/*   LICENCE SUM UP
	Copyright CEA, contributors : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)

	E-mail address:
	BILLARD, not reachable any more ;
	CALISTE, damien P caliste AT cea P fr.

	This software is a computer program whose purpose is to visualize atomic
	configurations in 3D.

	This software is governed by the CeCILL  license under French law and
	abiding by the rules of distribution of free software.  You can  use, 
	modify and/ or redistribute the software under the terms of the CeCILL
	license as circulated by CEA, CNRS and INRIA at the following URL
	"http://www.cecill.info". 

	The fact that you are presently reading this means that you have had
	knowledge of the CeCILL license and that you accept its terms. You can
	find a copy of this licence shipped with this software at Documentation/licence.en.txt.
*/

#include "renderingMode.h"

#include <visu_tools.h>
#include <visu_configFile.h>

#include <GL/gl.h>
#include <GL/glu.h>

#include <string.h>


#define FLAG_PARAMETER_OPENGL_RENDERING "opengl_render"
#define DESC_PARAMETER_OPENGL_RENDERING "Rules the way OpenGl draws objects ; 3 possible strings : Wireframe, Flat or Smooth"
#define PARAMETER_OPENGL_RENDERING_DEFAULT Smooth

RenderingModeId renderingOption;
gboolean readOpenGLRendering(gchar **lines, int nbLines,
			     int position, GString *errorMessage);
gboolean exportParametersRenderingMode(GString *data, int *nbLinesWritten);

static const char *renderingStrings[nb_renderingModes + 1] = {"Wireframe", "Flat", "Smooth",
/* 							      "SmoothAndEdge", (const char*)0}; */
							      (const char*)0};
static const char *renderingStringsI18n[nb_renderingModes + 1];


void openGLInit_renderingMode()
{
  VisuConfigFileEntry *confEntry;

  DBG_fprintf(stderr, "OpenGl RenderingMode : initialization.\n");
  confEntry = visuConfigFileAdd_entry(VISU_CONFIGFILE_PARAMETER,
				      FLAG_PARAMETER_OPENGL_RENDERING,
				      DESC_PARAMETER_OPENGL_RENDERING,
				      1, readOpenGLRendering);
  visuConfigFileAdd_exportFunction(VISU_CONFIGFILE_PARAMETER,
				   exportParametersRenderingMode);

  renderingOption = PARAMETER_OPENGL_RENDERING_DEFAULT;
  renderingStringsI18n[Wireframe] = _("Wireframe");
  renderingStringsI18n[Flat     ] = _("Flat");
  renderingStringsI18n[Smooth   ] = _("Smooth");
/*   renderingStringsI18n[3] = _("Smooth & edge"); */
  renderingStringsI18n[nb_renderingModes] = (const char*)0;
}

/* This function change the value of the parameter renderingOption. */
void openGLApply_renderingMode(RenderingModeId mode)
{
  switch (mode)
    {
    case Wireframe:
      glShadeModel(GL_FLAT);
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      break;
    case Flat:
      glShadeModel(GL_FLAT);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      break;
    case Smooth:
/*     case SmoothAndEdge: */
      glShadeModel(GL_SMOOTH);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      break;
    default:
      g_warning("Wrong value for parameter 'mode' in a call"
		" to 'openGLApply_renderingMode'.");
      return;
    }
  DBG_fprintf(stderr, "Rendering Mode : switch rendering mode to '%s'.\n",
	      renderingStrings[mode]);
}
gboolean openGLSet_globalRenderingMode(RenderingModeId value)
{
  g_return_val_if_fail(value < nb_renderingModes, FALSE);
  
  if (value == renderingOption)
    return FALSE;

  renderingOption = value;
  openGLApply_renderingMode(value);

  return 1;
}
/* This function retrieve the value of the parameter renderingOption. */
RenderingModeId openGLGet_globalRenderingOption()
{
  return renderingOption;
}
const char** openGLGet_allRenderingModesI18n()
{
  return renderingStringsI18n;
}

gboolean readOpenGLRendering(gchar **lines, int nbLines,
			     int position, GString *errorMessage)
{
  int res;
  int i;

  res = 0;
  lines[0] = g_strstrip(lines[0]);
  if (!lines[0][0])
    {
      if (errorMessage)
	g_string_append_printf(errorMessage, _("WARNING! Parse error at line %d,"
					       " 1 string value must appear"
					       " after the %s markup.\n"),
			       position, FLAG_PARAMETER_OPENGL_RENDERING);
      openGLSet_globalRenderingMode(PARAMETER_OPENGL_RENDERING_DEFAULT);
      res = 1;
    }
  else
    {
      i = 0;
      while (i < 3 && strcmp(lines[0], renderingStrings[i])) i++;
      if (i == 3)
	{
	  if (errorMessage)
	    g_string_append_printf(errorMessage, _("WARNING! Unknown value (%s) for the"
						   " '%s' parameter at line %d.\n"),
				   lines[0], FLAG_PARAMETER_OPENGL_RENDERING, position);
	  openGLSet_globalRenderingMode(PARAMETER_OPENGL_RENDERING_DEFAULT);
	  res = 1;
	}
      else
	openGLSet_globalRenderingMode(i);
    }
  
  if (res)
    return FALSE;
  else
    return TRUE;
}
gboolean exportParametersRenderingMode(GString *data, int *nbLinesWritten)
{
  g_string_append_printf(data, "# %s\n", DESC_PARAMETER_OPENGL_RENDERING);
  g_string_append_printf(data, "%s: %s\n\n", FLAG_PARAMETER_OPENGL_RENDERING,
	  renderingStrings[renderingOption]);
  *nbLinesWritten = 3;
  return TRUE;
}
