View previous topic :: View next topic |
Author |
Message |
Uwar
Joined: 10 Feb 2008 Posts: 11 Location: Poland
|
Posted: Thu May 15, 2008 7:35 am Post subject: GL2PS |
|
|
I need to use OpenGL to PostScript printing library with D.
1. I downloaded latest stable verion this.
2. I compiled this with gcc to gl2ps.dll
3. I converted gl2ps.h to gl2ps.d
_____________________________________________________________
gl2ps.d
Code: | /* Converted to D from gl2ps.h by htod */
module gl2ps;
/* $Id: gl2ps.h,v 1.113 2006/11/06 00:48:06 geuzaine Exp $ */
/*
* GL2PS, an OpenGL to PostScript Printing Library
* Copyright (C) 1999-2006 Christophe Geuzaine <geuz@geuz.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of either:
*
* a) the GNU Library General Public License as published by the Free
* Software Foundation, either version 2 of the License, or (at your
* option) any later version; or
*
* b) the GL2PS License as published by Christophe Geuzaine, 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 either
* the GNU Library General Public License or the GL2PS License for
* more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library in the file named "COPYING.LGPL";
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
* Cambridge, MA 02139, USA.
*
* You should have received a copy of the GL2PS License with this
* library in the file named "COPYING.GL2PS"; if not, I will be glad
* to provide one.
*
* For the latest info about gl2ps, see http://www.geuz.org/gl2ps/.
* Please report all bugs and problems to <gl2ps@geuz.org>.
*/
/* #include <opengl/gl.h> */
private import derelict.opengl.gl;
/* #include <stdio.h> */
private import std.c.stdio;
/* #include <windows.h> */
private import std.c.windows.windows;
extern (C):
// version number
//_______________________________________________________________________
enum : uint{
GL2PS_MAJOR_VERSION = 1,
GL2PS_MINOR_VERSION = 3,
GL2PS_PATCH_VERSION = 2
}
// output file formats (the values and the ordering are important!)
//_______________________________________________________________________
enum : uint{
GL2PS_PS = 0,
GL2PS_EPS = 1,
GL2PS_TEX = 2,
GL2PS_PDF = 3,
GL2PS_SVG = 4,
GL2PS_PGF = 5
}
// sorting algorithms
//_______________________________________________________________________
enum : uint{
GL2PS_NO_SORT = 1,
GL2PS_SIMPLE_SORT = 2,
GL2PS_BSP_SORT = 3
}
// message levels and error codes
//_______________________________________________________________________
enum : uint{
GL2PS_SUCCESS = 0,
GL2PS_INFO = 1,
GL2PS_WARNING = 2,
GL2PS_ERROR = 3,
GL2PS_NO_FEEDBACK = 4,
GL2PS_OVERFLOW = 5,
GL2PS_UNINITIALIZED = 6
}
// options for gl2psBeginPage
//_______________________________________________________________________
const GL2PS_NONE = 0;
const GL2PS_DRAW_BACKGROUND = (1<<0);
const GL2PS_SIMPLE_LINE_OFFSET = (1<<1);
const GL2PS_SILENT = (1<<2);
const GL2PS_BEST_ROOT = (1<<3);
const GL2PS_OCCLUSION_CULL = (1<<4);
const GL2PS_NO_TEXT = (1<<5);
const GL2PS_LANDSCAPE = (1<<6);
const GL2PS_NO_PS3_SHADING = (1<<7);
const GL2PS_NO_PIXMAP = (1<<8);
const GL2PS_USE_CURRENT_VIEWPORT= (1<<9);
const GL2PS_COMPRESS = (1<<10);
const GL2PS_NO_BLENDING = (1<<11);
const GL2PS_TIGHT_BOUNDING_BOX = (1<<12);
// arguments for gl2psEnable/gl2psDisable
//_______________________________________________________________________
enum : uint{
GL2PS_POLYGON_OFFSET_FILL = 1,
GL2PS_POLYGON_BOUNDARY = 2,
GL2PS_LINE_STIPPLE = 3,
GL2PS_BLEND = 4
}
// text alignment (o=raster position; default mode is BL):
//_______________________________________________________________________
enum : uint{
/*
* description:
* +---+ +---+ +---+ +---+ +---+ +---+ +-o-+ o---+ +---o
* | o | o | | o | | | | | | | | | | | |
* +---+ +---+ +---+ +-o-+ o---+ +---o +---+ +---+ +---+
* C CL CR B BL BR T TL TR
*/
GL2PS_TEXT_C = 1,
GL2PS_TEXT_CL = 2,
GL2PS_TEXT_CR = 3,
GL2PS_TEXT_B = 4,
GL2PS_TEXT_BL = 5,
GL2PS_TEXT_BR = 6,
GL2PS_TEXT_T = 7,
GL2PS_TEXT_TL = 8,
GL2PS_TEXT_TR = 9
}
alias GLfloat[4] GL2PSrgba;
GLint gl2psBeginPage(char* title, char* producer, GLint* viewport, GLint format, GLint sort, GLint options, GLint colormode, GLint colorsize, GLfloat[4]* colormap, GLint nr, GLint ng, GLint nb, GLint buffersize, FILE* stream, char* filename);
GLint gl2psEndPage();
GLint gl2psSetOptions(GLint options);
GLint gl2psBeginViewport(GLint *viewport);
GLint gl2psEndViewport();
GLint gl2psText(char* str, char* fontname, GLshort fontsize);
GLint gl2psTextOpt(char* str, char* fontname, GLshort fontsize, GLint o_align, GLfloat angle);
GLint gl2psSpecial(GLint format, char* str);
GLint gl2psDrawPixels(GLfloat width, GLfloat height, GLint xorig, GLint yorig, GLenum format, GLenum type, void* pixels);
GLint gl2psEnable(GLint mode);
GLint gl2psDisable(GLint mode);
GLint gl2psPointSize(GLfloat value);
GLint gl2psLineWidth(GLfloat value);
GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor);
// undocumented
GLint gl2psDrawImageMap(GLfloat width, GLfloat height, GLfloat* position, ubyte* imagemap);
char* gl2psGetFileExtension(GLint format);
char* gl2psGetFormatDescription(GLint format); |
_____________________________________________________________
4. I created gl2ps.def
_____________________________________________________________
gl2ps.def
Code: | LIBRARY C:\DPROG\IMPORT\EXT\GL2PS
DESCRIPTION 'C:\DProg\import\ext\gl2ps.dll'
EXETYPE NT
EXPORTS
_gl2psBeginPage = gl2psBeginPage
_gl2psEndPage = gl2psEndPage
_gl2psSetOptions = gl2psSetOptions
_gl2psBeginViewport = gl2psBeginViewport
_gl2psEndViewport = gl2psEndViewport
_gl2psText = gl2psText
_gl2psTextOpt = gl2psTextOpt
_gl2psSpecial = gl2psSpecial
_gl2psDrawPixels = gl2psDrawPixels
_gl2psEnable = gl2psEnable
_gl2psDisable = gl2psDisable
_gl2psPointSize = gl2psPointSize
_gl2psLineWidth = gl2psLineWidth
_gl2psBlendFunc = gl2psBlendFunc |
_____________________________________________________________
5. I created gl2ps.lib
_____________________________________________________________
Code: | implib gl2ps.lib gl2ps.def |
_____________________________________________________________
But code below fail after run.
_____________________________________________________________
Code: | import std.string;
import derelict.sdl.sdl;
import derelict.opengl.gl;
import derelict.opengl.glu;
import gl2ps;
import std.stdio;
// horizontal and vertical screen resolution
const int xResolution = 800;
const int yResolution = 600;
// number of bits per pixel used for display. 24 => true color
const int bitsPerPixel = 24;
// field of view => the angle our camera will see vertically
const float fov = 90.f;
// distance of the near clipping plane
const float nearPlane = .1f;
// distance of the far clipping plane
const float farPlane = 100.f;
/**
Setup some basic OpenGL parameters
*/
void setupGL() {
// switch to the projection mode matrix
glMatrixMode(GL_PROJECTION);
// load the identity matrix for projection
glLoadIdentity();
// setup a perspective projection matrix
gluPerspective(fov, cast(float)xResolution / yResolution, nearPlane, farPlane);
// switch back to the modelview transformation matrix
glMatrixMode(GL_MODELVIEW);
// load the identity matrix for modelview
glLoadIdentity();
}
void init() {
// initialize SDL, GL and GLU Derelict modules
DerelictSDL.load();
DerelictGL.load();
DerelictGLU.load();
// initialize SDL's VIDEO module
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
// enable double-buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// create our OpenGL window
SDL_SetVideoMode(xResolution, yResolution, bitsPerPixel, SDL_OPENGL);
SDL_WM_SetCaption(toStringz("My SDL Window"), null);
setupGL();
}
// be nice and release all resources
void cleanup() {
// tell SDL to quit
SDL_Quit();
// release GL, GLU and SDL's shared libs
DerelictGLU.unload();
DerelictGL.unload();
DerelictSDL.unload();
}
extern (C){
void drawGLFrame() {
FILE *fp;
int state = GL2PS_OVERFLOW, buffsize = 0;
fp = fopen("out.pdf", "wb");
printf("Writing 'out.pdf'... ");
while(state == GL2PS_OVERFLOW){
buffsize = 1024*1024;
gl2psBeginPage("test", "gl2psTestSimple", null, GL2PS_PDF, GL2PS_SIMPLE_SORT,
GL2PS_DRAW_BACKGROUND | GL2PS_USE_CURRENT_VIEWPORT,
GL_RGBA, 0, null, 0, 0, 0, 0, fp, "out.pdf"
);
glBegin(GL_TRIANGLES);
glColor3f (1, 0, 0);
glVertex3f(-1, -1, -2);
glColor3f (0, 1, 0);
glVertex3f(1, -1, -2);
glColor3f (0, 0, 1);
glVertex3f(0, 1, -2);
glEnd();
state = gl2psEndPage();
}
fclose(fp);
printf("Done!\n");
}
}
void main() {
init();
scope(exit) cleanup(); // when we exit, perform cleanup
mainLoop:
while (true) {
SDL_Event event;
// handle all SDL events that we might've received in this loop iteration
while (SDL_PollEvent(&event)) {
switch (event.type) {
// user has clicked on the window's close button
case SDL_QUIT:
break mainLoop;
// by default, we do nothing => break from the switch
default:
break;
}
}
// clear the screen. by default it clears to black
glClear(GL_COLOR_BUFFER_BIT);
// draw our stuff =)
drawGLFrame();
// swap the buffers, making our backbuffer the visible one
SDL_GL_SwapBuffers();
}
} |
_____________________________________________________________
I don't know what is wrong. Similar program in C is OK.
_____________________________________________________________
Code: | /**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
#include <gl2ps.h>
#include <stdio.h>
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
FILE *fp;
int state = GL2PS_OVERFLOW, buffsize = 0;
fp = fopen("out.pdf", "wb");
printf("Writing 'out.eps'... ");
while(state == GL2PS_OVERFLOW){
buffsize += 1024*1024;
gl2psBeginPage("test", "gl2psTestSimple", NULL, GL2PS_PDF, GL2PS_SIMPLE_SORT,
GL2PS_DRAW_BACKGROUND | GL2PS_USE_CURRENT_VIEWPORT,
GL_RGBA, 0, NULL, 0, 0, 0, buffsize, fp, "out.pdf");
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glRotatef (theta, 0.0f, 0.0f, 1.0f);
glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
glEnd ();
glPopMatrix ();
state = gl2psEndPage();
}
fclose(fp);
printf("Done!\n");
SwapBuffers (hDC);
theta += 1.0f;
Sleep (1);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
} |
Can anybody help? |
|
Back to top |
|
|
aldacron
Joined: 05 May 2004 Posts: 1322 Location: Seoul, South Korea
|
Posted: Thu May 15, 2008 8:58 am Post subject: Re: GL2PS |
|
|
Uwar wrote: |
But code below fail after run.
|
Without knowing what you mean by 'fail after run', I doubt you'll find many people willing to take the time to try to debug it for you no matter how much code you post. When reporting errors like this, you need to describe the problem you're having and the command line you're using to compile as a minimum. Also you should at least try to narrow the problem down as much as you can and only post code that appears relevant to the problem, if possible.
So with that in mind, how are you compiling and what do you mean by 'fail after run'? Are you getting an access violation? Some other error? Is nothing happening at all? _________________ The One With D | The One With Aldacron | D Bits |
|
Back to top |
|
|
Uwar
Joined: 10 Feb 2008 Posts: 11 Location: Poland
|
Posted: Thu May 15, 2008 9:30 am Post subject: |
|
|
ok, sorry
It's mean runtime error.
Code: | Writing 'out.pdf'... Error: Access Violation
GL2PS error: gl2psBeginPage called in wrong program state | that's all.
compiling command line:
Code: | C:\DProgramming\dmd\bin\build.exe main -TSDL_GL.exe -DCPATHC:\DProgramming\dmd\bin -IC:\DProg\import;C:\DProg\import\ext C:\DProg\import\ext\gl2ps.lib |
The problem point is gl2psBeginPage function.
Any idea? |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|