Java Code Examples for java.nio.ByteBuffer#asFloatBuffer()
The following examples show how to use
java.nio.ByteBuffer#asFloatBuffer() .
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: Trajectory.java From ParaViewTangoRecorder with Apache License 2.0 | 6 votes |
public Trajectory(int lineWidth, float[] color) { mLineWidth = lineWidth; mColor = color; // Reset the model matrix to the identity Matrix.setIdentityM(getModelMatrix(), 0); // Allocate a vertex buffer ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(MAX_VERTICES * BYTES_PER_FLOAT); vertexByteBuffer.order(ByteOrder.nativeOrder()); mVertexBuffer = vertexByteBuffer.asFloatBuffer(); // Load the vertex and fragment shaders, then link the program int vertexShader = RenderUtils.loadShader(GLES20.GL_VERTEX_SHADER, mVertexShaderCode); int fragShader = RenderUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderCode); mProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(mProgram, vertexShader); GLES20.glAttachShader(mProgram, fragShader); GLES20.glLinkProgram(mProgram); }
Example 2
Source File: VideoTextureRenderer.java From AndroidOpenGLVideoDemo with Apache License 2.0 | 6 votes |
private void setupTexture() { ByteBuffer texturebb = ByteBuffer.allocateDirect(TEXTURE_COORDINATES.length * 4); texturebb.order(ByteOrder.nativeOrder()); textureBuffer = texturebb.asFloatBuffer(); textureBuffer.put(TEXTURE_COORDINATES); textureBuffer.position(0); // Generate the actual texture GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glGenTextures(1, textures, 0); checkGlError("Texture generate"); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textures[0]); checkGlError("Texture bind"); videoTexture = new SurfaceTexture(textures[0]); videoTexture.setOnFrameAvailableListener(this); }
Example 3
Source File: GeoTiff.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
int writeData(float[] data, int imageNumber) throws IOException { if (file == null) init(); if (imageNumber == 1) channel.position(headerSize); else channel.position(nextOverflowData); // no way around making a copy ByteBuffer direct = ByteBuffer.allocateDirect(4 * data.length); FloatBuffer buffer = direct.asFloatBuffer(); buffer.put(data); // buffer.flip(); channel.write(direct); if (imageNumber == 1) firstIFD = headerSize + 4 * data.length; else firstIFD = 4 * data.length + nextOverflowData; return nextOverflowData; }
Example 4
Source File: GeometrySimplifier.java From BIMserver with GNU Affero General Public License v3.0 | 5 votes |
private boolean matchSameOrder(GeometryData geometryDate, GeometryData d) { ByteBuffer bb1 = ByteBuffer.wrap(geometryDate.getVertices().getData()); bb1.order(ByteOrder.nativeOrder()); FloatBuffer buffer1 = bb1.asFloatBuffer(); ByteBuffer bb2 = ByteBuffer.wrap(d.getVertices().getData()); bb2.order(ByteOrder.nativeOrder()); FloatBuffer buffer2 = bb2.asFloatBuffer(); float lastX1 = buffer1.get(0); float lastY1 = buffer1.get(1); float lastZ1 = buffer1.get(2); float lastX2 = buffer2.get(0); float lastY2 = buffer2.get(1); float lastZ2 = buffer2.get(2); for (int i=3; i<buffer1.capacity(); i+=3) { double distance1 = Math.sqrt(Math.pow(buffer1.get(i) - lastX1, 2) + Math.pow(buffer1.get(i+1) - lastY1, 2) + Math.pow(buffer1.get(i+2) - lastZ1, 2)); double distance2 = Math.sqrt(Math.pow(buffer2.get(i) - lastX2, 2) + Math.pow(buffer2.get(i+1) - lastY2, 2) + Math.pow(buffer2.get(i+2) - lastZ2, 2)); double diff = distance1 - distance2; if (diff > 0.1 || diff < -0.1) { return false; } lastX1 = buffer1.get(i); lastY1 = buffer1.get(i+1); lastZ1 = buffer1.get(i+2); lastX2 = buffer2.get(i); lastY2 = buffer2.get(i+1); lastZ2 = buffer2.get(i+2); } return true; }
Example 5
Source File: Square.java From opengl with Apache License 2.0 | 5 votes |
/** * Sets up the drawing object data for use in an OpenGL ES context. */ public Square() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (# of coordinate values * 4 bytes per float) squareCoords.length * 4); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(squareCoords); vertexBuffer.position(0); // initialize byte buffer for the draw list ByteBuffer dlb = ByteBuffer.allocateDirect( // (# of coordinate values * 2 bytes per short) drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); // prepare shaders and OpenGL program int vertexShader = MyGLRenderer.loadShader( GLES31.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyGLRenderer.loadShader( GLES31.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES31.glCreateProgram(); // create empty OpenGL Program GLES31.glAttachShader(mProgram, vertexShader); // add the vertex shader to program GLES31.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program GLES31.glLinkProgram(mProgram); // create OpenGL program executables }
Example 6
Source File: Square.java From opengl with Apache License 2.0 | 5 votes |
/** * Sets up the drawing object data for use in an OpenGL ES context. */ public Square() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (# of coordinate values * 4 bytes per float) squareCoords.length * 4); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(squareCoords); vertexBuffer.position(0); // initialize byte buffer for the draw list ByteBuffer dlb = ByteBuffer.allocateDirect( // (# of coordinate values * 2 bytes per short) drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); // prepare shaders and OpenGL program int vertexShader = MyGLRenderer.loadShader( GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyGLRenderer.loadShader( GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program GLES20.glLinkProgram(mProgram); // create OpenGL program executables }
Example 7
Source File: PLUtils.java From panoramagl with Apache License 2.0 | 5 votes |
public static FloatBuffer makeFloatBuffer(float[] array) { final int floatSize = Float.SIZE / 8; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * floatSize); byteBuffer.order(ByteOrder.nativeOrder()); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); floatBuffer.put(array); floatBuffer.position(0); return floatBuffer; }
Example 8
Source File: Triangle.java From recordablesurfaceview with Apache License 2.0 | 5 votes |
/** * Sets up the drawing object data for use in an OpenGL ES context. */ public Triangle() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (number of coordinate values * 4 bytes per float) triangleCoords.length * 4); // use the device hardware's native byte order bb.order(ByteOrder.nativeOrder()); // create a floating point buffer from the ByteBuffer vertexBuffer = bb.asFloatBuffer(); // add the coordinates to the FloatBuffer vertexBuffer.put(triangleCoords); // set the buffer to read the first coordinate vertexBuffer.position(0); // prepare shaders and OpenGL program int vertexShader = MyGLRenderer.loadShader( GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyGLRenderer.loadShader( GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program GLES20.glLinkProgram(mProgram); // create OpenGL program executables }
Example 9
Source File: BufferTest.java From j2objc with Apache License 2.0 | 5 votes |
private void testAsMethods(ByteBuffer b) { b.asCharBuffer(); b.asDoubleBuffer(); b.asFloatBuffer(); b.asIntBuffer(); b.asLongBuffer(); b.asReadOnlyBuffer(); b.asShortBuffer(); }
Example 10
Source File: TypedArrayFunctions.java From es6draft with MIT License | 5 votes |
private static final FloatBuffer asFloatBuffer(ArrayBuffer buffer) { ByteBuffer data = byteBuffer(buffer); int byteLength = byteLength(buffer); data.limit(byteLength).position(0); FloatBuffer view = data.asFloatBuffer(); data.clear(); return view; }
Example 11
Source File: GlUtil.java From sealrtc-android with MIT License | 5 votes |
public static FloatBuffer createFloatBuffer(float[] coords) { // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(coords); fb.position(0); return fb; }
Example 12
Source File: GlUtil.java From AndroidPlayground with MIT License | 5 votes |
/** * Allocates a direct float buffer, and populates it with the float array data. */ public static FloatBuffer createFloatBuffer(float[] coords) { // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(coords); fb.position(0); return fb; }
Example 13
Source File: CurlMesh.java From Android-Example with Apache License 2.0 | 4 votes |
/** * Constructor for mesh object. * * @param maxCurlSplits * Maximum number curl can be divided into. The bigger the value * the smoother curl will be. With the cost of having more * polygons for drawing. */ public CurlMesh(int maxCurlSplits) { // There really is no use for 0 splits. mMaxCurlSplits = maxCurlSplits < 1 ? 1 : maxCurlSplits; mArrScanLines = new Array<Double>(maxCurlSplits + 2); mArrOutputVertices = new Array<Vertex>(7); mArrRotatedVertices = new Array<Vertex>(4); mArrIntersections = new Array<Vertex>(2); mArrTempVertices = new Array<Vertex>(7 + 4); for (int i = 0; i < 7 + 4; ++i) { mArrTempVertices.add(new Vertex()); } if (DRAW_SHADOW) { mArrSelfShadowVertices = new Array<ShadowVertex>( (mMaxCurlSplits + 2) * 2); mArrDropShadowVertices = new Array<ShadowVertex>( (mMaxCurlSplits + 2) * 2); mArrTempShadowVertices = new Array<ShadowVertex>( (mMaxCurlSplits + 2) * 2); for (int i = 0; i < (mMaxCurlSplits + 2) * 2; ++i) { mArrTempShadowVertices.add(new ShadowVertex()); } } // Rectangle consists of 4 vertices. Index 0 = top-left, index 1 = // bottom-left, index 2 = top-right and index 3 = bottom-right. for (int i = 0; i < 4; ++i) { mRectangle[i] = new Vertex(); } // Set up shadow penumbra direction to each vertex. We do fake 'self // shadow' calculations based on this information. mRectangle[0].mPenumbraX = mRectangle[1].mPenumbraX = mRectangle[1].mPenumbraY = mRectangle[3].mPenumbraY = -1; mRectangle[0].mPenumbraY = mRectangle[2].mPenumbraX = mRectangle[2].mPenumbraY = mRectangle[3].mPenumbraX = 1; if (DRAW_CURL_POSITION) { mCurlPositionLinesCount = 3; ByteBuffer hvbb = ByteBuffer .allocateDirect(mCurlPositionLinesCount * 2 * 2 * 4); hvbb.order(ByteOrder.nativeOrder()); mBufCurlPositionLines = hvbb.asFloatBuffer(); mBufCurlPositionLines.position(0); } // There are 4 vertices from bounding rect, max 2 from adding split line // to two corners and curl consists of max mMaxCurlSplits lines each // outputting 2 vertices. int maxVerticesCount = 4 + 2 + (2 * mMaxCurlSplits); ByteBuffer vbb = ByteBuffer.allocateDirect(maxVerticesCount * 3 * 4); vbb.order(ByteOrder.nativeOrder()); mBufVertices = vbb.asFloatBuffer(); mBufVertices.position(0); if (DRAW_TEXTURE) { ByteBuffer tbb = ByteBuffer .allocateDirect(maxVerticesCount * 2 * 4); tbb.order(ByteOrder.nativeOrder()); mBufTexCoords = tbb.asFloatBuffer(); mBufTexCoords.position(0); } ByteBuffer cbb = ByteBuffer.allocateDirect(maxVerticesCount * 4 * 4); cbb.order(ByteOrder.nativeOrder()); mBufColors = cbb.asFloatBuffer(); mBufColors.position(0); if (DRAW_SHADOW) { int maxShadowVerticesCount = (mMaxCurlSplits + 2) * 2 * 2; ByteBuffer scbb = ByteBuffer .allocateDirect(maxShadowVerticesCount * 4 * 4); scbb.order(ByteOrder.nativeOrder()); mBufShadowColors = scbb.asFloatBuffer(); mBufShadowColors.position(0); ByteBuffer sibb = ByteBuffer .allocateDirect(maxShadowVerticesCount * 3 * 4); sibb.order(ByteOrder.nativeOrder()); mBufShadowVertices = sibb.asFloatBuffer(); mBufShadowVertices.position(0); mDropShadowCount = mSelfShadowCount = 0; } }
Example 14
Source File: NewLineRenderable.java From constellation with Apache License 2.0 | 4 votes |
/** * Draws the new line on the display. * <p> * @param drawable The OpenGL rendering target. * @param pMatrix The model view projection matrix. */ @Override public void display(final GLAutoDrawable drawable, final Matrix44f pMatrix) { final Matrix44f mvpMatrix = parent.getDisplayModelViewProjectionMatrix(); // If no endpoints are set, don't draw anything if (model != null && !model.isClear()) { final Vector3f startPosition = model.getStartLocation(); final Vector3f endPosition = model.getEndLocation(); final GL3 gl = drawable.getGL().getGL3(); gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, batch.getBufferName(vertexTarget)); ByteBuffer bbuf = gl.glMapBuffer(GL3.GL_ARRAY_BUFFER, GL3.GL_WRITE_ONLY); FloatBuffer fbuf = bbuf.asFloatBuffer(); // Update the line endpoints in the vertex buffer. float[] vertices = new float[]{ startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()}; fbuf.put(vertices); gl.glUnmapBuffer(GL3.GL_ARRAY_BUFFER); // Disable depth so the line is drawn over everything else. gl.glDisable(GL3.GL_DEPTH_TEST); gl.glDepthMask(false); // Draw the line. gl.glLineWidth(NEW_LINE_WIDTH); gl.glUseProgram(shader); gl.glUniformMatrix4fv(shaderMVP, 1, false, mvpMatrix.a, 0); batch.draw(gl); gl.glLineWidth(1); // Reenable depth. gl.glEnable(GL3.GL_DEPTH_TEST); gl.glDepthMask(true); // Rebind default array buffer gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); } }
Example 15
Source File: Sphere.java From Fatigue-Detection with MIT License | 4 votes |
/** * modified from hzqiujiadi on 16/1/8. * original source code: * https://github.com/shulja/viredero/blob/a7d28b21d762e8479dc10cde1aa88054497ff649/viredroid/src/main/java/org/viredero/viredroid/Sphere.java * @param radius 半径,半径应该在远平面和近平面之间 * @param rings * @param sectors */ public Sphere(float radius, int rings, int sectors) { final float PI = (float) Math.PI; final float PI_2 = (float) (Math.PI / 2); float R = 1f/(float)rings; float S = 1f/(float)sectors; short r, s; float x, y, z; int numPoint = (rings + 1) * (sectors + 1); float[] vertexs = new float[numPoint * 3]; float[] texcoords = new float[numPoint * 2]; short[] indices = new short[numPoint * 6]; //纹理映射2d-3d int t = 0, v = 0; for(r = 0; r < rings + 1; r++) { for(s = 0; s < sectors + 1; s++) { x = (float) (Math.cos(2*PI * s * S) * Math.sin( PI * r * R )); y = (float) Math.sin( -PI_2 + PI * r * R ); z = (float) (Math.sin(2*PI * s * S) * Math.sin( PI * r * R )); texcoords[t++] = s*S; texcoords[t++] = r*R; vertexs[v++] = x * radius; vertexs[v++] = y * radius; vertexs[v++] = z * radius; } } //球体绘制坐标索引,用于 glDrawElements int counter = 0; int sectorsPlusOne = sectors + 1; for(r = 0; r < rings; r++){ for(s = 0; s < sectors; s++) { indices[counter++] = (short) (r * sectorsPlusOne + s); //(a) indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s)); //(b) indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1)); // (sendLoopMsg) indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1)); // (sendLoopMsg) indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s)); //(b) indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s+1)); // (d) } } // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (# of coordinate values * 4 bytes per float) vertexs.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(vertexs); vertexBuffer.position(0); // initialize vertex byte buffer for shape coordinates ByteBuffer cc = ByteBuffer.allocateDirect( texcoords.length * 4); cc.order(ByteOrder.nativeOrder()); FloatBuffer texBuffer = cc.asFloatBuffer(); texBuffer.put(texcoords); texBuffer.position(0); // initialize byte buffer for the draw list ByteBuffer dlb = ByteBuffer.allocateDirect( // (# of coordinate values * 2 bytes per short) indices.length * 2); dlb.order(ByteOrder.nativeOrder()); indexBuffer = dlb.asShortBuffer(); indexBuffer.put(indices); indexBuffer.position(0); mTexCoordinateBuffer=texBuffer; mVerticesBuffer=vertexBuffer; mNumIndices=indices.length; }
Example 16
Source File: BufferUtil.java From oreon-engine with GNU General Public License v3.0 | 4 votes |
public static ByteBuffer createByteBuffer(Vertex[] vertices, VertexLayout layout){ ByteBuffer byteBuffer = allocateVertexByteBuffer(layout, vertices.length); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); for(int i = 0; i < vertices.length; i++) { if (layout == VertexLayout.POS2D || layout == VertexLayout.POS2D_UV){ floatBuffer.put(vertices[i].getPosition().getX()); floatBuffer.put(vertices[i].getPosition().getY()); } else { floatBuffer.put(vertices[i].getPosition().getX()); floatBuffer.put(vertices[i].getPosition().getY()); floatBuffer.put(vertices[i].getPosition().getZ()); } if (layout == VertexLayout.POS_NORMAL || layout == VertexLayout.POS_NORMAL_UV || layout == VertexLayout.POS_NORMAL_UV_TAN_BITAN){ floatBuffer.put(vertices[i].getNormal().getX()); floatBuffer.put(vertices[i].getNormal().getY()); floatBuffer.put(vertices[i].getNormal().getZ()); } if (layout == VertexLayout.POS_NORMAL_UV || layout == VertexLayout.POS_UV || layout == VertexLayout.POS_NORMAL_UV_TAN_BITAN || layout == VertexLayout.POS2D_UV){ floatBuffer.put(vertices[i].getUVCoord().getX()); floatBuffer.put(vertices[i].getUVCoord().getY()); } if (layout == VertexLayout.POS_NORMAL_UV_TAN_BITAN){ floatBuffer.put(vertices[i].getTangent().getX()); floatBuffer.put(vertices[i].getTangent().getY()); floatBuffer.put(vertices[i].getTangent().getZ()); floatBuffer.put(vertices[i].getBitangent().getX()); floatBuffer.put(vertices[i].getBitangent().getY()); floatBuffer.put(vertices[i].getBitangent().getZ()); } } return byteBuffer; }
Example 17
Source File: PageFront.java From PlayLikeCurl with MIT License | 4 votes |
@Override public void calculateVerticesCoords(){ super.calculateVerticesCoords(); float angle = 1.0f/((float)GRID*RADIUS); for(int row=0;row<=GRID;row++) for(int col=0;col<=GRID;col++){ int pos = 3*(row*(GRID+1)+col); if(!isactive()) vertices[pos+2]=depth; float perc = 1.0f-curlCirclePosition/(float)GRID; float dx=(GRID-curlCirclePosition); float calc_r = perc*RADIUS; if(calc_r>RADIUS) calc_r=RADIUS; calc_r=RADIUS*1; float mov_x=0; if(perc<0.20f) calc_r=RADIUS*perc*5; if(perc>0.05f) mov_x=perc-0.05f; if(isactive()) vertices[pos+2]=(float) (calc_r*(Math.sin(3.14/(GRID*0.60f)*(col-(dx))))+(calc_r*1.1f)); //Asin(2pi/wav*x) float w_h_ratio = 1-calc_r; vertices[pos]=((float)col/(float)GRID*w_h_ratio)-mov_x; vertices[pos+1]=((float)row/(float)GRID*h_w_ratio)-h_w_correction; } ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); }
Example 18
Source File: NativeFloat32Array.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
@Override public Float32ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { return new Float32ArrayData(nb.asFloatBuffer(), start, end); }
Example 19
Source File: BackgroundRenderer.java From augmentedreality with Apache License 2.0 | 4 votes |
/** * Allocates and initializes OpenGL resources needed by the background renderer. Must be called on * the OpenGL thread, typically in {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, * EGLConfig)}. * * @param context Needed to access shader source. */ public void createOnGlThread(Context context) { // Generate the background texture. int[] textures = new int[1]; GLES20.glGenTextures(1, textures, 0); textureId = textures[0]; int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; GLES20.glBindTexture(textureTarget, textureId); GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); int numVertices = 4; if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) { throw new RuntimeException("Unexpected number of vertices in BackgroundRenderer."); } ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE); bbVertices.order(ByteOrder.nativeOrder()); quadVertices = bbVertices.asFloatBuffer(); quadVertices.put(QUAD_COORDS); quadVertices.position(0); ByteBuffer bbTexCoords = ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); bbTexCoords.order(ByteOrder.nativeOrder()); quadTexCoord = bbTexCoords.asFloatBuffer(); quadTexCoord.put(QUAD_TEXCOORDS); quadTexCoord.position(0); ByteBuffer bbTexCoordsTransformed = ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); quadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer(); int vertexShader = ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, R.raw.screenquad_vertex); int fragmentShader = ShaderUtil.loadGLShader( TAG, context, GLES20.GL_FRAGMENT_SHADER, R.raw.screenquad_fragment_oes); quadProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(quadProgram, vertexShader); GLES20.glAttachShader(quadProgram, fragmentShader); GLES20.glLinkProgram(quadProgram); GLES20.glUseProgram(quadProgram); ShaderUtil.checkGLError(TAG, "Program creation"); quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); ShaderUtil.checkGLError(TAG, "Program parameters"); }
Example 20
Source File: ZGDanmaku.java From ZGDanmaku with Apache License 2.0 | 4 votes |
/** * 初始化顶点坐标与纹理坐标 */ public void initVertexData() { if (mBitmap == null) { return; } //顶点坐标数据 //顶点坐标系:窗口取值范围是-1至1,所以,左上角坐标是(-1,1),中点坐标是(0,0),右下角坐标是(1, -1) //其实就是把坐标给归一化了,下面是计算弹幕的归一化宽和高 //实际上顶点坐标是正负1的,所以弹幕在坐标系中要乘以2,放大两倍 float danmakuHeight = (float) mBitmap.getHeight() / mViewHeight * 2.0f; float danmakuWidth = (float) mBitmap.getWidth() / mViewWidth * 2.0f; //弹幕四个角的顶点坐标,我默认把它绘制在屏幕的左上角了,这样方便理解偏移计算 //为什么不是顺时针或者逆时针,实际上opengl只能绘制三角形,所以, //这里其实是绘制了两个三角形的,前三个点和后三个点分别是三角形 float vertices[] = new float[] { -1, 1, 0, //左上角 -(1 - danmakuWidth), 1, 0, //右上角 -1, 1 - danmakuHeight, 0, //左下角 -(1 - danmakuWidth), 1 - danmakuHeight, 0 //右下角 }; //一个float是4个字节,所以*4 //关于ByteBuffer,可以看nio相关知识:http://zhgeaits.me/java/2013/06/17/Java-nio.html ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer = vbb.asFloatBuffer(); mVertexBuffer.put(vertices); mVertexBuffer.position(0); //把纹理绘制到矩形中去,就需要指定读取纹理的坐标 //纹理坐标系:范围是0-1,左上角坐标是(0,0),右下角坐标是(1,1) float texCoor[] = new float[] { 0, 0, //左上角 1, 0, //右上角 0, 1, //左下角 1, 1 //右下角 }; ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4); cbb.order(ByteOrder.nativeOrder()); mTexCoorBuffer = cbb.asFloatBuffer(); mTexCoorBuffer.put(texCoor); mTexCoorBuffer.position(0); }