Пример #1
0
  /** Sets the {@code JComponent} that will be drawn to the OpenGL canvas. */
  public void setDrawableComponent(JComponent component) {
    if (component == drawableComponent) {
      return;
    }

    if (g2dglListener != null) {
      canvas.removeGLEventListener(g2dglListener);
      if (sideContext != null) {
        sideContext.removeGLEventListener(g2dglListener);
      }
    }

    if (drawableComponent != null) {
      remove(drawableComponent);
    }

    drawableComponent = component;
    if (drawableComponent != null) {
      verifyHierarchy(drawableComponent);

      g2dglListener = createG2DListener(drawableComponent);
      canvas.addGLEventListener(g2dglListener);
      if (sideContext != null) {
        sideContext.addGLEventListener(g2dglListener);
      }

      add(drawableComponent);
    }
  }
Пример #2
0
  /**
   * Calling {@link GLCanvas#removeNotify()} destroys the GLContext. We could mess with that
   * internally, but this is slightly easier.
   *
   * <p>This method is particularly important for docking frameworks and moving the panel from one
   * window to another. This is simple for normal Swing components, but GL contexts are destroyed
   * when {@code removeNotify()} is called.
   *
   * <p>Our workaround is to use context sharing. The pbuffer is initialized and by drawing into it
   * at least once, we automatically share all textures, etc. with the new pbuffer. This pbuffer
   * holds the data until we can initialize our new JOGL canvas. We share the pbuffer canvas with
   * the new JOGL canvas and everything works nicely from then on.
   *
   * <p>This has the unfortunate side-effect of leaking memory. I'm not sure how to fix this yet.
   */
  @Override
  public void removeNotify() {
    prepareSideContext();

    remove((Component) canvas);
    super.removeNotify();

    canvas = createGLComponent(chosenCapabilities, sideContext.getContext());
    canvas.addGLEventListener(g2dglListener);
    add((Component) canvas, 0);
  }
Пример #3
0
  /*
   * Initialize z-buffer, projection matrix, light source, and lighting model.
   * Do not specify a material property here.
   */
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    glut = new GLUT();
    //
    float position[] = {0.0f, 3.0f, 3.0f, 0.0f};
    float local_view[] = {0.0f};

    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LESS);

    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
    gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);

    gl.glFrontFace(GL.GL_CW);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_AUTO_NORMAL);
    gl.glEnable(GL2.GL_NORMALIZE);
    gl.glEnable(GL2.GL_FOG);
    {
      float fogColor[] = {0.5f, 0.5f, 0.5f, 1.0f};

      fogMode = GL2.GL_EXP;
      gl.glFogi(GL2.GL_FOG_MODE, fogMode);
      gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
      gl.glFogf(GL2.GL_FOG_DENSITY, 0.35f);
      gl.glHint(GL2.GL_FOG_HINT, GL.GL_DONT_CARE);

      gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }
  }
Пример #4
0
 public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
   GL2 gl = drawable.getGL().getGL2();
   //
   gl.glViewport(0, 0, w, h);
   gl.glMatrixMode(GL2.GL_PROJECTION);
   gl.glLoadIdentity();
   if (w <= (h * 3))
     gl.glOrtho(
         -6.0,
         6.0,
         -2.0 * ((float) h * 3) / (float) w,
         2.0 * ((float) h * 3) / (float) w,
         0.0,
         10.0);
   else
     gl.glOrtho(
         -6.0 * (float) w / ((float) h * 3), //
         6.0 * (float) w / ((float) h * 3),
         -2.0,
         2.0,
         0.0,
         10.0);
   gl.glMatrixMode(GL2.GL_MODELVIEW);
   gl.glLoadIdentity();
 }
Пример #5
0
  @Override
  public void init(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    try {
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_VERTEX_SHADER, "glsl/simple_vs"));
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_FRAGMENT_SHADER, "glsl/simple_fs"));
      program = GLSLHelpers.createProgram(gl3, shaders);

      gl3.glGenVertexArrays(1, VAO, 0);
      gl3.glBindVertexArray(VAO[0]);

      gl3.glGenBuffers(1, VBO, 0);
      gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, VBO[0]);
      FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(TRIANGLE);
      gl3.glBufferData(GL3.GL_ARRAY_BUFFER, buffer.capacity() * 4, buffer, GL3.GL_STATIC_DRAW);
      gl3.glEnableVertexAttribArray(0);
      gl3.glVertexAttribPointer(0, 2, GL3.GL_FLOAT, false, 0, 0);

      gl3.glBindVertexArray(0);
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
Пример #6
0
 @Override
 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
   System.out.println("reshape");
   GL3 gl3 = drawable.getGL().getGL3();
   /** Just the glViewport for this sample, normally here you update your projection matrix. */
   gl3.glViewport(x, y, width, height);
 }
Пример #7
0
 public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
   GL2 gl = drawable.getGL().getGL2();
   //
   gl.glViewport(0, 0, w, h);
   gl.glMatrixMode(GL2.GL_PROJECTION);
   gl.glLoadIdentity();
   glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
 }
Пример #8
0
 @Override
 public void paint(Graphics g) {
   if (isGLDrawing() && drawableComponent != null && canvas != null) {
     canvas.display();
   } else {
     super.paint(g);
   }
 }
Пример #9
0
  @Override
  public void dispose(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    gl3.glDeleteVertexArrays(1, VAO, 0);
    gl3.glDeleteBuffers(1, VBO, 0);
    for (int shader : shaders) gl3.glDeleteShader(shader);
    gl3.glDeleteProgram(program);
  }
Пример #10
0
 public final void display(GLAutoDrawable drawable) {
   try {
     if (this._is_setup_done) {
       this.draw(drawable.getGL());
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #11
0
 public void display(GLAutoDrawable drawable) {
   GL2 gl = drawable.getGL().getGL2();
   //
   gl.glClear(GL.GL_COLOR_BUFFER_BIT);
   gl.glColor3f(1.0f, 1.0f, 1.0f);
   gl.glCallList(startList);
   gl.glCallList(startList + 1);
   gl.glFlush();
 }
Пример #12
0
  @Override
  public void draw(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glLineWidth(lineWidth); // call before glBegin

    gl.glBegin(GL.GL_LINES);
    gl.glColor3f(1.0f, 0.51f, 0.98f); // #FF83FA
    gl.glVertex2f(x1, y); // start point
    gl.glVertex2f(x2, y); // end point
    gl.glEnd();
  }
  @Override
  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glMatrixMode(GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(30.0, (double) width / (double) height, 1.0, 300.0);

    // 視点位置と視線方向
    // glu.gluLookAt(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);//コメントアウト

    gl.glMatrixMode(GL_MODELVIEW);
  }
Пример #14
0
  @Override
  public void dispose(GLAutoDrawable drawable) {
    System.out.println("dispose");

    GL3 gl3 = drawable.getGL().getGL3();

    gl3.glDeleteProgram(programName);
    gl3.glDeleteVertexArrays(1, vertexArrayName);
    gl3.glDeleteBuffers(Buffer.MAX, bufferName);

    System.exit(0);
  }
Пример #15
0
 public final void init(GLAutoDrawable drawable) {
   try {
     GL gl = drawable.getGL();
     this.setup(gl);
     Animator animator = new Animator(drawable);
     animator.start();
     this._is_setup_done = true;
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return;
 }
Пример #16
0
  private void prepareSideContext() {
    if (sideContext == null) {
      GLDrawableFactory factory = canvas.getFactory();
      sideContext = factory.createOffscreenAutoDrawable(null, chosenCapabilities, null, 1, 1);
      ((GLOffscreenAutoDrawable) sideContext).setSharedContext(canvas.getContext());
      sideContext.addGLEventListener(g2dglListener);
    }

    Runnable work =
        new Runnable() {
          @Override
          public void run() {
            sideContext.display();
          }
        };

    if (Threading.isOpenGLThread()) {
      work.run();
    } else {
      Threading.invokeOnOpenGLThread(false, work);
    }
  }
Пример #17
0
 @Override
 public void init(final GLAutoDrawable drawable) {
   final GL gl = drawable.getGL();
   System.err.println("GL_VENDOR   " + gl.glGetString(GL.GL_VENDOR));
   System.err.println("GL_RENDERER " + gl.glGetString(GL.GL_RENDERER));
   System.err.println("GL_VERSION  " + gl.glGetString(GL.GL_VERSION));
   if (useVBO) {
     gl.glGenBuffers(1, vboName, 0);
     if (0 == vboName[0]) {
       throw new GLException("glGenBuffers didn't return valid VBO name");
     }
   }
 }
Пример #18
0
  @Override
  public void display(final GLAutoDrawable drawable) {
    final GL2ES1 gl = drawable.getGL().getGL2ES1();

    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    // draw a triangle filling the window
    gl.glLoadIdentity();

    if (useVBO) {
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName[0]);
      gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferAll.limit(), bufferAll, GL.GL_STATIC_DRAW);
      if (useArrayData) {
        arrayV.setVBOName(vboName[0]);
        arrayC.setVBOName(vboName[0]);
      }
    }

    gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
    if (useArrayData) {
      gl.glVertexPointer(arrayV);
    } else {
      if (useVBO) {
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, bufferVOffset);
      } else {
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, bufferV);
      }
    }

    gl.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);
    if (useArrayData) {
      gl.glColorPointer(arrayC);
    } else {
      if (useVBO) {
        gl.glColorPointer(3, GL.GL_FLOAT, 0, bufferCOffset);
      } else {
        gl.glColorPointer(3, GL.GL_FLOAT, 0, bufferC);
      }
    }

    if (useVBO) {
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
    }

    gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
    gl.glFlush();

    gl.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
    gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
  }
Пример #19
0
  public void init(final GLAutoDrawable drawable) {
    super.init(drawable);

    final GL2ES2 gl = drawable.getGL().getGL2ES2();

    final RenderState rs = getRenderer().getRenderState();

    gl.setSwapInterval(1);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glEnable(GL.GL_BLEND);
    rs.setColorStatic(0.0f, 0.0f, 0.0f, 1.0f);

    createTestOutline();
  }
Пример #20
0
  @Override
  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    final GL2 gl = drawable.getGL().getGL2();
    if (height <= 0) height = 1;

    final float h = (float) width / (float) height;
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();

    glu.gluPerspective(FOV, h, 1.0, 4000.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
  }
Пример #21
0
  @Override
  public void draw(GLAutoDrawable glAutoDrawable, MatrixStack mvStack, Matrix3D pMat) {
    GL4 gl = (GL4) glAutoDrawable.getGL();

    mvStack.pushMatrix(); // push translate
    translate(dxTranslate, dyTranslate, dzTranslate);
    mvStack.multMatrix(getTranslation());
    mvStack.pushMatrix(); // push rotate

    rotate(dxRotate, dyRotate, dzRotate);
    mvStack.multMatrix(getRotation());
    mvStack.multMatrix(getScale());

    gl.glUniformMatrix4fv(IdentityLocs.getMvLoc(), 1, false, mvStack.peek().getFloatValues(), 0);
    gl.glUniformMatrix4fv(IdentityLocs.getProjLoc(), 1, false, pMat.getFloatValues(), 0);
    gl.glUniformMatrix4fv(
        IdentityLocs.getnlocation(),
        1,
        false,
        (mvStack.peek().inverse().transpose().getFloatValues()),
        0);

    // bind vertex values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index]);
    gl.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(0);

    // bind normal values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index + 2]);
    gl.glVertexAttribPointer(1, 3, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(1);

    // bind texture values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index + 1]);
    gl.glVertexAttribPointer(2, 2, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(2);

    gl.glActiveTexture(gl.GL_TEXTURE0);
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture);

    gl.glEnable(GL_CULL_FACE);
    gl.glFrontFace(GL_CCW);
    gl.glEnable(GL_DEPTH_TEST);
    gl.glDepthFunc(GL_LEQUAL);
    // draw arrays
    gl.glDrawArrays(GL_TRIANGLES, 0, myShape.getIndices().length);
    mvStack.popMatrix(); // pop rotate
    mvStack.popMatrix(); // pop translate
  }
Пример #22
0
  @Override
  public void reshape(
      final GLAutoDrawable gLDrawable, final int x, final int y, final int w, final int h) {
    final GL2 gl = gLDrawable.getGL().getGL2();

    gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    gl.glLoadIdentity();
    if (w <= h) {
      gl.glOrtho(-3.5, 3.5, -3.5 * h / w, 3.5 * h / w, -3.5, 3.5);
    } else {
      gl.glOrtho(-3.5 * w / h, 3.5 * w / h, -3.5, 3.5, -3.5, 3.5);
    }
    gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    gl.glLoadIdentity();
  }
Пример #23
0
  @Override
  public void display(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    gl3.glClear(GL3.GL_COLOR_BUFFER_BIT);

    gl3.glUseProgram(program);

    gl3.glBindVertexArray(VAO[0]);
    gl3.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
    gl3.glBindVertexArray(0);

    gl3.glUseProgram(0);
  }
    public void run() {
      System.err.println(
          "Animatr " + id + ", count " + frameCount + ": PRE: " + Thread.currentThread().getName());

      for (int c = 0; c < frameCount; c++) {
        glad.display();
      }

      System.err.println("Animatr " + id + ": DONE/SYNC: " + Thread.currentThread().getName());
      synchronized (postSync) {
        done = true;
        System.err.println("Animatr " + id + ": END: " + Thread.currentThread().getName());
        postSync.notifyAll();
      }
    }
Пример #25
0
  @Override
  public void dispose(final GLAutoDrawable drawable) {
    final GL2ES2 gl = drawable.getGL().getGL2ES2();
    if (null != texture) {
      texture.disable(gl);
      texture.destroy(gl);
    }
    if (null != textureData) {
      textureData.destroy();
    }

    pmvMatrixUniform = null;
    pmvMatrix = null;
    st.destroy(gl);
    st = null;
  }
Пример #26
0
  @Override
  public void reshape(
      final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
    final GL2ES1 gl = drawable.getGL().getGL2ES1();

    gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    gl.glLoadIdentity();

    // coordinate system origin at lower left with width and height same as the window
    glu.gluOrtho2D(0.0f, width, 0.0f, height);

    gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glViewport(0, 0, width, height);
  }
Пример #27
0
  @Override
  public void init(GLAutoDrawable drawable) {
    System.out.println("init");

    GL3 gl3 = drawable.getGL().getGL3();

    initBuffers(gl3);

    initVertexArray(gl3);

    initProgram(gl3);

    gl3.glEnable(GL_DEPTH_TEST);

    start = System.currentTimeMillis();
  }
Пример #28
0
 @Override
 public void display(GLAutoDrawable drawable) {
   GL2 gl = drawable.getGL().getGL2();
   gl.glClear(GL.GL_COLOR_BUFFER_BIT);
   // gl.glColor3f(1.0f, 0.0f, 0.0f); //ここは削除
   gl.glRotatef(25f, 0f, 1f, 0f);
   gl.glBegin(GL_POLYGON);
   gl.glColor3f(1.0f, 0.0f, 0.0f); // 赤
   gl.glVertex2f(-0.9f, -0.9f);
   gl.glColor3f(0.0f, 1.0f, 0.0f); // 緑
   gl.glVertex2f(0.9f, -0.9f);
   gl.glColor3f(0.0f, 0.0f, 1.0f); // 青
   gl.glVertex2f(0.9f, 0.9f);
   gl.glColor3f(1.0f, 1.0f, 0.0f); // 黄
   gl.glVertex2f(-0.9f, 0.9f);
   gl.glEnd();
 }
Пример #29
0
  public void display(final GLAutoDrawable drawable) {
    final GL2ES2 gl = drawable.getGL().getGL2ES2();

    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    final RegionRenderer regionRenderer = getRenderer();
    final PMVMatrix pmv = regionRenderer.getMatrix();
    pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    pmv.glLoadIdentity();
    pmv.glTranslatef(getXTran(), getYTran(), getZTran());
    pmv.glRotatef(getAngle(), 0, 1, 0);
    if (weight != regionRenderer.getRenderState().getWeight()) {
      regionRenderer.getRenderState().setWeight(weight);
    }
    region.draw(gl, regionRenderer, getSampleCount());
  }
Пример #30
-1
 @Override
 public void dispose(final GLAutoDrawable drawable) {
   final GL gl = drawable.getGL();
   if (0 != vboName[0]) {
     gl.glDeleteBuffers(1, vboName, 0);
     vboName[0] = 0;
   }
 }