We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I'm trying to convert this sketch (from the PixelfFlow library) to Python but I'm having difficulties understanding some part of it. Especially the following part:
// callback for scene display (used in GBAA)
DwSceneDisplay scene_display = new DwSceneDisplay() {
@ Override
public void display(PGraphics3D canvas) {
displayScene(canvas);
}
};
Would love your help !
Answers
Although Python doesn't have an exact syntax for it, there are easy workarounds. #:-S
We can define an inner subclass 1st, and then instantiate it next, rather than both actions at the same statement: :ar!
That's assumed you're converting it to a ".py" file. L-)
If it's a ".pyde" file, replace
self.displayScene(canvas)
w/ justdisplayScene(canvas)
The variable self (by convention) refers to the current instance of subclass DepthOfField_Demo.
But if you write it as a regular ".pyde" file, cut off that DepthOfField_Demo wrapper completely. >-)
BtW, I had to do something like that here: :ar!
https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
But for Java's class TimerTask instead of PixelFlow's class DwSceneDisplay. :>
@GoToLoop -- Thanks a ton for all the information and clarifications.
I might ask for help on this thread again as I'm not sure to be able to fully convert the Java sketch.
I'm struggling again with the following snippet:
...
It seems that each function is being declared with itself in argument (?!):
pg_render = DwUtils.changeTextureSize(this, pg_render, ...);
Also I'm not sure to understand why the RESIZED variable (a boolean) is instantiated with curly braces.
Would really appreciate your help @GoToLoop
Not declared, assigned. They are declared up top, here:
So during assignment, a
PGraphics3D
object is passed toDwUtils.changeTextureSize()
, which returns aPGraphics3D
, and the returned object is assigned to the variablepg_render
.Assigning a variable using itself as an argument is really common; you do it all the time.
Re:
This is just declaring the members of an array on initialization. It is an array of booleans with one member.
For example:
et cetera.
For some odd reasons, a PGraphics object can't use its resize() method: [-X
https://Processing.org/reference/PImage_resize_.html
A PGraphics needs to be cloned as an actual vanilla PImage via its method get(), so resize() can be finally used on it: :-B
https://Processing.org/reference/PImage_get_.html
After that, another PGraphics needs to be created w/ those new dimensions via createGraphics(): :-\"
https://Processing.org/reference/createGraphics_.html
Then as a final step, transfer the rescaled PImage's pixels[] to the new PGraphics's pixels[] via arrayCopy(): :-bd
https://Processing.org/reference/arrayCopy_.html
That's why the passed PGraphics variable needs to be reassigned to the new rescaled PGraphics returned by changeTextureSize(); so it now points to the modified PGraphics rather than its previous 1. :bz
In short, b/c a PGraphics object can't be mutated in situ for rescaling purposes, those changes need to be transferred to another PGraphics. #:-S
And its variable needs to be reassigned to that new PGraphics if we want that to point to it. 8-X
@jeremydouglass @GoToLoop Thank you both of you for the very comprehensive explanations.
As far as I understand a python transcription of the snippet would roughly look like this:
in setup():
in draw():
class():
The problems I'm facing:
GL2.GL_RGBA16F, GL2.GL_RGBA, GL2.GL_FLOAT
doesn't seem to work.geombuffer.update(pg_render)
doesn't work either and I guess it's because I didn't declaredpg_render
correctly (because there's no such thing asPGraphics3D pg_render
in Python)Have you imported the interface GL2 from package com.jogamp.opengl before using its constants? :-\"
http://JogAmp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/GL2.html
import com.jogamp.opengl.GL2;
-->from com.jogamp.opengl import GL2
PGraphics3D is the declared datatype which variable pg_render accepts to be assigned w/ under Java.
Under Python however, variables hold any datatype reference. No restriction at all! 8->
Under Java, all reference datatype fields are initialized w/
null
by default. ~O)Under Python, simply assign them w/
None
in order to have the same default behavior. :\">PGraphics3D pg_render;
-->pg_render = None
Take a look at the link below in order to learn the default initial value for each Java datatype: ~O)
https://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Thank you for pointing out this omission. Unfortunately, I'm still stuck on line 43:
for some reason
geombuffer.update(pg_render)
prevents the sketch from running correctly. I suspectpg_render
not to be correctly instantiated...I wonder if you 1st was able to transpile back the ".java" sketch as a ".pde" in order to run it under Java Mode? :-?
package
keyword.public class DepthOfField_Demo extends PApplet {
statement & its corresponding closing curly brace.public static void main(String args[]) {
code block. #:-SThat whole code is æsthetically messy. But here's what I've come w/ as a ".pde" file w/o changing the original ".java" 1 much: :P
"Depth_of_Field.pde":
@GoToLoop Apologies for the late reply. Wow, I don't know how to thank you for all the work and time you've dedicated to this .pde transcription, really. It's going to be very helpful for my own .pyde transcription. I'll let you know when I'm done working on this DoF sketch.
I'm back with another question, sorry. I still have a persistent issue with the
resizeScreen()
function and more particularly with thepg_render
andpg_tmp
grahics buffers variables.When running the .pyde script I get the following error messages:
GammaCorrection.apply shader | GL_ERROR: invalid operation
GammaCorrection.apply | GL_ERROR: invalid operation
GammaCorrection.apply | GL_ERROR: invalid framebuffer operation
This occurs at line 110 (Java script) or 58 (Python version):
DwFilter.get(context).gamma.apply(pg_render, pg_render)
Do you know what could be the problem here ?
Here below, for comparison purposes:
Java
Python
In other words, and to make it easier to understand...
In the working Java sketch I have something like this:
Python version
However, although the Python version looks similar to the Java script, it doesn't work (Processing suddenly quits).
When I add:
global pg_render, pg_dof, pg_tmp
just belowdef resizeScreen()
the script runs but then I get the errors I mentionned before:GammaCorrection.apply shader | GL_ERROR: invalid operation
GammaCorrection.apply | GL_ERROR: invalid operation
GammaCorrection.apply | GL_ERROR: invalid framebuffer operation
Would love your insight @GoToLoop
I really would like some help with that Python script... it's been weeks i'm working on that Depth of Field effect without finding a proper solution. Could you give me a hand finding what's wrong with the .pyde script ?
Hi @solub! It took a long time, but I've just finished refactoring "Depth_of_Field.pde". #:-S
Now that I've reorganized this sketch, I'm ready to convert it to a ".pyde" file. :)>-
Just w8 a lil' more. Stay w/ the latest ".pde" file for now: :-\"
From the bottom of my heart... Thank You
Here's my Python Mode attempt. However, it's not working yet, it's just pitch black! :-&
This statement crashes the sketch:
geombuffer.update(pg_render)
within resizeScreen(). Even when following exactly what it does, it doesn't seem to render anything. :o3https://GitHub.com/diwi/PixelFlow/blob/master/src/com/thomasdiewald/pixelflow/java/render/skylight/DwScreenSpaceGeometryBuffer.java#L67-L79
So I'm at loss about it! Sorry. X_X Try finding help on diwi or jdf. ^#(^
This is the exact problem I was facing/mentionning.
I should be the one apologizing for bringing you down in this mess. Once again thank you for all the effort and time you've spent to help me, I'm truly grateful to you.
For now I'm giving up on this sketch and going to try to find another way to create a DoF effect.
Just something I've just found out (v1.4.1+): L-)
If we hit the SHIFT key, we switch for the alt_render PGraphics3D geombuffer.pg_geom.
It kinda "works". At least it's visible rather than the pitch-black pg_render 1. :-\"