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

271
LINES

< > BotCompany Repo | #1034074 // OpenGL Demo (works on Linux & Windows)

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

Download Jar. Uses 9219K of libraries. Click here for Pure Java version (312L/4K).

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

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, ekrmjmnbrukm, gwrvuhgaqvyk, iveijnkanddl, mikhejdllvsw, mowyntqkapby, mqqgnosmbjvj, tpturoigtvwk

No comments. add comment

Snippet ID: #1034074
Snippet name: OpenGL Demo (works on Linux & Windows)
Eternal ID of this version: #1034074/4
Text MD5: 43e25fdf253b646160c58f95063c7042
Transpilation MD5: 0258e3c815a90c92127dab286109fdc3
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-21 17:41:50
Source code size: 12988 bytes / 271 lines
Pitched / IR pitched: No / No
Views / Downloads: 330 / 869
Version history: 3 change(s)
Referenced in: [show references]