Java Code Examples for com.jogamp.opengl.GL2#glMatrixMode()

The following examples show how to use com.jogamp.opengl.GL2#glMatrixMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JCuboid.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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(45.0f, h, 1.0, 20.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 2
Source File: OpenGLGFXCMD.java    From sagetv with Apache License 2.0 6 votes vote down vote up
void drawString(String text, int x, int y, java.awt.Color color, GL2 gl, com.jogamp.opengl.util.awt.TextRenderer tr)
{
  //System.out.println("drawString "+ text + " x: "+x+" y: "+y);
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glPushMatrix();
  gl.glLoadIdentity();
  //System.out.println("glOrtho(0,"+ c.getWidth() + ","+c.getHeight()+",0,-1.0,1.0)");
  gl.glOrtho(0,c.getWidth(),0,c.getHeight(),-1.0,1.0);
  tr.begin3DRendering();

  tr.setColor(color);
  tr.draw3D(text, x, c.getHeight()-y, 0, 1);
  tr.end3DRendering();
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glPopMatrix();
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
}
 
Example 3
Source File: ViewportEntity.java    From Robot-Overlord-App with GNU General Public License v2.0 6 votes vote down vote up
public void renderPick(GL2 gl2,double pickX,double pickY) {
       gl2.glMatrixMode(GL2.GL_PROJECTION);
       gl2.glLoadIdentity();
       
       // get the current viewport dimensions to set up the projection matrix
       int[] viewportDimensions = new int[4];
	gl2.glGetIntegerv(GL2.GL_VIEWPORT,viewportDimensions,0);

	GLU glu = GLU.createGLU(gl2);
       
	// Set up a tiny viewport that only covers the area behind the cursor. 
	// Tiny viewports are faster.
	glu.gluPickMatrix(pickX, canvasHeight-pickY, 5.0, 5.0, viewportDimensions,0);

	if(drawOrtho.get()) {
		renderOrtho(gl2);
	} else {
		renderPerspective(gl2);
	}
	
	renderShared(gl2);
}
 
Example 4
Source File: OpenGLGFXCMD.java    From sagetv with Apache License 2.0 6 votes vote down vote up
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
  GL2 gl;
  System.out.println("reshape "+x+","+y+" "+width+","+height);
  gl = drawable.getGL().getGL2();
  newosdwidth=width;
  newosdheight=height;
  gl.glViewport(0, 0, newosdwidth, newosdheight);
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glOrtho(0,newosdwidth,newosdheight,0,-1.0,1.0);
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  gl.glLoadIdentity();
  gl.glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
  gl.glClear( gl.GL_COLOR_BUFFER_BIT);
  synchronized(newpbufferlock)
  {
    newpbuffer=true;
  }
  myConn.postResizeEvent(new java.awt.Dimension(c.getWidth(), c.getHeight()));
}
 
Example 5
Source File: JCudaDriverVolumeRendererJOGL.java    From jcuda-samples with MIT License 6 votes vote down vote up
/**
 * Set up a default view for the given GLAutoDrawable
 * 
 * @param drawable The GLAutoDrawable to set the view for
 */
private void setupView(GLAutoDrawable drawable)
{
    GL2 gl = drawable.getGL().getGL2();

    int w = drawable.getSurfaceWidth();
    int h = drawable.getSurfaceHeight();
    gl.glViewport(0, 0, w, h);

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

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
}
 
Example 6
Source File: J3DBasic.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

    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(40.0f, h, 1.5, 18.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();

}
 
Example 7
Source File: CubeSample2.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		GL2 gl = drawable.getGL().getGL2();
//		gl.glViewport(0f, 0f, width, height); //(3) Jogl内部で実行済みなので,不要.(APIDOCに書いてある)
//		gl.glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
		gl.glMatrixMode(GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(30.0, (double)width / (double)height, 1.0, 300.0);
//		gl.glTranslatef(0.0f, 0.0f, -5.0f);
		glu.gluLookAt(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
//		System.out.printf("x:%d, y:%d, w:%d, h:%d, %n", x, y, width, height);
		//これによりウィンドウをリサイズしても中の図形は大きさが維持される.
		//また,第3,第4引数を入れ替えることによりGLWindowの座標系(左上隅が原点)とデバイス座標系(左下隅が原点)の違いを吸収している.
//		gl.glOrthof(x, x + width, y + height, y, -1.0f, 1.0f); //(4)
//		gl.glOrthof(x/300f, (x + width)/300f, (y + height)/300f, y/300f, -1.0f, 1.0f); //(4)
//		gl.glOrthof(x/400f, (x + width)/400f, (y + height)/400f, y/400f, -1.0f, 1.0f); //(4)
//		gl.glOrthof(x/400f, (x + width)/400f, (y + height)/400f, y/400f, -1.0f, 1.0f); //(4)
//		gl.glOrthof(x/300f, (x/300) + width, (y/300) + height, y/300f, -1.0f, 1.0f); //(4)
//		gl.glOrthof(x , x + width/300f, y + height/300f, y, -1.0f, 1.0f); //(4)

		gl.glMatrixMode(GL_MODELVIEW);
//		gl.glLoadIdentity();
	}
 
Example 8
Source File: J3DTriangle.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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(45.0f, h, 1.0, 20.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 9
Source File: J3DCube.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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(45.0f, h, 1.0, 20.0);
    glu.gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 10
Source File: OneTriangle.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected static void setup( GL2 gl2, int width, int height ) {
    gl2.glMatrixMode( GL2.GL_PROJECTION );
    gl2.glLoadIdentity();

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

    gl2.glMatrixMode( GL2.GL_MODELVIEW );
    gl2.glLoadIdentity();

    gl2.glViewport( 0, 0, width, height );
}
 
Example 11
Source File: CubeTexture.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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(45.0f, h, 1.0, 20.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 12
Source File: Tessellation.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: BasicLine1.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    final GL2 gl = drawable.getGL().getGL2();
    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(-2f, 2f, -2f, 2f, 0.0f, 1.0f);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 14
Source File: DelaunayTriangulationExample.java    From delaunay-triangulator with MIT License 5 votes vote down vote up
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrtho(0, DIMENSION.getWidth(), DIMENSION.getHeight(), 0, 1.0, -1.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}
 
Example 15
Source File: ViewportEntity.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
public void renderPerspective(GL2 gl2) {
   	gl2.glMatrixMode(GL2.GL_PROJECTION);
	gl2.glLoadIdentity();
	
	double zNear = nearZ.get();
	double zFar = farZ.get();
	double fH = Math.tan( Math.toRadians(fieldOfView.get()/2) ) * zNear;
	double aspect = (double)canvasWidth / (double)canvasHeight;
	double fW = fH * aspect;

	gl2.glFrustum(-fW,fW,-fH,fH,zNear,zFar);
}
 
Example 16
Source File: ViewportEntity.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
public void renderOrtho(GL2 gl2,double zoom) {
   	gl2.glMatrixMode(GL2.GL_PROJECTION);
	gl2.glLoadIdentity();
	
       double w = canvasWidth/10;
       double h = canvasHeight/10;
       
	gl2.glOrtho(-w/zoom, w/zoom, -h/zoom, h/zoom, nearZ.get(), farZ.get());
}
 
Example 17
Source File: ViewportEntity.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
public void renderShared(GL2 gl2) {
	// store the projection matrix for later
       double [] m = new double[16];
       gl2.glGetDoublev(GL2.GL_PROJECTION_MATRIX, m, 0);
       projectionMatrix.set(m);

   	gl2.glMatrixMode(GL2.GL_MODELVIEW);
       gl2.glLoadIdentity();
   	
	PoseEntity camera = getAttachedTo();
	Matrix4d mFinal = camera.getPoseWorld();
	mFinal.invert();
	MatrixHelper.applyMatrix(gl2, mFinal);
}
 
Example 18
Source File: ViewCubeEntity.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
public void render(GL2 gl2) {
	RobotOverlord ro = (RobotOverlord)getRoot();
	ViewportEntity cameraView = ro.viewport;
	
	gl2.glClear(GL2.GL_DEPTH_BUFFER_BIT);

	gl2.glEnable(GL2.GL_DEPTH_TEST);
	gl2.glEnable(GL2.GL_CULL_FACE);
	
	gl2.glBlendFunc(GL2.GL_SRC_ALPHA,GL2.GL_ONE_MINUS_SRC_ALPHA);

   	gl2.glMatrixMode(GL2.GL_PROJECTION);
	gl2.glPushMatrix();
	cameraView.renderOrtho(gl2,1);
	gl2.glMatrixMode(GL2.GL_MODELVIEW);
	
	gl2.glPushMatrix();			
		double c = cubeSize.get();			
		PoseEntity camera = cameraView.getAttachedTo();
		Matrix4d m = camera.getPoseWorld();
		Vector3d p = camera.getPosition();
		Vector3d vx = MatrixHelper.getXAxis(m);
		Vector3d vy = MatrixHelper.getYAxis(m);
		Vector3d vz = MatrixHelper.getZAxis(m);
	
		vz.scale(-100);
		vx.scale(cameraView.getCanvasWidth() /10 -c*2);
		vy.scale(cameraView.getCanvasHeight()/10 -c*2);
		p.add(vx);
		p.add(vy);
		p.add(vz);
		
		gl2.glTranslated(p.x, p.y, p.z);
		gl2.glScaled(c,c,c);

		model.render(gl2);

		gl2.glDisable(GL2.GL_LIGHTING);
		gl2.glDisable(GL2.GL_COLOR_MATERIAL);
		gl2.glDisable(GL2.GL_TEXTURE_2D);
		
		// the big lines
		gl2.glLineWidth(4);
		gl2.glPushMatrix();
			gl2.glTranslated(-1.05,-1.05,-0.95);
			gl2.glBegin(GL2.GL_LINES);
			gl2.glColor3d(1, 0, 0);		gl2.glVertex3d(0, 0, 0);		gl2.glVertex3d(2.5, 0, 0);
			gl2.glColor3d(0, 1, 0);		gl2.glVertex3d(0, 0, 0);		gl2.glVertex3d(0, 2.5, 0);
			gl2.glColor3d(0, 0, 1);		gl2.glVertex3d(0, 0, 0);		gl2.glVertex3d(0, 0, 2.5);
			gl2.glEnd();
		gl2.glPopMatrix();
		gl2.glLineWidth(1);
					
	gl2.glPopMatrix();

   	gl2.glMatrixMode(GL2.GL_PROJECTION);
	gl2.glPopMatrix();
	gl2.glMatrixMode(GL2.GL_MODELVIEW);
}
 
Example 19
Source File: OpenGLGFXCMD.java    From sagetv with Apache License 2.0 4 votes vote down vote up
private void initpbuffer()
{
  GL2 gl;
  System.out.println("initpbuffer");
  osdwidth=newosdwidth;
  osdheight=newosdheight;
  GLCapabilities caps = new GLCapabilities(null);
  caps.setHardwareAccelerated(true);
  caps.setDoubleBuffered(false);
  caps.setAlphaBits(8);
  caps.setRedBits(8);
  caps.setGreenBits(8);
  caps.setBlueBits(8);
  caps.setDepthBits(0);
  caps.setFBO(false);
  System.out.println("initpbuffer2");
  if (!GLDrawableFactory.getFactory(caps.getGLProfile()).canCreateGLPbuffer(null, caps.getGLProfile()))
  {
    throw new GLException("pbuffers unsupported");
  }
  if(pbuffer!=null) pbuffer.destroy();
  System.out.println("initpbuffer3");
  pbuffer = GLDrawableFactory.getFactory(caps.getGLProfile()).createOffscreenAutoDrawable(null,
      caps,
      null,
      osdwidth,
      osdheight
      );
  pbuffer.setContext(pbuffer.createContext(c.getContext()), true);
  //pbuffer.setContext(c.getContext(), false);
  System.out.println("initpbuffer4: pbuffers is null? " + (pbuffer==null));
  if(pbuffer.getContext().makeCurrent()==GLContext.CONTEXT_NOT_CURRENT)
  {
    System.out.println("Couldn't make pbuffer current?");
    return;
  }
  System.out.println("initpbuffer5");
  gl = pbuffer.getGL().getGL2();

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

  gl.glClear( gl.GL_COLOR_BUFFER_BIT);

  gl.glViewport(0, 0, osdwidth, osdheight);
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glOrtho(0,osdwidth,0,osdheight,-1.0,1.0);
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  gl.glLoadIdentity();

  // TODO: look into reusing same texture like OSX version...
  if(osdt!=null) gl.glDeleteTextures(1, osdt, 0);
  osdt = new int[1];
  byte img[] = new byte[osdwidth*osdheight*4];
  gl.glGenTextures(1, osdt, 0);
  gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
  gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE,osdt[0]);
  gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
  gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
  gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 4, osdwidth, osdheight, 0,
      gl.GL_BGRA, bigendian ? gl.GL_UNSIGNED_INT_8_8_8_8_REV : gl.GL_UNSIGNED_BYTE, java.nio.ByteBuffer.wrap(img));

  gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
  gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, osdt[0]);
  gl.glCopyTexSubImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 0, 0, 0, 0, osdwidth, osdheight);
  gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
  System.out.println("initpbuffer6");
  pbuffer.getContext().release();
  System.out.println("initpbuffer7");
}
 
Example 20
Source File: JCudaDriverVolumeRendererJOGL.java    From jcuda-samples with MIT License 4 votes vote down vote up
@Override
public void display(GLAutoDrawable drawable)
{
    GL2 gl = drawable.getGL().getGL2();

    // Use OpenGL to build view matrix
    float modelView[] = new float[16];
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glPushMatrix();
    gl.glLoadIdentity();
    gl.glRotatef(-simpleInteraction.getRotationDegX(), 1.0f, 0.0f, 0.0f);
    gl.glRotatef(-simpleInteraction.getRotationDegY(), 0.0f, 1.0f, 0.0f);
    gl.glTranslatef(
        -simpleInteraction.getTranslationX(), 
        -simpleInteraction.getTranslationY(), 
        -simpleInteraction.getTranslationZ());
    gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, modelView, 0);
    gl.glPopMatrix();

    // Build the inverted view matrix
    invViewMatrix[0] = modelView[0];
    invViewMatrix[1] = modelView[4];
    invViewMatrix[2] = modelView[8];
    invViewMatrix[3] = modelView[12];
    invViewMatrix[4] = modelView[1];
    invViewMatrix[5] = modelView[5];
    invViewMatrix[6] = modelView[9];
    invViewMatrix[7] = modelView[13];
    invViewMatrix[8] = modelView[2];
    invViewMatrix[9] = modelView[6];
    invViewMatrix[10] = modelView[10];
    invViewMatrix[11] = modelView[14];

    // Copy the inverted view matrix to the global variable that
    // was obtained from the module. The inverted view matrix
    // will be used by the kernel during rendering.
    cuMemcpyHtoD(c_invViewMatrix, Pointer.to(invViewMatrix),
        invViewMatrix.length * Sizeof.FLOAT);

    // Render and fill the PBO with pixel data
    render();

    // Draw the image from the PBO
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glDisable(GL.GL_DEPTH_TEST);
    gl.glRasterPos2i(0, 0);
    gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pbo);
    gl.glDrawPixels(width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, 0);
    gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, 0);

    // Update FPS information in main frame title
    step++;
    long currentTime = System.nanoTime();
    if (prevTimeNS == -1)
    {
        prevTimeNS = currentTime;
    }
    long diff = currentTime - prevTimeNS;
    if (diff > 1e9)
    {
        double fps = (diff / 1e9) * step;
        String t = "JCuda 3D texture volume rendering sample - ";
        t += String.format("%.2f", fps)+" FPS";
        frame.setTitle(t);
        prevTimeNS = currentTime;
        step = 0;
    }

}