Login  Register

Re: Using multiple models in a scene

Posted by ted_gress on Jun 22, 2018; 6:37pm
URL: https://forum.jogamp.org/Using-multiple-models-in-a-scene-tp4038956p4038959.html

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import com.jogamp.common.nio.Buffers;
import com.jogamp.nativewindow.NativeWindowFactory;
import com.jogamp.newt.Display;
import com.jogamp.newt.NewtFactory;
import com.jogamp.newt.Screen;
import com.jogamp.newt.Window;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.FPSCounter;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2ES1;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLPipelineFactory;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.glsl.fixedfunc.FixedFuncUtil;
import com.jogamp.opengl.util.glsl.fixedfunc.ShaderSelectionMode;

public class Renderer implements GLEventListener{
       
                int maxvbonumbers = 20;
       
           int[] vboNames = new int[maxvbonumbers];

           boolean innerCube;

           boolean initialized = false;

           float time = 0.0f;

            static int bufferCount = 0;
           
            static int maxBuffers = 10;
           
           ShortBuffer [] cubeVertices = new ShortBuffer[maxBuffers];

           ShortBuffer [] cubeTexCoords = new ShortBuffer[maxBuffers];

           FloatBuffer [] cubeColors = new FloatBuffer[maxBuffers];

           ByteBuffer [] cubeNormals = new ByteBuffer[maxBuffers];

           ByteBuffer [] cubeIndices = new ByteBuffer[maxBuffers];

           private GLU glu;

           public boolean glDebug = false;

                public boolean glTrace = false;

                boolean quit = false;
               
                static  float[] light_position = { -50.f, 50.f, 50.f, 0.f };

                static  float[] light_ambient = { 0.125f, 0.125f, 0.125f, 1.f };

                static  float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.f };

                static  float[] material_spec = { 1.0f, 1.0f, 1.0f, 0.f };

                static  float[] zero_vec4 = { 0.0f, 0.0f, 0.0f, 0.f };
               
// private static  short[] s_cubeVertices = null;
//
//
//   private static  short[] s_cubeTexCoords = null;
//
//
//
//   private static  float[] s_cubeColors = null;
//
//
//
//   private static  byte[] s_cubeIndices= null;
//
//   private static  byte[] s_cubeNormals = null;
//  
                   static GL2ES1 gl = null;
                   
                   static float aspect;
               
                public void pushData(CubeObject cubeObject, CubeObject2 cubeObject2)
                {
                       
                       
                       
                         
                         if(!innerCube) {

                           System.err.println("Entering initialization");

                           System.err.println("GL Profile: "+gl.getGLProfile());

                           System.err.println("GL:" + gl);

                           System.err.println("GL_VERSION=" + gl.glGetString(gl.GL_VERSION));

                           System.err.println("GL_EXTENSIONS:");

                           System.err.println("  " + gl.glGetString(gl.GL_EXTENSIONS));

                           System.err.println("GLF:" + gl);
                           
                           light_position = cubeObject.light_position;
                           light_ambient = cubeObject.light_ambient;
                           light_diffuse = cubeObject.light_diffuse;
                           material_spec = cubeObject.material_spec;
                           zero_vec4 = cubeObject.zero_vec4;
                           
                           
                           cubeVertices[bufferCount] = cubeObject.cubeVertices;
                      cubeTexCoords[bufferCount] = cubeObject.cubeTexCoords;
                      cubeColors[bufferCount] = cubeObject.cubeColors;
                      cubeNormals[bufferCount] = cubeObject.cubeNormals;
                      cubeIndices[bufferCount] =cubeObject.cubeIndices;
                      bufferCount++;
                      cubeVertices[bufferCount] = cubeObject2.cubeVertices;
                      cubeTexCoords[bufferCount] = cubeObject2.cubeTexCoords;
                      cubeColors[bufferCount] = cubeObject2.cubeColors;
                      cubeNormals[bufferCount] = cubeObject2.cubeNormals;
                      cubeIndices[bufferCount] =cubeObject2.cubeIndices;
                      bufferCount++;
                         }
                }

                         
               
               
                //bind data
                   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

                       aspect = (height != 0) ? ((float)width / (float)height) : 1.0f;


                        gl = drawable.getGL().getGL2ES1();
                       



                       gl.glViewport(0, 0, width, height);



                       gl.glMatrixMode(gl.GL_MODELVIEW);

                       gl.glLoadIdentity();



                       // JAU gl.glScissor(0, 0, width, height);

                       if(innerCube) {

                           // Clear background to white

                           gl.glClearColor(1.0f, 1.0f, 1.0f, 0.4f);

                       } else {

                           // Clear background to blue

                           gl.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

                       }


                       if(!innerCube) {

                           gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, light_position, 0);

                           gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, light_ambient, 0);

                           gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, light_diffuse, 0);

                           gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, zero_vec4, 0);

                           gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, material_spec, 0);



                           gl.glEnable(gl.GL_LIGHTING);

                           gl.glEnable(gl.GL_LIGHT0);

                           gl.glEnable(gl.GL_COLOR_MATERIAL);

                       } else {

                           gl.glDisable(gl.GL_LIGHTING);

                           gl.glDisable(gl.GL_LIGHT0);

                       }

                       gl.glEnable(gl.GL_CULL_FACE);

                       gl.glEnable(gl.GL_NORMALIZE);
                       
                        buffers();
                   }
                   
                   public void buffers()
                   {
                           int vbonums = 0;
                           
                           for (int i=0;i<bufferCount;i++)
                           {
                                   
                                   
                                   gl.glEnableClientState(gl.GL_VERTEX_ARRAY);

                                   gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboNames[vbonums]);

                                   gl.glBufferData(GL.GL_ARRAY_BUFFER, cubeVertices[i].limit() * Buffers.SIZEOF_SHORT, cubeVertices[i], GL.GL_STATIC_DRAW);

                                   gl.glVertexPointer(3, gl.GL_SHORT, 0, 0);

                                   vbonums++;


                                   gl.glEnableClientState(gl.GL_NORMAL_ARRAY);

                                   gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboNames[vbonums]);

                                   gl.glBufferData(GL.GL_ARRAY_BUFFER, cubeNormals[i].limit() * Buffers.SIZEOF_BYTE, cubeNormals[i], GL.GL_STATIC_DRAW);

                                   gl.glNormalPointer(gl.GL_BYTE, 0, 0);

                                   vbonums++;


                                   gl.glEnableClientState(gl.GL_COLOR_ARRAY);

                                   gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboNames[vbonums]);

                                   gl.glBufferData(GL.GL_ARRAY_BUFFER, cubeColors[i].limit() * Buffers.SIZEOF_FLOAT, cubeColors[i], GL.GL_STATIC_DRAW);

                                   gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);

                                   vbonums++;

                                   if (cubeTexCoords != null) {

                                           gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);

                                           gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboNames[vbonums]);

                                           gl.glBufferData(GL.GL_ARRAY_BUFFER, cubeTexCoords[i].limit() * Buffers.SIZEOF_SHORT, cubeTexCoords[i], GL.GL_STATIC_DRAW);

                                           gl.glTexCoordPointer(2, gl.GL_SHORT, 0, 0);

                                           /* issues an GL_INVALID_ENUM

                               gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_INCR);

                           } */
                                           vbonums++;

                                   } else {

                                           gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY);

                                   }

                                   gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);



                                   gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_FASTEST);



                                   gl.glMatrixMode(gl.GL_PROJECTION);

                                   gl.glLoadIdentity();



                                   if(!innerCube) {

                                           glu.gluPerspective(90.0f, aspect, 1.0f, 100.0f);

                                   } else {

                                           gl.glOrthof(-20.0f, 20.0f, -20.0f, 20.0f, 1.0f, 40.0f);

                                   }

                                   // weird effect ..: gl.glCullFace(gl.GL_FRONT);
                           }
                   }


                     
               
                   public Renderer (boolean useTexCoords, boolean innerCube)
                        {
                           
                           
                                 // Initialize data Buffers
                                // store data
                                /*
                               this.cubeVertices = Buffers.newDirectShortBuffer(s_cubeVertices.length);

                               cubeVertices.put(s_cubeVertices);

                               cubeVertices.flip();



                               this.cubeColors = Buffers.newDirectFloatBuffer(s_cubeColors.length);

                               cubeColors.put(s_cubeColors);

                               cubeColors.flip();



                               this.cubeNormals = Buffers.newDirectByteBuffer(s_cubeNormals.length);

                               cubeNormals.put(s_cubeNormals);

                               cubeNormals.flip();



                               this.cubeIndices = Buffers.newDirectByteBuffer(s_cubeIndices.length);

                               cubeIndices.put(s_cubeIndices);

                               cubeIndices.flip();

                               

                               if (useTexCoords) {

                                   this.cubeTexCoords = Buffers.newDirectShortBuffer(s_cubeTexCoords.length);

                                   cubeTexCoords.put(s_cubeTexCoords);

                                   cubeTexCoords.flip();

                             
                               }
                               */
                               
                        }
                   
                   public void init(GLAutoDrawable drawable) {

                           //initialize gl flags
                           
                       GL _gl = drawable.getGL();



                       _gl.glGetError(); // flush error ..



                       if(glDebug) {

                           try {

                               _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", null, _gl, null) );

                           } catch (Exception e) {e.printStackTrace();}

                       }



                       if(glTrace) {

                           try {

                               _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", null, _gl, new Object[] { System.err } ) );

                           } catch (Exception e) {e.printStackTrace();}

                       }



                       gl = FixedFuncUtil.wrapFixedFuncEmul(_gl, ShaderSelectionMode.AUTO, null);

                       gl.glGenBuffers(maxvbonumbers, vboNames, 0);

                       glu = GLU.createGLU();
                       
                                CubeObject cubeObject = new CubeObject();
                                CubeObject2 cubeObject2 = new CubeObject2();
                                pushData(cubeObject, cubeObject2);

                   }
                   
                        public void dispose(GLAutoDrawable drawable) {

                                quit = true;

                        }
                        public void display(GLAutoDrawable drawable) {

                                display(drawable, true);

                        }
                       
                       
                        public void display(GLAutoDrawable drawable, boolean clear) {

                                //GL2ES1//
                                gl = drawable.getGL().getGL2ES1();

                                if (clear) {

                                        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);

                                }

                                gl.glMatrixMode(gl.GL_MODELVIEW);

                                gl.glLoadIdentity();

                                gl.glTranslatef(0.f, 0.f, -30.f);

                                gl.glRotatef((float) (time * 29.77f), 1.0f, 2.0f, 0.0f);

                                gl.glRotatef((float) (time * 22.311f), -0.1f, 0.0f, -5.0f);

                                for (int i=0;i<bufferCount;i++)
                                {
                                        gl.glDrawElements(gl.GL_TRIANGLES, 6 * 6, gl.GL_UNSIGNED_BYTE, cubeIndices[i]);
                                }
                                time += 0.01f;
                        }
                       
                        public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {

                        }
                       
                        private void run(int type) {

                                int width = 800;

                                int height = 480;

                                System.err.println("Cube.run()");
                               
       
                               

                                try {

                                        GLCapabilities caps = new GLCapabilities(null);

                                        // For emulation library, use 16 bpp

                                        caps.setRedBits(5);

                                        caps.setGreenBits(6);

                                        caps.setBlueBits(5);

                                        caps.setDepthBits(16);

                                        Window nWindow = null;

                                        GLWindow window;

                                        if (0 != (type & USE_AWT)) {

                                                Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display

                                                Screen nScreen = NewtFactory.createScreen(nDisplay, 0); // screen 0

                                                nWindow = NewtFactory.createWindow(nScreen, caps);

                                                nWindow.setUndecorated(false);

                                                window = GLWindow.create(nWindow);

                                        } else {

                                                window = GLWindow.create(caps);

                                        }

                                        window.addGLEventListener(this);

                                        window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err);

                                        // Size OpenGL to Video Surface

                                        window.setSize(width, height);

                                        window.setFullscreen(true);

                                        window.setVisible(true);

                                        while (!quit && window.getTotalFPSDuration() < 31000) {

                                                window.display();

                                        }

                                        // Shut things down cooperatively

                                        window.destroy();

                                        System.out.println("Cube shut down cleanly.");

                                } catch (Throwable t) {

                                        t.printStackTrace();

                                }

                        }
                       
                        public static int USE_NEWT = 0;

                        public static int USE_AWT = 1 << 0;

                        public static void main(String[] args) {

                               
                                int type = USE_NEWT;

                                for (int i = args.length - 1; i >= 0; i--) {

                                        if (args[i].equals("-awt")) {

                                                type |= USE_AWT;

                                        }

                                }

                                new Renderer(true, true).run(type);

                                System.exit(0);
                               
                       
                        }
                       

       
}