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

The following examples show how to use com.jogamp.opengl.GL2#glBindBuffer() . 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: KeystoneHelper.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
private void storeAttributes(final int shaderAttributeType, final int bufferIndex, final int size,
		final float[] data) {
	final GL2 gl = getGL();
	// Select the VBO, GPU memory data, to use for data
	gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferIndex);
	// Associate Vertex attribute with the last bound VBO
	gl.glVertexAttribPointer(shaderAttributeType, size, GL2.GL_FLOAT, false, 0, 0 /* offset */);
	// compute the total size of the buffer :
	final int numBytes = data.length * 4;
	gl.glBufferData(GL2.GL_ARRAY_BUFFER, numBytes, null, GL2.GL_STATIC_DRAW);

	final FloatBuffer fbData = Buffers.newDirectFloatBuffer(data);
	gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, numBytes, fbData);
	gl.glEnableVertexAttribArray(shaderAttributeType);
}
 
Example 2
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;
    }

}
 
Example 3
Source File: KeystoneHelper.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public void createScreenSurface() {
	final GL2 gl = getGL();
	// Keystoning computation (cf
	// http://www.bitlush.com/posts/arbitrary-quadrilaterals-in-opengl-es-2-0)
	// transform the coordinates [0,1] --> [-1,+1]
	final ICoordinates coords = getData().getKeystone();
	final float[] p0 = new float[] { (float) coords.at(0).x * 2f - 1f, (float) (coords.at(0).y * 2f - 1f) }; // bottom-left
	final float[] p1 = new float[] { (float) coords.at(1).x * 2f - 1f, (float) coords.at(1).y * 2f - 1f }; // top-left
	final float[] p2 = new float[] { (float) coords.at(2).x * 2f - 1f, (float) coords.at(2).y * 2f - 1f }; // top-right
	final float[] p3 = new float[] { (float) coords.at(3).x * 2f - 1f, (float) coords.at(3).y * 2f - 1f }; // bottom-right

	final float ax = (p2[0] - p0[0]) / 2f;
	final float ay = (p2[1] - p0[1]) / 2f;
	final float bx = (p3[0] - p1[0]) / 2f;
	final float by = (p3[1] - p1[1]) / 2f;

	final float cross = ax * by - ay * bx;

	if (cross != 0) {
		final float cy = (p0[1] - p1[1]) / 2f;
		final float cx = (p0[0] - p1[0]) / 2f;

		final float s = (ax * cy - ay * cx) / cross;

		final float t = (bx * cy - by * cx) / cross;

		final float q0 = 1 / (1 - t);
		final float q1 = 1 / (1 - s);
		final float q2 = 1 / t;
		final float q3 = 1 / s;

		// I can now pass (u * q, v * q, q) to OpenGL
		final float[] listVertices =
				new float[] { p0[0], p0[1], 1f, p1[0], p1[1], 0f, p2[0], p2[1], 0f, p3[0], p3[1], 1f };
		final float[] listUvMapping =
				new float[] { 0f, 1f * q0, 0f, q0, 0f, 0f, 0f, q1, 1f * q2, 0f, 0f, q2, 1f * q3, 1f * q3, 0f, q3 };
		// VERTICES POSITIONS BUFFER
		storeAttributes(AbstractShader.POSITION_ATTRIBUTE_IDX, verticesBufferIndex, 3, listVertices);
		// UV MAPPING (If a texture is defined)
		storeAttributes(AbstractShader.UVMAPPING_ATTRIBUTE_IDX, uvMappingBufferIndex, 4, listUvMapping);

	}

	// gl.glActiveTexture(GL.GL_TEXTURE0);
	// gl.glBindTexture(GL.GL_TEXTURE_2D, fboScene.getFBOTexture());
	getOpenGL().bindTexture(fboScene.getFBOTexture());
	// Select the VBO, GPU memory data, to use for colors
	gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferIndex);
	gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 24, ibIdxBuff, GL2.GL_STATIC_DRAW);
	ibIdxBuff.rewind();
}
 
Example 4
Source File: PrimitiveSolids.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
/**
 * draw a sphere with a given radius.
 * TODO expose quality parameters?
 * TODO generate a sphere once as a model, return that.
 * See https://www.gamedev.net/forums/topic/537269-procedural-sphere-creation/4469427/
 * @param gl2
 * @param radius
 */
static public void drawSphere(GL2 gl2,double radius) {
	int width = 32;
	int height = 16;
	
	double theta, phi;
	int i, j, t;

	int nvec = (height-2)* width + 2;
	int ntri = (height-2)*(width-1)*2;

	FloatBuffer vertices = FloatBuffer.allocate(nvec * 3);
	IntBuffer indexes = IntBuffer.allocate(ntri * 3);

	float [] dat = vertices.array();
	int   [] idx = indexes.array();
	
	for( t=0, j=1; j<height-1; j++ ) {
		for(i=0; i<width; i++ )  {
			theta = (double)(j)/(double)(height-1) * Math.PI;
			phi   = (double)(i)/(double)(width-1 ) * Math.PI*2;

			dat[t++] = (float)( Math.sin(theta) * Math.cos(phi));
			dat[t++] = (float)( Math.cos(theta));
			dat[t++] = (float)(-Math.sin(theta) * Math.sin(phi));
		}
	}
	dat[t++]= 0;
	dat[t++]= 1;
	dat[t++]= 0;
	dat[t++]= 0;
	dat[t++]=-1;
	dat[t++]= 0;
	
	for( t=0, j=0; j<height-3; j++ ) {
		for(      i=0; i<width-1; i++ )  {
			idx[t++] = (j  )*width + i  ;
			idx[t++] = (j+1)*width + i+1;
			idx[t++] = (j  )*width + i+1;
			idx[t++] = (j  )*width + i  ;
			idx[t++] = (j+1)*width + i  ;
			idx[t++] = (j+1)*width + i+1;
		}
	}
	for( i=0; i<width-1; i++ )  {
		idx[t++] = (height-2)*width;
		idx[t++] = i;
		idx[t++] = i+1;
		idx[t++] = (height-2)*width+1;
		idx[t++] = (height-3)*width + i+1;
		idx[t++] = (height-3)*width + i;
	}

	int NUM_BUFFERS=1;
	int[] VBO = new int[NUM_BUFFERS];
	gl2.glGenBuffers(NUM_BUFFERS, VBO, 0);
	gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[0]);
    // Write out vertex buffer to the currently bound VBO.
	int s=(Float.SIZE/8);  // bits per float / bits per byte = bytes per float
    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, dat.length*s, vertices, GL2.GL_STATIC_DRAW);
    
    
	gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
	gl2.glVertexPointer(3,GL2.GL_FLOAT,0,0);
	
	gl2.glEnableClientState(GL2.GL_NORMAL_ARRAY);
	gl2.glNormalPointer(GL2.GL_FLOAT,0,0);

	gl2.glPushMatrix();
	gl2.glScaled(radius,radius,radius);
	gl2.glDrawElements(GL2.GL_TRIANGLES, ntri*3, GL2.GL_UNSIGNED_INT, indexes );
	gl2.glPopMatrix();
	
	gl2.glDisableClientState(GL2.GL_NORMAL_ARRAY);
	gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY);
	
	gl2.glDeleteBuffers(NUM_BUFFERS, VBO, 0);
}
 
Example 5
Source File: Model.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Regenerate the optimized rendering buffers for the fixed function pipeline.
 * Also recalculate the bounding box.
 * @param gl2
 */
private void updateBuffers(GL2 gl2) {
	int numVertexes = vertexArray.size()/3;
	Iterator<Float> fi;
	int j=0;

	Point3d boundBottom = new Point3d(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
	Point3d boundTop = new Point3d(-Double.MAX_VALUE,-Double.MAX_VALUE,-Double.MAX_VALUE);

	FloatBuffer vertices = FloatBuffer.allocate(vertexArray.size());
	fi = vertexArray.iterator();
	Point3d p = new Point3d();
	while(fi.hasNext()) {
		p.x = fi.next().floatValue();
		p.y = fi.next().floatValue();
		p.z = fi.next().floatValue();
		adjust.transform(p);
		vertices.put(j++, (float)p.x);
		vertices.put(j++, (float)p.y);
		vertices.put(j++, (float)p.z);
		
		// also recalculate the bounding limits			
		if(boundBottom.x>p.x) boundBottom.x=p.x;
		if(boundBottom.y>p.y) boundBottom.y=p.y;
		if(boundBottom.z>p.z) boundBottom.z=p.z;
		if(boundTop.x<p.x) boundTop.x=p.x;
		if(boundTop.y<p.y) boundTop.y=p.y;
		if(boundTop.z<p.z) boundTop.z=p.z;
	}
	
	cuboid.setBounds(boundTop, boundBottom);

	int s=(Float.SIZE/8);  // bits per float / bits per byte = bytes per float
	int totalBufferSize = numVertexes*3*s;
	int vboIndex=0;
	
	// bind a buffer
	vertices.rewind();
	gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
    // Write out vertex buffer to the currently bound VBO.
    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, vertices, GL2.GL_STATIC_DRAW);
    vboIndex++;
    
	if(hasNormals) {
		j=0;
	    // repeat for normals
		Matrix3d pose = new Matrix3d();
		adjust.get(pose);
		FloatBuffer normals = FloatBuffer.allocate(normalArray.size());
		fi = normalArray.iterator();
		while(fi.hasNext()) {
			p.x = fi.next().floatValue();
			p.y = fi.next().floatValue();
			p.z = fi.next().floatValue();
			pose.transform(p);
			normals.put(j++, (float)p.x);
			normals.put(j++, (float)p.y);
			normals.put(j++, (float)p.z);
		}
		
		normals.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, normals, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}

	if(hasColors) {
	    // repeat for colors
		FloatBuffer colors = FloatBuffer.allocate(colorArray.size());
		fi = colorArray.iterator();
		while(fi.hasNext()) {
			colors.put(fi.next().floatValue());
		}
		
		colors.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, totalBufferSize, colors, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}
	
	if(hasUVs) {
	    // repeat for textures
		FloatBuffer texCoords = FloatBuffer.allocate(texCoordArray.size());
		fi = texCoordArray.iterator();
		while(fi.hasNext()) {
			texCoords.put(fi.next().floatValue());
		}
		
	    texCoords.rewind();
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex]);
	    gl2.glBufferData(GL2.GL_ARRAY_BUFFER, numVertexes*2*s, texCoords, GL2.GL_STATIC_DRAW);
	    vboIndex++;
	}
}
 
Example 6
Source File: Model.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
public void render(GL2 gl2) {
	if(unloadASAP) {
		unloadASAP=false;
		unload(gl2);
	}
	if(!isLoaded) {
		createBuffers(gl2);
		isDirty=true;
		isLoaded=true;
	}
	if(isDirty) {
		updateBuffers(gl2);
		isDirty=false;
	}
	if(VBO==null) return;
	
	int vboIndex=0;
	gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
	// Bind the vertex buffer to work with
	gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex++]);
	gl2.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
    
	if(hasNormals) {
		gl2.glEnableClientState(GL2.GL_NORMAL_ARRAY);
		// Bind the normal buffer to work with
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex++]);
		gl2.glNormalPointer(GL2.GL_FLOAT, 0, 0);
	}
	if(hasColors) {
		gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
		// Bind the color buffer to work with
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex++]);
		gl2.glColorPointer(4,GL2.GL_FLOAT, 0, 0);
	}
	if(hasUVs) {
		gl2.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
		// Bind the texture buffer to work with
		gl2.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBO[vboIndex++]);
		gl2.glTexCoordPointer(2, GL2.GL_FLOAT, 0, 0);
	}
	
	int count=vertexArray.size()/3;
	if(renderStyle==GL2.GL_POINTS) {
		count*=3;
	}
	gl2.glDrawArrays(renderStyle, 0, count);
	//gl2.glDrawArrays(GL2.GL_LINE_LOOP, 0, count);
	
	gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY);
	gl2.glDisableClientState(GL2.GL_NORMAL_ARRAY);
	gl2.glDisableClientState(GL2.GL_COLOR_ARRAY);
	gl2.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
}