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

257
LINES

< > BotCompany Repo | #1035520 // OpenGL Demo, shortened (works on Linux & Windows, except with Linux+JDK18)

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

Author comment

Began life as a copy of #1034074

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035520
Snippet name: OpenGL Demo, shortened (works on Linux & Windows, except with Linux+JDK18)
Eternal ID of this version: #1035520/2
Text MD5: 901f7a4a79d1de30cf0637bceefba886
Transpilation MD5: be411a84c596f3a46f8711e0c82dee2e
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-10 16:45:20
Source code size: 12522 bytes / 257 lines
Pitched / IR pitched: No / No
Views / Downloads: 282 / 825
Version history: 1 change(s)
Referenced in: [show references]