@Override public void onDrawFrame(GL10 gl) { glClear(GL_COLOR_BUFFER_BIT); // JMIM EDIT: // Just a guess: // vData.position(0); <--This will cause crash. // page 51: glDrawArrays will draw TWO(2) triangles // because it has been instructed to read in SIX(6) verticies. glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f); glDrawArrays(GL_TRIANGLES, 0, 6); // Drawing dividing lines: Page 52: glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 0.0f); glDrawArrays(GL_TRIANGLES, 6, 2); // blue mallet: glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f); glDrawArrays(GL_POINTS, 8, 1); // red mallet: glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f); glDrawArrays(GL_POINTS, 9, 1); // http://stackoverflow.com/questions/4679405/gldrawarrays-problem // JMIM HACK: Attempt to fix: // glBindBuffer(GL_ARRAY_BUFFER,0); //<--Causes crash. }
public void draw() { // Add program to OpenGL environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // Enable a handle to the scene vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // Prepare the scene coordinate data GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, vertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // Set colorVertices for drawing the scene GLES20.glUniform4fv(mColorHandle, 1, colorVertices, 0); // Get handle to shape's transformation matrix int mtrxhandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // Apply the projection and view transformation GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, pointsRenderer.mtrxProjectionAndView, 0); // Draw the scene GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vertexCount); // Disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); if (drawLines) { GLES20.glUseProgram(mLinesProgram); mLinesPositionHandle = GLES20.glGetAttribLocation(mLinesProgram, "vPosition"); GLES20.glEnableVertexAttribArray(mLinesPositionHandle); // Prevent null pointer race condition if (linesVertexBuffer != null) { GLES20.glVertexAttribPointer( mLinesPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, linesVertexBuffer); mLinesColorHandle = GLES20.glGetUniformLocation(mLinesProgram, "vColor"); GLES20.glUniform4fv(mLinesColorHandle, 1, colorLines, 0); int mtrxLineshandle = GLES20.glGetUniformLocation(mLinesProgram, "uMVPMatrix"); GLES20.glUniformMatrix4fv( mtrxLineshandle, 1, false, pointsRenderer.mtrxProjectionAndView, 0); // GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, linesVertexCount); // GLES20.glDrawArrays(GLES20.GL_LINES, 0, linesVertexCount); // GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, linesVertexCount); // GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, linesVertexCount); GLES20.glDrawArrays(renderMethod, 0, linesVertexCount); GLES20.glDisableVertexAttribArray(mLinesPositionHandle); } } }
public boolean Draw(Matrix4 projectionMatrix, Matrix4 viewMatrix) { GLES20.glUseProgram(programID); Matrix4 mvp = Matrix4.Multiply( projectionMatrix, Matrix4.Multiply(viewMatrix, parent.transform.transformMatrix)); float[] mvpArray = mvp.GetOneDimensionalArray(); GLES20.glUniformMatrix4fv(mvpLocation, 1, false, mvpArray, 0); GLES20.glUniform1f(opacityLocation, opacity); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID); GLES20.glUniform1i(textureSamplerLocation, 0); // Log.i(DEBUG_TAG,"Enabling " + vertexArrayList.size() + " attribs"); for (GLVertexArrayObject element : vertexArrayList) { GLES20.glEnableVertexAttribArray(element.attributeNumber); } GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexArraySize); for (GLVertexArrayObject element : vertexArrayList) { GLES20.glDisableVertexAttribArray(element.attributeNumber); } return true; }
public void drawSphere(final FloatBuffer aSphereBuffer) { aSphereBuffer.position(mPositionOffset); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, aSphereBuffer); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information // aTriangleBuffer.position(mColorOffset); // GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, // mStrideBytes, aTriangleBuffer); // // GLES20.glEnableVertexAttribArray(mColorHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, sphereTriangles * 3); }
public void draw(float[] mvpMatrix) { // Add program to OpenGL environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // Enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // Prepare the triangle coordinate data GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // Set color for drawing the triangle GLES20.glUniform4fv(mColorHandle, 1, color, 0); // get handle to shape's transformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); MyGLRenderer.checkGlError("glGetUniformLocation"); // Apply the projection and view transformation GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); MyGLRenderer.checkGlError("glUniformMatrix4fv"); // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); }
public void drawSelf() { MatrixState.rotate(xAngle, 1, 0, 0); // 绕X轴转动 MatrixState.rotate(yAngle, 0, 1, 0); // 绕Y轴转动 MatrixState.rotate(zAngle, 0, 0, 1); // 绕Z轴转动 // 制定使用某套着色器程序 GLES20.glUseProgram(mProgram); // 将最终变换矩阵传入着色器程序 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); // 将位置、旋转变换矩阵传入着色器程序 GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0); // 将光源位置传入着色器程序 GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB); // 将摄像机位置传入着色器程序 GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB); // 将顶点经纬度数据传入渲染管线 GLES20.glVertexAttribPointer(maLongLatHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mLongLatBuffer); // 将顶点位置数据传入渲染管线 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 将顶点法向量数据传入渲染管线 GLES20.glVertexAttribPointer(maNormalHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer); // 启用顶点位置、顶点经纬度数据 GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glEnableVertexAttribArray(maNormalHandle); // 启用顶点法向量数据 GLES20.glEnableVertexAttribArray(maLongLatHandle); // 绘制球 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); }
public void onDraw( final int textureId, final FloatBuffer cubeBuffer, final FloatBuffer textureBuffer) { GLES20.glUseProgram(mGLProgId); runPendingOnDrawTasks(); if (!mIsInitialized) { return; } cubeBuffer.position(0); GLES20.glVertexAttribPointer(mGLAttribPosition, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer); GLES20.glEnableVertexAttribArray(mGLAttribPosition); textureBuffer.position(0); GLES20.glVertexAttribPointer( mGLAttribTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer); GLES20.glEnableVertexAttribArray(mGLAttribTextureCoordinate); if (textureId != OpenGlUtils.NO_TEXTURE) { GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); GLES20.glUniform1i(mGLUniformTexture, 0); } onDrawArraysPre(); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); GLES20.glDisableVertexAttribArray(mGLAttribPosition); GLES20.glDisableVertexAttribArray(mGLAttribTextureCoordinate); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); }
@Override public void onDrawFrame(GL10 unused) { GLES20.glClearColor(0f, 0f, 0f, 0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); if (mShaderCompilerSupport[0] == false) { return; } GLES20.glDisable(GLES20.GL_DEPTH_TEST); GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); float angle = (SystemClock.uptimeMillis() % 5000) / 5000f * 360; Matrix.setRotateM(mMatrixModel, 0, angle, 1, 2, 0); Matrix.multiplyMM(mMatrixModelViewProjection, 0, mMatrixView, 0, mMatrixModel, 0); Matrix.multiplyMM( mMatrixModelViewProjection, 0, mMatrixProjection, 0, mMatrixModelViewProjection, 0); mShaderSpline.useProgram(); GLES20.glUniformMatrix4fv( mShaderSpline.getHandle("uModelViewProjectionM"), 1, false, mMatrixModelViewProjection, 0); GLES20.glVertexAttribPointer( mShaderSpline.getHandle("aPosition"), 2, GLES20.GL_FLOAT, false, 0, mBufferSpline); GLES20.glEnableVertexAttribArray(mShaderSpline.getHandle("aPosition")); for (float[] ctrl : mSplines) { GLES20.glUniform3fv(mShaderSpline.getHandle("uCtrl"), 4, ctrl, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, COUNT_VERTICES); } }
@Override public void draw(GL10 glUnused) { super.draw(glUnused); GLES20.glDisable(GLES20.GL_CULL_FACE); calcMVP(); GLES20.glUniformMatrix4fv(getUniform(ShaderVal.MVP_MATRIX), 1, false, MVP, 0); GLES20.glEnableVertexAttribArray(ShaderVal.POSITION.loc); GLES20.glVertexAttribPointer(ShaderVal.POSITION.loc, 3, GLES20.GL_FLOAT, false, 0, vertices); if (useVertexColors) { GLES20.glEnableVertexAttribArray(ShaderVal.ATTRIB_COLOR.loc); GLES20.glVertexAttribPointer( ShaderVal.ATTRIB_COLOR.loc, 4, GLES20.GL_FLOAT, false, 0, colors); } else { GLES20.glUniform4f( getUniform(ShaderVal.UNIFORM_COLOR), color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); } GLES20.glDrawArrays(drawMode, 0, vertexCount); GLES20.glEnable(GLES20.GL_CULL_FACE); }
public void draw() { // 将program加入OpenGL ES环境中 GLES20.glUseProgram(mProgram); // 获取指向vertex shader的成员vPosition的 handle mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // 启用一个指向三角形的顶点数组的handle GLES20.glEnableVertexAttribArray(mPositionHandle); // 准备三角形的坐标数据 GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // 获取指向fragment shader的成员vColor的handle mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // 设置三角形的颜色 GLES20.glUniform4fv(mColorHandle, 1, color, 0); // 画三角形 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // 禁用指向三角形的顶点数组 GLES20.glDisableVertexAttribArray(mPositionHandle); }
public void draw() { GLES20.glUseProgram(shaderProgram); int muMVPMat = GLES20.glGetUniformLocation(shaderProgram, "uMVPMatrix"); GLESUtils.checkGlError("glGetUniformLocation"); GLES20.glUniformMatrix4fv(muMVPMat, 1, false, scene.modelViewProjectionMatrix(), 0); GLESUtils.checkGlError("glUniformMatrix4fv"); int mPositionH = vertexAttribArrayf("vPosition", CpV, cStride, posBuffer); GLESUtils.checkGlError("vPosition"); int normalH = vertexAttribArrayf("vNormal", CpV, cStride, normalBuffer); GLESUtils.checkGlError("vNormal"); int mColorH = GLES20.glGetAttribLocation(shaderProgram, "vColor"); GLES20.glVertexAttrib4f(mColorH, c[0], c[1], c[2], c[3]); GLESUtils.checkGlError("vColor"); int phaseH = vertexAttribArrayf("vPhase", 1, NumVerts, phaseBuffer); GLESUtils.checkGlError("vPhase"); int gTime = GLES20.glGetUniformLocation(shaderProgram, "iGlobalTime"); GLES20.glUniform1f(gTime, (float) (SystemClock.uptimeMillis() / 1000.0)); GLESUtils.checkGlError("iGlobalTime"); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, oBufferLength); GLES20.glDisableVertexAttribArray(mPositionH); GLES20.glDisableVertexAttribArray(normalH); GLES20.glDisableVertexAttribArray(mColorH); GLES20.glDisableVertexAttribArray(phaseH); }
public void draw(final int mShader) { // Bind Texture VBO GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getTexVBO()); // Pass in texture map position mTexVertBuffer.position(0); final int mTextureCoordinateHandle = GLES20.glGetAttribLocation(mShader, "a_TexCoord"); GLES20.glVertexAttribPointer( mTextureCoordinateHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0); GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); // Bind VBO GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getPlaneVBO()); // Pass in the position information mPlaneVertBuffer.position(0); final int mPositionHandle = GLES20.glGetAttribLocation(mShader, "a_Position"); GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information final int mColorHandle = GLES20.glGetUniformLocation(mShader, "u_Color"); GLES20.glUniform4f(mColorHandle, 0, 1, 1, 1); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, indexCount); // Unbind buffers GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void drawCube() { GLES20.glUseProgram(cubeProgram); checkGLError("Use program"); GLES20.glUniform3fv(cubeLightPosParam, 1, lightPosInEyeSpace, 0); // Set the Model in the shader, used to calculate lighting GLES20.glUniformMatrix4fv(cubeModelParam, 1, false, mModelCube, 0); // Set the ModelView in the shader, used to calculate lighting GLES20.glUniformMatrix4fv(cubeModelViewParam, 1, false, modelView, 0); // Set the position of the cube GLES20.glVertexAttribPointer( cubePositionParam, COORDS_PER_VERTEX_CUBE, GLES20.GL_FLOAT, false, 0, cubeVertices); // Set the ModelViewProjection matrix in the shader. GLES20.glUniformMatrix4fv(cubeModelViewProjectionParam, 1, false, modelViewProjection, 0); // Set the normal positions of the cube, again for shading GLES20.glVertexAttribPointer(cubeNormalParam, 3, GLES20.GL_FLOAT, false, 0, cubeNormals); GLES20.glVertexAttribPointer(cubeColorParam, 4, GLES20.GL_FLOAT, false, 0, cubeColors); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36); checkGLError("Drawing cube"); }
public void draw() { // Add program to OpenGLES environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // Enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // Prepare the triangle coordinate data GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, mVertexStride, mVertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // set color for drawing the triangle GLES20.glUniform4fv(mColorHandle, 1, mColor, 0); // draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mVertexCount); // disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); }
public void drawSelf(int texId) { MatrixState.rotate(xAngle, 1, 0, 0); MatrixState.rotate(yAngle, 0, 1, 0); MatrixState.rotate(zAngle, 0, 0, 1); // 制定使用某套shader程序 GLES20.glUseProgram(mProgram); // 将最终变换矩阵传入shader程序 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); // 传送顶点位置数据 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 传送顶点纹理坐标数据 GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer); // 启用顶点位置数据 GLES20.glEnableVertexAttribArray(maPositionHandle); // 启用顶点纹理数据 GLES20.glEnableVertexAttribArray(maTexCoorHandle); // 绑定纹理 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId); // 绘制纹理矩形 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); }
public void drawSelf(int texId) { // 制定使用某套shader程序 GLES20.glUseProgram(mProgram); // 将最终变换矩阵传入shader程序 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); // 将位置、旋转变换矩阵传入shader程序 GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0); // 将摄像机位置传入shader程序 GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB); // 将光源位置传入shader程序 GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB); // 传送顶点位置数据 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 传送顶点纹理坐标数据 GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer); // 传送顶点法向量数据 GLES20.glVertexAttribPointer(maNormalHandle, 4, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer); // 启用顶点位置数据 GLES20.glEnableVertexAttribArray(maPositionHandle); // 启用顶点纹理数据 GLES20.glEnableVertexAttribArray(maTexCoorHandle); // 启用顶点法向量数据 GLES20.glEnableVertexAttribArray(maNormalHandle); // 绑定纹理 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId); // 绘制纹理矩形 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); }
/** * Method for painting any given object. Data must be 7 floats per face – we render one triangle * consisting of 3 vertices with each vertice having an rgba color float value. * * @param data The vertice data containing coordinates and colors to draw. */ private void drawObject(final FloatBuffer data) { // Pass in the position information data.position(mPositionOffset); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information data.position(mColorOffset); GLES20.glVertexAttribPointer( mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data); GLES20.glEnableVertexAttribArray(mColorHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // The (1+data.capacity() / 8) tells us how many vertices we need to // draw GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, (1 + data.capacity() / 8)); }
/** * OnDrawFrame is called whenever a new frame needs to be drawn. Normally, this is done at the * refresh rate of the screen. */ @Override public void onDrawFrame(GL10 glUnused) { // Clear the rendering surface. glClear(GL_COLOR_BUFFER_BIT); // Draw the table. glDrawArrays(GL_TRIANGLE_FAN, 0, 6); // Draw the center dividing line. glDrawArrays(GL_LINES, 6, 2); // Draw the first mallet. glDrawArrays(GL_POINTS, 8, 1); // Draw the second mallet. glDrawArrays(GL_POINTS, 9, 1); }
@Override public void onDrawFrame(GL10 unused) { GLES20.glClearColor(0f, 0f, 0f, 1f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); if (mShaderCompilerSupport[0] == false) { return; } GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); long time = SystemClock.uptimeMillis(); if (mLastRenderTime == -1) { for (Star star : mStarArray) { star.randomize(); star.mPosition[2] = (float) (Math.random() * 4 - 2); } mLastRenderTime = time; } float t = (time - mLastRenderTime) / 1000f; mLastRenderTime = time; mShaderStar.useProgram(); GLES20.glUniformMatrix4fv( mShaderStar.getHandle("uModelViewProjectionM"), 1, false, mMatrixModelViewProjection, 0); GLES20.glUniform1f(mShaderStar.getHandle("uSize"), .01f); GLES20.glVertexAttribPointer( mShaderStar.getHandle("aPosition"), 2, GLES20.GL_BYTE, false, 0, mBufferVertices); GLES20.glEnableVertexAttribArray(mShaderStar.getHandle("aPosition")); for (Star star : mStarArray) { star.mPosition[2] += star.mSpeed * t; if (star.mPosition[2] > 2) { star.randomize(); } } Arrays.sort( mStarArray, new Comparator<Star>() { @Override public int compare(Star arg0, Star arg1) { return arg0.mPosition[2] < arg1.mPosition[2] ? -1 : 1; } }); for (Star star : mStarArray) { GLES20.glUniform3fv(mShaderStar.getHandle("uPosition"), 1, star.mPosition, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); } GLES20.glDisable(GLES20.GL_BLEND); }
/** Draws a cube. */ private void drawCube() { // Pass in the position information mCubePositions.position(0); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mCubePositions); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information mCubeColors.position(0); GLES20.glVertexAttribPointer( mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mCubeColors); GLES20.glEnableVertexAttribArray(mColorHandle); // Pass in the normal information mCubeNormals.position(0); GLES20.glVertexAttribPointer( mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, mCubeNormals); GLES20.glEnableVertexAttribArray(mNormalHandle); // Pass in the texture coordinate information mCubeTextureCoordinates.position(0); GLES20.glVertexAttribPointer( mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, mCubeTextureCoordinates); GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // Pass in the modelview matrix. GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); // Pass in the combined matrix. GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Pass in the light position in eye space. GLES20.glUniform3f( mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]); // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36); }
/** * draw specific texture with specific texture matrix * * @param tex_id texture ID * @param tex_matrix texture matrix、if this is null, the last one use(we don't check size of this * array and needs at least 16 of float) */ public void draw(final int tex_id, final float[] tex_matrix) { GLES20.glUseProgram(hProgram); if (tex_matrix != null) GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, tex_matrix, 0); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, tex_id); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, VERTEX_NUM); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); GLES20.glUseProgram(0); }
public void onDrawFrame(GL10 glUnused) { synchronized (this) { if (updateSurface) { mSurface.updateTexImage(); mSurface.getTransformMatrix(mSTMatrix); updateSurface = false; } else { return; } } GLES20.glClearColor(255.0f, 255.0f, 255.0f, 1.0f); GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); GLES20.glUseProgram(mProgram); checkGlError("glUseProgram"); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID); mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); GLES20.glVertexAttribPointer( maPositionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices); checkGlError("glVertexAttribPointer maPosition"); GLES20.glEnableVertexAttribArray(maPositionHandle); checkGlError("glEnableVertexAttribArray maPositionHandle"); mTextureVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET); GLES20.glVertexAttribPointer( maTextureHandle, 2, GLES20.GL_FLOAT, false, TEXTURE_VERTICES_DATA_STRIDE_BYTES, mTextureVertices); checkGlError("glVertexAttribPointer maTextureHandle"); GLES20.glEnableVertexAttribArray(maTextureHandle); checkGlError("glEnableVertexAttribArray maTextureHandle"); Matrix.setIdentityM(mMVPMatrix, 0); GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); checkGlError("glDrawArrays"); GLES20.glFinish(); }
public void drawSelf() { GLES20.glUseProgram(mProgram); // 制定使用某套着色器程序 // 将最终变换矩阵传入着色器程序 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); GLES20.glUniform1f(uPointSizeHandle, scale); // 将顶点尺寸传入着色器程序 GLES20.glVertexAttribPointer( // 为画笔指定顶点位置数据 maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 允许顶点位置数据数组 GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vCount); // 绘制星星点 }
/** * Simple recursive method to draw a node and its subnodes * * @param nodeInstance The node instance */ private void drawNode(Node nodeInstance) { MatrixUtils.multiplyMM(this.lightMvpCache, 48, this.lightMvpCache, 32, nodeInstance.model, 0); GLES20.glUniformMatrix4fv(this.u_lightMvpMatrixMat4Handle, 1, false, this.lightMvpCache, 48); if (nodeInstance.geometryInstances != null) { for (GeometryInstance geometryInstance : nodeInstance.geometryInstances) { for (Element element : geometryInstance.geometry.elements) { if (element.handle == GlBuffer.UNBIND_HANDLE) { GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, GlBuffer.UNBIND_HANDLE); final GlBuffer<float[]> elementBuffer = BufferUtils.elementToGlBuffer(element); elementBuffer.position(elementBuffer.chunks[0]); GLES20.glVertexAttribPointer( this.a_PositionVec4Handle, elementBuffer.chunks[0].components, elementBuffer.datatype, false, elementBuffer.stride, elementBuffer.data); GLES20.glDrawArrays(element.type, 0, element.count); elementBuffer.free(); } else { GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, element.handle); GLES20Utils.glVertexAttribPointer( this.a_PositionVec4Handle, element.inputs[0][GlAssets.Geometry.Element.SIZE], GLES20.GL_FLOAT, false, element.stride, element.inputs[0][GlAssets.Geometry.Element.OFFSET]); GLES20.glDrawArrays(element.type, 0, element.count); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, GlBuffer.UNBIND_HANDLE); } } } } if (nodeInstance.nodeInstances != null) { for (Node childNode : nodeInstance.nodeInstances) { this.drawNode(childNode); } } }
// Draw |textures| using |vertices| (X,Y coordinates). private void drawRectangle(int[] textures, FloatBuffer vertices) { for (int i = 0; i < 3; ++i) { GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]); } GLES20.glVertexAttribPointer(posLocation, 2, GLES20.GL_FLOAT, false, 0, vertices); GLES20.glEnableVertexAttribArray(posLocation); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); checkNoGLES2Error(); }
public void draw(GL10 gl) { // Set our vertex/frag shader program. GLES20.glUseProgram(mProgram); // Set program handles for drawing. // mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_Position"); // mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate"); mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture"); // Set the active texture unit to texture unit 0. // GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // Bind the texture to this unit. // GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); vertexBuffer.rewind(); textureBuffer.rewind(); // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit // 0. GLES20.glUniform1i(mTextureUniformHandle, 0); // Point to our buffers // gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Point to our vertex buffer // gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); // gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBufferPointer); // first parameter is the ID number of the attribute of the vertex // we set attribute id = 0 to the position and 1 to the texture coordin GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, 0); GLES20.glEnableVertexAttribArray(0); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, textureBufferPointer); GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 0, 0); GLES20.glEnableVertexAttribArray(1); // Set the face rotation // GLES20.glFrontFace(GL10.GL_CW); // Draw the vertices as triangle strip GLES20.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3); System.out.println(GLUtils.getEGLErrorString(gl.glGetError())); // Disable the client state before leaving // gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); }
public void onDrawFrame(GL10 unused) { // Redraw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // TODO: Calculate delta time. m_activity.on_update(0.1f); m_active_shader.bind(); // Draw everything for (Sprite sprite : m_sprites) { sprite.get_texture().bind(); // Set up our matrix in the shader m_active_shader.set_model_matrix(sprite.get_matrix()); // Set the color m_active_shader.set_color(sprite.get_color()); // Set up our buffers to render. GLES20.glVertexAttribPointer( Shader.SHADER_ATTRIB_VERTEX_ID, 2, GLES20.GL_FLOAT, false, 4 * 4, sprite.get_vertex_data()); GLES20.glEnableVertexAttribArray(Shader.SHADER_ATTRIB_VERTEX_ID); // The texture offsets start at UV in the struct. However we need to cast vertex data to a // char first so we add the offset in bytes // properly. This might seem weird.. but it's the only proper way that doesn't generate a gcc // warning. /*GLES20.glVertexAttribPointer( Shader.SHADER_ATTRIB_TEXCOORD_ID, 2, GLES20.GL_FLOAT, false, 4 * 4, sprite.get_vertex_data(). ); glEnableVertexAttribArray(SHADER_ATTRIB_TEXCOORD_ID);*/ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); } }
public void draw(float[] mvpMatrix) { GLES20.glUseProgram(mProgram); mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); GLES20.glEnableVertexAttribArray(mPositionHandle); GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); GLES20.glUniform4fv(mColorHandle, 1, color, 0); mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); checkGlError("glGetUniformLocation"); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); checkGlError("glUniformMatrix4fv"); GLES20.glDrawArrays(GLES20.GL_LINES, 0, vertexCount); GLES20.glDisableVertexAttribArray(mPositionHandle); }
public void draw(int program) { mVertexArray.position(0); int positionHandle = glGetAttribLocation(program, Constants.A_POSITION); glEnableVertexAttribArray(positionHandle); glVertexAttribPointer( positionHandle, POS_COORDS_PER_VERTEX, GL_FLOAT, false, mStride, mVertexArray); mVertexArray.position(POS_COORDS_PER_VERTEX); int texHandle = glGetAttribLocation(program, Constants.A_TEX_COORDS); glEnableVertexAttribArray(texHandle); glVertexAttribPointer(texHandle, TEX_COORDS_PER_VERTEX, GL_FLOAT, false, mStride, mVertexArray); glDrawArrays(GL_TRIANGLE_STRIP, 0, mVertexCount); glDisableVertexAttribArray(positionHandle); glDisableVertexAttribArray(texHandle); }
public void drawSelf( int ballTexId, int isShadow, int planeId, int isLanbanYy) // 0-no shadow 1-with shadow { // 制定使用某套shader程序 GLES20.glUseProgram(mProgram); // 将最终变换矩阵传入shader程序 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); // 将位置、旋转变换矩阵传入shader程序 GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0); // 将光源位置传入shader程序 GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB); // 将摄像机位置传入shader程序 GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB); // 将是否绘制阴影属性传入shader程序 GLES20.glUniform1i(muIsShadow, isShadow); GLES20.glUniform1i(muIsShadowFrag, isShadow); GLES20.glUniform1i(muIsLanBanShdow, isLanbanYy); // 将摄像机矩阵传入shader程序 GLES20.glUniformMatrix4fv(muCameraMatrixHandle, 1, false, MatrixState.mVMatrix, 0); // 将投影矩阵传入shader程序 GLES20.glUniformMatrix4fv(muProjMatrixHandle, 1, false, MatrixState.mProjMatrix, 0); // 将平面位置传入程序 GLES20.glUniform3fv(muPlaneV, 1, Constant.mianFXL[planeId][0]); // 将平面法向量传入程序 GLES20.glUniform3fv(muPlaneN, 1, Constant.mianFXL[planeId][1]); // 传入顶点位置数据 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 传入顶点纹理坐标数据 GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer); // 传入顶点法向量数据 GLES20.glVertexAttribPointer(maNormalHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer); // 允许顶点位置、纹理坐标、法向量数据数组 GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glEnableVertexAttribArray(maTexCoorHandle); GLES20.glEnableVertexAttribArray(maNormalHandle); // 绑定纹理 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, ballTexId); GLES20.glUniform1i(muBallTexHandle, 0); // 绘制纹理矩形 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); }