/*   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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <X11/Xlib.h>
#include <GL/glx.h>

#include <glib.h>

#include "visu_openGL.h"
#include <visu_tools.h>

struct DumpImage_struct
{
  Display *dpy;
  GLXContext context;
  GLXPixmap glxPixmap;
  Pixmap pixmap;
  guint width, height;
};

static XVisualInfo *vinfo = (XVisualInfo*)0;

XVisualInfo* visuOpenGLGet_visualInfo(Display *dpy, int screenId)
{
  int list[] = {
    GLX_RGBA,
    GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
    GLX_DEPTH_SIZE, 1,
    GLX_DOUBLEBUFFER,
    GLX_STEREO,
    None
  };

  if (!vinfo)
    {
      if ( (vinfo = glXChooseVisual(dpy, screenId, list)) == NULL )
	{
	  list[10] = None; 
	  if ( (vinfo = glXChooseVisual(dpy, screenId, list)) == NULL )
	    g_error("Cannot find a visual.\n"
		    "Have you enough right access on the X server?");
	}
    }

  DBG_fprintf(stderr, "Visu GLX : context visualID %d.\n", (int)vinfo->visualid);
  return vinfo;
}

DumpImage* visuOpenGLNew_pixmapContext(guint width, guint height)
{
  DumpImage *image;
  int screenId, res;
  XVisualInfo *visualInfo;

  DBG_fprintf(stderr, "Visu GLX : creating a off-screen buffer (%dx%d).\n", width, height);
  image = g_malloc(sizeof(DumpImage));
  image->dpy = (Display*)0;
  image->pixmap = (Pixmap)0;
  image->glxPixmap = (GLXPixmap)0;
  image->context = (GLXContext)0;

  image->dpy = XOpenDisplay(0);
  if (!image->dpy)
    {
      g_warning("Cannot connect to the X server.");
      g_free(image);
      return (DumpImage*)0;
    }
  DBG_fprintf(stderr, "Visu GLX : display opened.\n");

  screenId = DefaultScreen(image->dpy);
  visualInfo = visuOpenGLGet_visualInfo(image->dpy, screenId);

  image->width = width;
  image->height = height;
  image->pixmap = XCreatePixmap(image->dpy, RootWindow(image->dpy, screenId), width, height, visualInfo->depth);
  if (!image->pixmap)
    {
      g_warning("Cannot allocate a XPixmap for the indirect rendering.");
      XCloseDisplay(image->dpy);
      g_free(image);
      return (DumpImage*)0;
    }

  image->glxPixmap = glXCreateGLXPixmap(image->dpy, visualInfo, image->pixmap);
  if (!image->glxPixmap)
    {
      g_warning("Cannot allocate a GLXPixmap for the indirect rendering.");
      XFreePixmap(image->dpy, image->pixmap);
      XCloseDisplay(image->dpy);
      g_free(image);
      return (DumpImage*)0;
    }

  image->context = glXCreateContext(image->dpy, visualInfo, 0, GL_FALSE);
  if (!image->context)
    {
      g_warning("Cannot create indirect GLX context.");
      XFreePixmap(image->dpy, image->pixmap);
      glXDestroyGLXPixmap(image->dpy, image->glxPixmap);
      XCloseDisplay(image->dpy);
      g_free(image);
      return (DumpImage*)0;
    }

  res = glXMakeCurrent(image->dpy, image->glxPixmap, image->context);
  if (!res)
    {
      g_warning("Cannot make current the pixmap context.");
      XFreePixmap(image->dpy, image->pixmap);
      glXDestroyGLXPixmap(image->dpy, image->glxPixmap);
      XCloseDisplay(image->dpy);
      g_free(image);
      return (DumpImage*)0;
    }

  return image;
}

void visuOpenGLFree_pixmapContext(DumpImage *dumpData)
{
  g_return_if_fail(dumpData);

  if (!dumpData->dpy)
    {
      g_free(dumpData);
      return;
    }

  if (dumpData->pixmap)
    XFreePixmap(dumpData->dpy, dumpData->pixmap);
  if (dumpData->glxPixmap)
    glXDestroyGLXPixmap(dumpData->dpy, dumpData->glxPixmap);
  if (dumpData->context)
    glXDestroyContext(dumpData->dpy, dumpData->context);
  XCloseDisplay(dumpData->dpy);
  g_free(dumpData);
}

GLuint visuOpenGLinit_fontList()
{
  XFontStruct *fontInfo; 
  unsigned int first, last; 
  char name[] =  
    "-adobe-helvetica-medium-r-normal--18-180-75-75-p-98-iso8859-1"; 
  GLuint BASE;
  Display *dpy;
        
  DBG_fprintf(stderr, "Visu GLX : initialise fonts.\n");
  dpy = XOpenDisplay(0);
  if ((fontInfo = XLoadQueryFont(dpy, name)) == NULL) 
    {
      g_warning("Specified font not available in gl_font_init\n" 
		"Trying to use fixed font\n"); 
      if ((fontInfo = XLoadQueryFont(dpy, "fixed")) == NULL) 
	g_error("Fixed font not available.\n"); 
    }

  first = fontInfo->min_char_or_byte2; 
  last = fontInfo->max_char_or_byte2; 
 
  /* this creates display lists 1 to 256 (not checked) */ 
  if ( (BASE = glGenLists(last+1)) == 0 ) 
    return BASE;
    
  glXUseXFont(fontInfo->fid, first, last-first+1, BASE+first); 
 
  return BASE; 
}


syntax highlighted by Code2HTML, v. 0.9.1