Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

260
LINES

< > BotCompany Repo | #1035573 // OpenGL Demo, updating with new Jogamp

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 11440K of libraries. Click here for Pure Java version (270L/3K).

1  
!7
2  
3  
set flag OtherOpenGL.
4  
lib 1400583 // Jogamp
5  
6  
!include once #1035519 // OpenGL Include
7  
8  
/**
9  
 * <pre>
10  
 *   __ __|_  ___________________________________________________________________________  ___|__ __
11  
 *  //    /\                                           _                                  /\    \\
12  
 * //____/  \__     __ _____ _____ _____ _____ _____  | |     __ _____ _____ __        __/  \____\\
13  
 *  \    \  / /  __|  |     |   __|  _  |     |  _  | | |  __|  |     |   __|  |      /\ \  /    /
14  
 *   \____\/_/  |  |  |  |  |  |  |     | | | |   __| | | |  |  |  |  |  |  |  |__   "  \_\/____/
15  
 *  /\    \     |_____|_____|_____|__|__|_|_|_|__|    | | |_____|_____|_____|_____|  _  /    /\
16  
 * /  \____\                       http://jogamp.org  |_|                              /____/  \
17  
 * \  /   "' _________________________________________________________________________ `"   \  /
18  
 *  \/____.                                                                             .____\/
19  
 * </pre>
20  
 *
21  
 * <p>
22  
 * JogAmp JOGL OpenGL ES 2 graph nurbs demo to expose and learn how to use the graph API to draw nurbs.
23  
 *
24  
 * Inside the main JOGL source tree we have the "graph" API that is what we consider the *best* way to render nurbs on all GPU's using a patent free shaders implementation.
25  
 * Graph is suitable for both desktop and mobile GPU processors.
26  
 *
27  
 * In a nutshell the JogAmp Graph API enable you to define nurbs shapes
28  
 * Outline → OutlineShapes → GLRegion
29  
 * and then render the shapes using a Renderer
30  
 * RegionRenderer
31  
 * TextRenderer (same as RegionRender with Helper methods for texts and fonts.)
32  
 *
33  
 * outline.addVertex(x, y, z, w, onCurve);
34  
 * outlineShape.addOutline(outline);
35  
 * region = GLRegion.create(outlineShape, getRenderModes());
36  
 * region.render(gl, outlineShape,...);
37  
 *
38  
 * The graph API is using the math by Rami Santina introduced in 2011
39  
 * https://jogamp.org/doc/gpunurbs2011/p70-santina.pdf
40  
 * https://jogamp.org/doc/gpunurbs2011/graphicon2011-slides.pdf
41  
 *
42  
 * The best documentation for the graph API is found in the JOGL junit tests
43  
 * http://jogamp.org/git/?p=jogl.git;a=tree;f=src/test/com/jogamp/opengl/test/junit/graph;hb=HEAD
44  
 *
45  
 * and javadoc for Outline and OutlineShape .. and all classes i mentioned above..
46  
 * https://www.jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/graph/geom/Outline.html
47  
 * https://www.jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/graph/curve/OutlineShape.html
48  
 * </p>
49  
 *
50  
 * @author Xerxes Rånby (xranby)
51  
 */
52  
53  
    static Animator animator;
54  
55  
    public static void main(String[] args) {
56  
57  
        // Enable JOGL debugging of GLSL shader compilation and GL calls
58  
        System.setProperty( "jogl.debug.GLSLCode", "");
59  
        System.setProperty( "jogl.debug.DebugGL", "");
60  
61  
        GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2ES2));
62  
        caps.setAlphaBits(4);
63  
      GLWindow glWindow = GLWindow.create(caps);
64  
        glWindow.setSize(800,400);
65  
        glWindow.setTitle("JogAmp JOGL Graph API nurbs demo");
66  
        glWindow.setVisible(true);
67  
68  
        glWindow.addGLEventListener(new GraphNurbs() /* GLEventListener */);
69  
70  
        animator = new Animator();
71  
        animator.add(glWindow);
72  
        animator.start();
73  
    }
74  
75  
    private static class GraphNurbs implements GLEventListener{
76  
77  
        // these will define a shape that is defined once at init
78  
        OutlineShape outlineShape;
79  
        RenderState renderState;
80  
        RegionRenderer regionRenderer;
81  
        GLRegion glRegion;
82  
83  
        // these will define a shape that is updated dynamically for each frame
84  
        OutlineShape dynamicOutlineShape;
85  
        RenderState dynamicRenderState;
86  
        RegionRenderer dynamicRegionRenderer;
87  
        GLRegion dynamicGlRegion;
88  
89  
        volatile float weight = 1.0f;
90  
91  
        final float zNear = 0.1f, zFar = 7000f;
92  
93  
        /* 2nd pass texture size antialias SampleCount
94  
           4 is usually enough */
95  
        private final int[] sampleCount = new int[] { 4 };
96  
97  
        /* variables used to update the PMVMatrix before rendering */
98  
        private float xTranslate = -40f;
99  
        private float yTranslate =  0f;
100  
        private float zTranslate = -100f;
101  
        private float angleRotate = 0f;
102  
103  
        private final int renderModes = Region.VARWEIGHT_RENDERING_BIT;
104  
105  
        @Override
106  
        public void init(GLAutoDrawable drawable) {
107  
108  
            final GL2ES2 gl = drawable.getGL().getGL2ES2();
109  
            gl.setSwapInterval(1);
110  
            gl.glEnable(GL.GL_DEPTH_TEST);
111  
            gl.glEnable(GL.GL_BLEND);
112  
            gl.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
113  
114  
             /* initialize OpenGL specific classes that know how to render the graph API shapes */
115  
            renderState = RenderState.createRenderState(SVertex.factory());
116  
            // define a colour to render our shape with
117  
            renderState.setColorStatic(1.0f, 1.0f, 1.0f, 1.0f);
118  
            renderState.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED);
119  
120  
            /* use the generic graph API to define a shape
121  
             * we use the renderState getVertexFactory
122  
             * to automatically store all vertex data on the GPU
123  
             **/
124  
            outlineShape = new OutlineShape(renderState.getVertexFactory());
125  
126  
            // Here i add some points off curve causing nurbs bends
127  
            outlineShape.addEmptyOutline();
128  
            outlineShape.addVertex(0.0f,-10.0f, true);
129  
            outlineShape.addVertex(17.0f,-10.0f, true);
130  
            outlineShape.addVertex(11.0f,5.0f, /* onCurve */false);
131  
            outlineShape.addVertex(17.0f,10.0f, true);
132  
            outlineShape.addVertex(7.0f,15.0f, /* onCurve */ false);
133  
            outlineShape.addVertex(6.0f,8.0f, /* onCurve */false);
134  
            outlineShape.addVertex(0.0f,10.0f,true);
135  
            outlineShape.closeLastOutline(true);
136  
137  
            // Here i add all points on curve == straight lines
138  
            float offset = 30;
139  
            outlineShape.addEmptyOutline();
140  
            outlineShape.addVertex(offset+0.0f,-10.0f, true);
141  
            outlineShape.addVertex(offset+17.0f,-10.0f, true);
142  
            outlineShape.addVertex(offset+11.0f,5.0f, true);
143  
            outlineShape.addVertex(offset+16.0f,10.0f, true);
144  
            outlineShape.addVertex(offset+7.0f,15.0f, true);
145  
            outlineShape.addVertex(offset+6.0f,8.0f, true);
146  
            outlineShape.addVertex(offset+0.0f,10.0f, true);
147  
            outlineShape.closeLastOutline(true);
148  
149  
            regionRenderer = RegionRenderer.create(renderState, /* GLCallback */ RegionRenderer.defaultBlendEnable, /* GLCallback */ RegionRenderer.defaultBlendDisable);
150  
151  
            glRegion = GLRegion.create(/* RenderModes */ renderModes, /* TextureSequence */ null);
152  
            glRegion.addOutlineShape(outlineShape, null, glRegion.hasColorChannel() ? renderState.getColorStatic(new float[4]) : null);
153  
154  
155  
            /* initialize OpenGL specific classes that know how to render the graph API shapes */
156  
            dynamicRenderState = RenderState.createRenderState(SVertex.factory());
157  
            // define a RED colour to render our shape with
158  
            dynamicRenderState.setColorStatic(1.0f, 0.0f, 0.0f, 1.0f);
159  
            dynamicRenderState.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED);
160  
161  
            dynamicRegionRenderer = RegionRenderer.create(dynamicRenderState, /* GLCallback */ RegionRenderer.defaultBlendEnable, /* GLCallback */ RegionRenderer.defaultBlendDisable);
162  
163  
            // we will fill the OutlineShape dynamically in display
164  
            dynamicOutlineShape = new OutlineShape(dynamicRenderState.getVertexFactory());
165  
166  
            dynamicGlRegion = GLRegion.create(/* RenderModes */ renderModes, /* TextureSequence */ null);
167  
168  
        }
169  
170  
        @Override
171  
        public void dispose(GLAutoDrawable drawable) {
172  
            final GL2ES2 gl = drawable.getGL().getGL2ES2();
173  
            //stop the animator thread when user close the window
174  
            animator.stop();
175  
            // it is important to free memory allocated no the GPU!
176  
            // this memory cant be garbage collected by the JVM
177  
            regionRenderer.destroy(gl);
178  
            glRegion.destroy(gl);
179  
            dynamicGlRegion.destroy(gl);
180  
        }
181  
182  
        @Override
183  
        public void display(GLAutoDrawable drawable) {
184  
            final GL2ES2 gl = drawable.getGL().getGL2ES2();
185  
186  
            // use JogAmp high resolution timer for smooth animations!
187  
            double time = com.jogamp.common.os.Platform.currentTimeMicros();
188  
            float sinusAnimation = (float) (Math.sin(time/100000f));
189  
            float sinusAnimationRotate = (float) (Math.sin(time/1000000f));
190  
191  
            // clear screen
192  
            gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
193  
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
194  
195  
            // the RegionRenderer PMVMatrix define where we want to render our shape
196  
            final PMVMatrix pmv = regionRenderer.getMatrix();
197  
            pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
198  
            pmv.glLoadIdentity();
199  
            pmv.glTranslatef(xTranslate, yTranslate, zTranslate);
200  
            pmv.glRotatef(angleRotate+ 10f * sinusAnimationRotate, 0, 0, 1);
201  
202  
            if( weight != regionRenderer.getRenderState().getWeight() ) {
203  
                regionRenderer.getRenderState().setWeight(weight);
204  
            }
205  
206  
            // Draw the static shape using RegionRenderer and GLRegion
207  
            regionRenderer.enable(gl, true);
208  
            glRegion.draw(gl, regionRenderer, sampleCount);
209  
            regionRenderer.enable(gl, false);
210  
211  
212  
            float offset = 60;
213  
214  
            // We will now update the dynamic shape that changes on each frame
215  
            // I will animate the off curve points
216  
            dynamicOutlineShape.clear();
217  
            dynamicOutlineShape.addVertex(offset + 0.0f,-10.0f, true);
218  
            dynamicOutlineShape.addVertex(offset + 17.0f,-10.0f, true);
219  
            dynamicOutlineShape.addVertex(offset + 11.0f +5 * sinusAnimation,5.0f + 5 * sinusAnimation, /* onCurve */false);
220  
            dynamicOutlineShape.addVertex(offset + 17.0f,10.0f, true);
221  
            dynamicOutlineShape.addVertex(offset + 7.0f + 5 * sinusAnimation,15.0f + 5 * sinusAnimation, /* onCurve */ false);
222  
            dynamicOutlineShape.addVertex(offset + 6.0f ,8.0f , true);
223  
            dynamicOutlineShape.addVertex(offset + 0.0f,10.0f, true);
224  
            dynamicOutlineShape.closeLastOutline(true);
225  
226  
            // the RegionRenderer PMVMatrix define where we want to render our shape
227  
            final PMVMatrix dynamicPmv = dynamicRegionRenderer.getMatrix();
228  
            dynamicPmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
229  
            dynamicPmv.glLoadIdentity();
230  
            dynamicPmv.glTranslatef(xTranslate, yTranslate, zTranslate);
231  
            dynamicPmv.glRotatef(angleRotate+ 10f * sinusAnimationRotate, 0, 0, 1);
232  
233  
            if( weight != dynamicRegionRenderer.getRenderState().getWeight() ) {
234  
                dynamicRegionRenderer.getRenderState().setWeight(weight);
235  
            }
236  
237  
            // when changing the OutlineShape dynamically it is very important that you clear the GPU from old data
238  
            dynamicGlRegion.clear(gl);
239  
            // here we upload the new dynamically created data to the GPU
240  
            dynamicGlRegion.addOutlineShape(dynamicOutlineShape, null, glRegion.hasColorChannel() ? renderState.getColorStatic(new float[4]) : null);
241  
242  
             // Draw the dynamic shape using RegionRenderer and GLRegion
243  
            dynamicRegionRenderer.enable(gl, true);
244  
            dynamicGlRegion.draw(gl, dynamicRegionRenderer, sampleCount);
245  
            dynamicRegionRenderer.enable(gl, false);
246  
        }
247  
248  
        @Override
249  
        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
250  
            final PMVMatrix pmv = regionRenderer.getMatrix();
251  
            regionRenderer.reshapePerspective(45.0f, width, height, zNear, zFar);
252  
            pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
253  
            pmv.glLoadIdentity();
254  
255  
            final PMVMatrix dynamicPmv = dynamicRegionRenderer.getMatrix();
256  
            dynamicRegionRenderer.reshapePerspective(45.0f, width, height, zNear, zFar);
257  
            dynamicPmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
258  
            dynamicPmv.glLoadIdentity();
259  
        }
260  
    }

Author comment

Began life as a copy of #1035520

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): mowyntqkapby, mqqgnosmbjvj, ubadkicnhrcp

No comments. add comment

Snippet ID: #1035573
Snippet name: OpenGL Demo, updating with new Jogamp
Eternal ID of this version: #1035573/1
Text MD5: 0dd218d1844a0b01f049b64f5ccb8851
Transpilation MD5: c042e57336993b0a3adaca8e0346e3fd
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-17 14:31:29
Source code size: 12570 bytes / 260 lines
Pitched / IR pitched: No / No
Views / Downloads: 230 / 750
Referenced in: [show references]