We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Pages: 1 2 
Reusing PGraphicsGL (Read 2590 times)
Reusing PGraphicsGL
Dec 5th, 2005, 11:26pm
 
Hi all,

I think this is the right forum to ask this question. It's about a library I'm starting to write for Processing, but my question is more related to OpenGL in Processing.

My library is intended to import X3D xml files and convert them to 3D models. The models will be drawn using OpenGL. So far Im just in the preliminary coding stages and have a question on the best way to do the above;

My library will obviously need to draw to the PApplet, so, my basic class is created something like the following:

Code:

Ex3d my_model = new Ex3d(this); // this being the PApplet


However, since Im going to be using OpenGL, and OpenGL would be required I would think to have the user import the processing opengl libraries, and reuse its gl object, like so:

Code:

import processing.opengl.*;
import ex3d.*;

....

size(200,200,OPENGL);
Ex3d my_model = new Ex3d(this, ((PGraphicsGL)g).gl);


Now I'm not certain if this is the best way to approach this, I was just wondering if anyone has any suggestions on continuing this way. Or is there a simpler way I'm missing?

Cheers Smiley
Re: Reusing PGraphicsGL
Reply #1 - Dec 6th, 2005, 1:46am
 
Hi,

I'm at the point of finishing a library that imports vector images and holds them in Processing objects.
It's a bit like yours but for 2D stuff.

I ran in to the same problem as you, and I chosed the following solution.

When we want to draw a model (or in my case a 2D vector image) what we really need to specify is what PGraphics object to draw it to.

So instead of your code I used:

Code:

Ex3D modelo = new Ex3D(filename);
modelo.draw(g);


this would be the case we want to draw our model in the applet's default graphics object or canvas.  But it would also allow us to draw our image in any graphics object we want, which I think gives us is more flexibility.  And I don't think this really is an overhead to newbies, since in any of the ways they must remember something (in your case to pass "this" as parameter and in my case to pass "g" as parameter).

This means when creating your library class you'll need to have a function that looks like the following:

Code:

import processing.core*;

public class Ex3D{

public void draw(PGraphics gfx){
// change colors, call smooth, draw tristrips or whatever, maybe something like this...
gfx.beginShape(TRI_STRIP);
// gfx.vertex() the points of your model ...
gfx.endShape();
// change back the colors to normal...
}


I wouldn't call any specific methods of OPENGL as a requirement.  I would rather add those inside a "try catch statement" or by seeing if the gfx object passed as argument is of type PGraphicsGL.  And would try to keep the option of loading and drawing 3D models with the other 3D renderers.  Because maybe someday someone writes a 3D recorder and this would still be compatible with the Processing API. In my opinion this would give longer life and wider use to your library.

But of course these are just suggestions and any library you make is more than welcome!!!

thanks
ricard
Re: Reusing PGraphicsGL
Reply #2 - Dec 6th, 2005, 12:56pm
 
I think most libraries use the 'new Ex3D(this)' format, so if you can use similar, it may help users by having a consistent interface, btu it's by no means required.

((PGraphicsGL)g).gl is actually an object of type GL (as found in net.java.games.jogl.*) not a PGraphicsGL, as g on itself is the PGraphicsGL or possibly P3D or Java2D.

The only error the end user would see if trying to use your code with P3D (and not loading the opengl library) would be that PGraphicsGL is an unknown type, which isn't all that instructive. And if they did load the OpenGL library, but still used P3D in size, then you'd be none the wiser that the library was expecting GL.

What you can do however, is in your library startup, check what type of object 'g' is, and throw an exception with a useful error if it's not a PGraphicsGL.

e.g.:
Code:

package jimdaddy.Ex3D

import processing.core.*;
import processing.opengl.*;

public class Ex3D
{
PApplet parent;
PGraphicsGL g;
Ex3D(PApplet _parent)
{
parent=_parent;
if(!(g instanceof PGraphicsGL))
{
throw new RuntimeException("Ex3D requires OpenGL");
}
g=(PGraphicsGL)parent.g;
}

... code to load model etc ...
public void draw()
{
g.beginShape(...);
g.vertex(...);
// etc, etc
}
}

Re: Reusing PGraphicsGL
Reply #3 - Dec 6th, 2005, 4:13pm
 
Hi again,

Quote:
I think most libraries use the 'new Ex3D(this)' format, so if you can use similar, it may help users by having a consistent interface, btu it's by no means required.


I think this is a good method when for example loading files that will be in the data folder. Since without the PApplet we cannot access such path.  So I still think it will be necessary in the Ex3D case.

On the other hand, you just gave me a good idea on what to do with my library.  I think it will be better to have the 'new Whatever(this)' format in all constructors, and have a draw() method without parameters when wanting to draw in the main canvas and a draw(PGraphics gfx) when wanting to draw on a specific canvas, we shouldn't limit the user.

Although a library with many different classes from very primitive to more high-level, it can mean a lot of overhead for the user (e.g. every time we want an instance of a point calling RPoint(this,x,y) can be a little bit cumbersome). I'm not really sure.

I still think that making a library for a specific specific renderer is very limiting, and there's always the possibility of checking (as JohnG says) if the PGraphics is instance of PGraphicsGL every time we want to use specific OpenGL functions, and try to have an alternative for the other renderers, even if the alternative is to throw an exception saying that the alternative is not yet implemented, Wink

Hope this helps
Re: Reusing PGraphicsGL
Reply #4 - Dec 6th, 2005, 4:57pm
 
Hey guys,

cheers for the great feedback. I reckon I understand now how Im going to go about this. A lot of confusion has be cleared up Wink

Unfortunitly, this project will require opengl, and I won't be concentrating on using other types (at least not for the forseeable future) as this is a college project, and I've already written my design documents and don't really want to deviate much. Especially as I also hope to include the ability to apply post-processing effects to the objects. For example, once the object is loaded apply cel shading effects through a sinmple command. I know that doing these post processing effects in OpenGL is hard enough as it is and trying to support other 3D graphics libraries is out of the question Wink

However that doesnt mean I can't alter this at a later stage.

Again thanks for the quick replies Smiley
Re: Reusing PGraphicsGL
Reply #5 - Dec 8th, 2005, 5:25am
 
Hey again,

I've researched this a bit more, and it turns out this library will be built for OpenGL alone. Too many problems would arise if I was to do it and try have all renderers support it, (or at least from what I can tell).

So with your help I made this very simple library to just draw some vertices onto Processing's existing gl object. However it just doesnt seem to do anything. Am I meant to call a redraw method or something similar?

My basic code for the library:
Code:

package ex3d;

/* import core processing libs */
import processing.core.*;
import processing.opengl.*;

/* standard includes */
import net.java.games.jogl.*;
// JOGL

public class Ex3D
{
public PApplet parent;
public GL gl;

public Ex3D(PApplet _parent)
{
parent=_parent;

this.gl = ((PGraphicsGL)parent.g).gl;
}

public void draw()
{
// tried translate here and in P5

this.gl.glTranslatef(1.5f,0.0f,-6.0f);



this.gl.glBegin(GL.GL_QUADS);

this.gl.glColor3f(1.0f,1.0f,-1.0f);

this.gl.glVertex3f(1.0f,-1.0f,-1.0f);

this.gl.glVertex3f(-1.0f,-1.0f,-1.0f);

this.gl.glVertex3f(-1.0f,1.0f,-1.0f);

this.gl.glVertex3f(1.0f,-1.0f,-1.0f);

this.gl.glEnd();

}

// just testing here to see if i needed to reset gl object :/
public void end()
{

((PGraphicsGL)parent.g).gl = this.gl;
}
}


Example call from P5:
Code:

Ex3D my_x3d;

void setup()
{
...
my_x3d = new Ex3D(this);
...
}

void draw()
{
my_x3d.draw();
//my_x3d.end();
}


I basically get a blank window, without the front face of a quad as should appear. I really appreciate any help anyone can give. Cheers Wink
Re: Reusing PGraphicsGL
Reply #6 - Dec 8th, 2005, 1:01pm
 
Unfortunately, you're coming up against the same problem as I did here: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Contribution_3DOpenGL;action=display;num=1133267060

Basicly, if you're using gl.glWhatever(...); it won't work properly, since processing does some matrix munging behind the scenes. you can still use normal processing translate/beginShape/vertex etc, and they'll end up being the OpenGL commands, but you probably won't be able to do it directly.
Re: Reusing PGraphicsGL
Reply #7 - Dec 8th, 2005, 5:02pm
 
Oooook. That's not good news at all. I would have to use OpenGL commands if I was able to apply bump mapping / cel shading effects.

Has anyone any idea what I can do? Using the built in Processing functions will severely drop this projects functionality (and grade) :/

Or is my best chance now to make a new canvas or something? I don't know the core code in processing well enough to know this now, so if anyone has any ideas...
Re: Reusing PGraphicsGL
Reply #8 - Dec 9th, 2005, 3:29pm
 
Some OpenGL commands you may be able to use, e.g. gl.glEnable(...) others, like textures, you may be able to use, but I'm not sure which ones will work, and which won't.
Re: Reusing PGraphicsGL
Reply #9 - Dec 9th, 2005, 11:12pm
 
Having had a bit more thought, here's what I thin can/can't be used.

Anything that would change the position on screen something would be drawn, or glVertex, will either not work, or have an effect different to that you think it should.

Anything that requires any special GL capabilities may or may not work.

Other things, like glTexture may work, if you don't then specify a texture with the processing syntax. And you should be able to mix that with processing beginShape() and vertex etc.
Re: Reusing PGraphicsGL
Reply #10 - Dec 10th, 2005, 2:13pm
 
I believe you might be able to jump over the matrix transformations applied by the processing core by setting the matrices to identity matrices.

Don't knoz exactly how this can be done, but I'll look into it when I get back home.
Re: Reusing PGraphicsGL
Reply #11 - Dec 10th, 2005, 5:10pm
 
using processing as an opengl (jogl) programming environment is likely to be really frustrating, since it's not designed that way, and we're not going to do anything to make it easier to program that way. if you really want to do a lot of opengl-oriented things, i recommend just using jogl with a regular java ide.
Re: Reusing PGraphicsGL
Reply #12 - Dec 15th, 2005, 10:59am
 
a more jogl related thing but nonetheless worth noticing that it is advices not to cache jogl s GL object. i never seen anything crashing because of that but still ...
Jogl - User's Guide / Writing a GLEventListener

hey not even 'PGraphicsGL' is doing it properly Wink
Re: Reusing PGraphicsGL
Reply #13 - Dec 15th, 2005, 11:14am
 
hey ben. that sounds really discouraging, a 'lack of color'. i think it is cool to use opengl in processing and there are a few things that can be done to make it a bit easier as i wrote on another tread. it just still might be an even hotter beverage Wink

i always felt that it was the strength of processing to range from super-simple to hardcore. this notion is always worth encouraging, where possible.

the thing about opengl is that there is quite a lot to be learned about computergraphics and the related math before one can 'really' use it. that is not always obvious with opengl.
Re: Reusing PGraphicsGL
Reply #14 - Dec 15th, 2005, 3:54pm
 
it's a limitation on my time. i don't have time to maintain an entire opengl library that would be compatible that way.

obviously i'd prefer it to work in a way that lets you do opengl directly, but i do this in my free time and there are far more pressing issues.
Pages: 1 2