micycle1 / processing-core-4

Processing 4 core as a Maven artifact via JitPack
36 stars 10 forks source link

P3D render lead to NPE on start #8

Closed rlg-pro closed 1 year ago

rlg-pro commented 1 year ago

Given

A maven project for java 17 with following attributes:

Project structure:

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── example
    │   │           └── Main.java
    │   └── resources
    └── test
        └── java

pom.xml content:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.github.micycle1</groupId>
            <artifactId>processing-core-4</artifactId>
            <version>4.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>org.example.Main</Main-Class>
                                        <Build-Number>1.0</Build-Number>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>
</project>-

The creation of an uber-jar is here to ease the tests.

Content of org.example.Main.java

package org.example;

import processing.core.PApplet;

public class Main extends PApplet {

    public void settings(){

        size(500,500, P3D);

    }

    public void draw(){
        translate(width/2,height/2,-100);
        rotateY(PI/3);
        box(75);
    }

    public static void main(String[] args) {

        PApplet.runSketch(new String[]{"test"}, new Main());

    }
}

When

compile and run

cd $PROJECT_HOME
mvn clean install
java -jar target/my-project-1.0-SNAPSHOT.jar

Should

have a drawing of a cube and no error in the console

But

get a blank canvas and the following stack:

java.lang.NullPointerException: Cannot invoke "java.net.URL.openStream()" because "url" is null
    at processing.opengl.PJOGL.loadVertexShader(PJOGL.java:576)
    at processing.opengl.PJOGL.loadVertexShader(PJOGL.java:537)
    at processing.opengl.PGraphicsOpenGL.getPolyShader(PGraphicsOpenGL.java:7054)
    at processing.opengl.PGraphicsOpenGL.flushPolys(PGraphicsOpenGL.java:2343)
    at processing.opengl.PGraphicsOpenGL.flush(PGraphicsOpenGL.java:2280)
    at processing.opengl.PGraphicsOpenGL.endDraw(PGraphicsOpenGL.java:1447)
    at processing.core.PApplet.handleDraw(PApplet.java:2111)
    at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:840)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
    at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
    at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:787)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:453)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.base/java.util.TimerThread.mainLoop(Timer.java:566)
    at java.base/java.util.TimerThread.run(Timer.java:516)

Workaround to create 3D scene despite this problem.

Create the following folders in the src/main/resource folder :

cd $PROJECT_HOME
cd src/main/resources
mkdir -p processing/opengl/shaders

Extract in this folder the glsl files located at the root of the compiled uber-jar :

cd $PROJECT_HOME
cd src/main/resources/processing/opengl/shaders
jar_path=$PROJECT_HOME/target/my-project-1.0-SNAPSHOT.jar
jar -xf "$jar_path" $(jar -tf "$jar_path" | grep -E "^\w+\.glsl")

Now recompile and run the project

cd $PROJECT_HOME
mvn clean install
java -jar target/my-project-1.0-SNAPSHOT.jar

Now a cube is drawn on the canvas without any error in the console.

Hope this help.

micycle1 commented 1 year ago

Now that's thorough!

However I find that I do not get this error and P3D seemingly works fine.

@TidensBarn Can you check too?

image

micycle1 commented 1 year ago

Ok, maybe creating a fat-jar is the problem. Do you get the same error running as a project as a java app from the IDE?

rlg-pro commented 1 year ago

I get the same stack when running it directly in IntelliJ.

idea-error

The jar of processing-core-4.4.2.jar has no shaders folder in processing.opengl.

All the shaders are at the root of the archive.

This jar has been downloaded on my machine the 22 march.

It’s sha1 is : 27951e7caaf4dac3f0191d9e7632a12e10daf344

TidensBarn commented 1 year ago

There's a commit I made to fix precisely this issue, but it's not included in the 4.2 release. @micycle1 Could you move the 4.2 tag up to include it?

In the meantime, referencing it directly instead of v4.2 should get you the correct jar:

<dependency>
    <groupId>com.github.micycle1</groupId>
    <artifactId>processing-core-4</artifactId>
    <version>d33cae501e</version> 
</dependency>

Direct download: processing-core-4-d33cae501e.zip

rlg-pro commented 1 year ago

The new release work OK ! Thanks for your reactivity.