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

187
LINES

< > BotCompany Repo | #1035523 // OpenGL_DemoObjects

JavaX fragment (include) [tags: use-pretranspiled]

Uses 9219K of libraries. Click here for Pure Java version (785L/5K).

1  
!include once #1035519 // OpenGL Include
2  
3  
srecord noeq OpenGL_DemoObjects(Animator animator) is GLEventListener{
4  
    // these will define a shape that is defined once at init
5  
    OutlineShape outlineShape;
6  
    RenderState renderState;
7  
    RegionRenderer regionRenderer;
8  
    GLRegion glRegion;
9  
10  
    // these will define a shape that is updated dynamically for each frame
11  
    OutlineShape dynamicOutlineShape;
12  
    RenderState dynamicRenderState;
13  
    RegionRenderer dynamicRegionRenderer;
14  
    GLRegion dynamicGlRegion;
15  
16  
    volatile float weight = 1.0f;
17  
18  
    final float zNear = 0.1f, zFar = 7000f;
19  
20  
    /* 2nd pass texture size antialias SampleCount
21  
       4 is usually enough */
22  
    private final int[] sampleCount = new int[] { 4 };
23  
24  
    /* variables used to update the PMVMatrix before rendering */
25  
    private float xTranslate = -40f;
26  
    private float yTranslate =  0f;
27  
    private float zTranslate = -100f;
28  
    private float angleRotate = 0f;
29  
30  
    private final int renderModes = Region.VARWEIGHT_RENDERING_BIT;
31  
32  
    @Override
33  
    public void init(GLAutoDrawable drawable) {
34  
35  
        final GL2ES2 gl = drawable.getGL().getGL2ES2();
36  
        gl.setSwapInterval(1);
37  
        gl.glEnable(GL.GL_DEPTH_TEST);
38  
        gl.glEnable(GL.GL_BLEND);
39  
        gl.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
40  
41  
         /* initialize OpenGL specific classes that know how to render the graph API shapes */
42  
        renderState = RenderState.createRenderState(SVertex.factory());
43  
        // define a colour to render our shape with
44  
        renderState.setColorStatic(1.0f, 1.0f, 1.0f, 1.0f);
45  
        renderState.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED);
46  
47  
        /* use the generic graph API to define a shape
48  
         * we use the renderState getVertexFactory
49  
         * to automatically store all vertex data on the GPU
50  
         **/
51  
        outlineShape = new OutlineShape(renderState.getVertexFactory());
52  
53  
        // Here i add some points off curve causing nurbs bends
54  
        outlineShape.addEmptyOutline();
55  
        outlineShape.addVertex(0.0f,-10.0f, true);
56  
        outlineShape.addVertex(17.0f,-10.0f, true);
57  
        outlineShape.addVertex(11.0f,5.0f, /* onCurve */false);
58  
        outlineShape.addVertex(17.0f,10.0f, true);
59  
        outlineShape.addVertex(7.0f,15.0f, /* onCurve */ false);
60  
        outlineShape.addVertex(6.0f,8.0f, /* onCurve */false);
61  
        outlineShape.addVertex(0.0f,10.0f,true);
62  
        outlineShape.closeLastOutline(true);
63  
64  
        // Here i add all points on curve == straight lines
65  
        float offset = 30;
66  
        outlineShape.addEmptyOutline();
67  
        outlineShape.addVertex(offset+0.0f,-10.0f, true);
68  
        outlineShape.addVertex(offset+17.0f,-10.0f, true);
69  
        outlineShape.addVertex(offset+11.0f,5.0f, true);
70  
        outlineShape.addVertex(offset+16.0f,10.0f, true);
71  
        outlineShape.addVertex(offset+7.0f,15.0f, true);
72  
        outlineShape.addVertex(offset+6.0f,8.0f, true);
73  
        outlineShape.addVertex(offset+0.0f,10.0f, true);
74  
        outlineShape.closeLastOutline(true);
75  
76  
        regionRenderer = RegionRenderer.create(renderState, /* GLCallback */ RegionRenderer.defaultBlendEnable, /* GLCallback */ RegionRenderer.defaultBlendDisable);
77  
78  
        glRegion = GLRegion.create(/* RenderModes */ renderModes, /* TextureSequence */ null);
79  
        glRegion.addOutlineShape(outlineShape, null, glRegion.hasColorChannel() ? renderState.getColorStatic(new float[4]) : null);
80  
81  
82  
        /* initialize OpenGL specific classes that know how to render the graph API shapes */
83  
        dynamicRenderState = RenderState.createRenderState(SVertex.factory());
84  
        // define a RED colour to render our shape with
85  
        dynamicRenderState.setColorStatic(1.0f, 0.0f, 0.0f, 1.0f);
86  
        dynamicRenderState.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED);
87  
88  
        dynamicRegionRenderer = RegionRenderer.create(dynamicRenderState, /* GLCallback */ RegionRenderer.defaultBlendEnable, /* GLCallback */ RegionRenderer.defaultBlendDisable);
89  
90  
        // we will fill the OutlineShape dynamically in display
91  
        dynamicOutlineShape = new OutlineShape(dynamicRenderState.getVertexFactory());
92  
93  
        dynamicGlRegion = GLRegion.create(/* RenderModes */ renderModes, /* TextureSequence */ null);
94  
95  
    }
96  
97  
    @Override
98  
    public void dispose(GLAutoDrawable drawable) {
99  
        final GL2ES2 gl = drawable.getGL().getGL2ES2();
100  
        //stop the animator thread when user close the window
101  
        animator?.stop();
102  
        // it is important to free memory allocated no the GPU!
103  
        // this memory cant be garbage collected by the JVM
104  
        regionRenderer.destroy(gl);
105  
        glRegion.destroy(gl);
106  
        dynamicGlRegion.destroy(gl);
107  
    }
108  
109  
    @Override
110  
    public void display(GLAutoDrawable drawable) {
111  
        final GL2ES2 gl = drawable.getGL().getGL2ES2();
112  
113  
        // use JogAmp high resolution timer for smooth animations!
114  
        double time = com.jogamp.common.os.Platform.currentTimeMicros();
115  
        float sinusAnimation = (float) (Math.sin(time/100000f));
116  
        float sinusAnimationRotate = (float) (Math.sin(time/1000000f));
117  
118  
        // clear screen
119  
        gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
120  
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
121  
122  
        // the RegionRenderer PMVMatrix define where we want to render our shape
123  
        final PMVMatrix pmv = regionRenderer.getMatrix();
124  
        pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
125  
        pmv.glLoadIdentity();
126  
        pmv.glTranslatef(xTranslate, yTranslate, zTranslate);
127  
        pmv.glRotatef(angleRotate+ 10f * sinusAnimationRotate, 0, 0, 1);
128  
129  
        if( weight != regionRenderer.getRenderState().getWeight() ) {
130  
            regionRenderer.getRenderState().setWeight(weight);
131  
        }
132  
133  
        // Draw the static shape using RegionRenderer and GLRegion
134  
        regionRenderer.enable(gl, true);
135  
        glRegion.draw(gl, regionRenderer, sampleCount);
136  
        regionRenderer.enable(gl, false);
137  
138  
139  
        float offset = 60;
140  
141  
        // We will now update the dynamic shape that changes on each frame
142  
        // I will animate the off curve points
143  
        dynamicOutlineShape.clear();
144  
        dynamicOutlineShape.addVertex(offset + 0.0f,-10.0f, true);
145  
        dynamicOutlineShape.addVertex(offset + 17.0f,-10.0f, true);
146  
        dynamicOutlineShape.addVertex(offset + 11.0f +5 * sinusAnimation,5.0f + 5 * sinusAnimation, /* onCurve */false);
147  
        dynamicOutlineShape.addVertex(offset + 17.0f,10.0f, true);
148  
        dynamicOutlineShape.addVertex(offset + 7.0f + 5 * sinusAnimation,15.0f + 5 * sinusAnimation, /* onCurve */ false);
149  
        dynamicOutlineShape.addVertex(offset + 6.0f ,8.0f , true);
150  
        dynamicOutlineShape.addVertex(offset + 0.0f,10.0f, true);
151  
        dynamicOutlineShape.closeLastOutline(true);
152  
153  
        // the RegionRenderer PMVMatrix define where we want to render our shape
154  
        final PMVMatrix dynamicPmv = dynamicRegionRenderer.getMatrix();
155  
        dynamicPmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
156  
        dynamicPmv.glLoadIdentity();
157  
        dynamicPmv.glTranslatef(xTranslate, yTranslate, zTranslate);
158  
        dynamicPmv.glRotatef(angleRotate+ 10f * sinusAnimationRotate, 0, 0, 1);
159  
160  
        if( weight != dynamicRegionRenderer.getRenderState().getWeight() ) {
161  
            dynamicRegionRenderer.getRenderState().setWeight(weight);
162  
        }
163  
164  
        // when changing the OutlineShape dynamically it is very important that you clear the GPU from old data
165  
        dynamicGlRegion.clear(gl);
166  
        // here we upload the new dynamically created data to the GPU
167  
        dynamicGlRegion.addOutlineShape(dynamicOutlineShape, null, glRegion.hasColorChannel() ? renderState.getColorStatic(new float[4]) : null);
168  
169  
         // Draw the dynamic shape using RegionRenderer and GLRegion
170  
        dynamicRegionRenderer.enable(gl, true);
171  
        dynamicGlRegion.draw(gl, dynamicRegionRenderer, sampleCount);
172  
        dynamicRegionRenderer.enable(gl, false);
173  
    }
174  
175  
    @Override
176  
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
177  
        final PMVMatrix pmv = regionRenderer.getMatrix();
178  
        regionRenderer.reshapePerspective(45.0f, width, height, zNear, zFar);
179  
        pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
180  
        pmv.glLoadIdentity();
181  
182  
        final PMVMatrix dynamicPmv = dynamicRegionRenderer.getMatrix();
183  
        dynamicRegionRenderer.reshapePerspective(45.0f, width, height, zNear, zFar);
184  
        dynamicPmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
185  
        dynamicPmv.glLoadIdentity();
186  
    }
187  
}

Author comment

Began life as a copy of #1035520

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035523
Snippet name: OpenGL_DemoObjects
Eternal ID of this version: #1035523/2
Text MD5: 134f9b128767b65b11aefca9e5b35fa2
Transpilation MD5: 5c1872d9b66439a401e37713807916c0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-02 08:53:02
Source code size: 8698 bytes / 187 lines
Pitched / IR pitched: No / No
Views / Downloads: 160 / 222
Version history: 1 change(s)
Referenced in: [show references]