我在這裏給你一個簡單的問題。我正在使用紋理來渲染位圖,並且一切都很正常。不過,我想在上述紋理上繪製一條線。我知道vert/tex座標如何工作,但由於某些未知原因,我無法做到這一點。我打電話在調用繪製位圖到紋理之後繪製線,但只有位圖仍然顯示。達到此目的的最佳方法是什麼?我使用texure2D在我的片段着色器中繪製位圖,是否需要添加一些東西來繪製具有指定顏色的行?任何幫助將不勝感激。在紋理中畫一條線Open GL ES 2.0 Android
謝謝。
代碼相關的位圖紋理:
private final String vertexTextureShaderCode =
"attribute vec4 aPosition;"+ // Position of out Vertex
"attribute vec2 aTexPosition;"+ // Position of our texture contained in the vertex
"varying vec2 vTexPosition;"+ //
"void main(){"+
" gl_Position = aPosition;"+
" vTexPosition = aTexPosition;"+
"}";
private final String fragmentTextureShaderCode =
"precision mediump float;" +
"uniform sampler2D uTexture;" + // Our Background Image (Our uploaded image)
"varying vec2 vTexPosition;" +
"void main() {" +
" gl_FragColor = texture2D(uTexture, vTexPosition);" +
"}";
public void draw(float[] m ,ImageEffectFactory effectFactory,int currentEffect){
// add program to OpenGl ES Environment;
this.imageEffectFactory = effectFactory;
boolean effectChanged = false;
if(currentEffect != mCurrentEffect){
mCurrentEffect = currentEffect;
effectChanged = true;
}
int whichTexture = 0;
if(currentEffect > 0 && currentEffect < 6){
whichTexture = 1;
if(effectChanged) {
imageEffectFactory.applyEffect(mTexturePointer, ImageEffectFactory.FILTER.values()[currentEffect], mTextureImageWidth, mTextureImageHeight);
effectChanged = false;
}
}
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glUseProgram(mProgram);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glViewport(0,0,mViewWidth, mViewHeight);
// get handle to vertex shader vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
mTextureHandle = GLES20.glGetUniformLocation(mProgram, "uTexture");
mTexturePositionHandle = GLES20.glGetAttribLocation(mProgram, "aTexPosition");
GLES20.glVertexAttribPointer(mTexturePositionHandle, 2, GLES20.GL_FLOAT, false, 0, mTextureBuffer);
GLES20.glEnableVertexAttribArray(mTexturePositionHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexturePointer[whichTexture]);
GLES20.glUniform1i(mTextureHandle,0);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, vertexBuffer);
// enable a handle to the triangle vertice
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Draw the square
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertexCount);
// Disbale the vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
正如我剛纔所說,這工作正常。我嘗試加載的位圖將顯示並看起來很完美。但在繪製完成後,我試圖畫出線條。這裏是該行的設置:
private static final String vsShader =
"uniform mat4 uMVPMatrix;"+
"attribute vec4 vPosition; " +
"void main() { " +
" gl_Position = uMVPMatrix * vPosition;" +
" }";
private static final String fsShader =
"precision mediump float;"
+ "uniform vec4 vColor;"
+ "void main() {"
+ "gl_FragColor = vColor;" +
"}";
private float[] pathCoords = {
-0.5f, 0.5f,0.0f,
-0.5f, -0.5f, 0.0f
};
private short[] pathDrawOrder = {0,1};
private float[] color = {1.0f, 0.0f, 0.0f, 1.0f}; // - red for testing
private final FloatBuffer mVertBuffer;
private final ShortBuffer mDrawListBuffer;
private int mCropProgram;
private int vPositionHandle;
private int vColorHandle;
private int uMVPMatrixHandle;
public CropFrameRect(Context context){
this.context = context;
// Allocat a byteBuffer for our Vertex Array;
ByteBuffer bb = ByteBuffer.allocateDirect(pathCoords.length * 4);
// ORder those Bytes correctly depending on the device (ie. BIG-ENDIAN vs. LITTLE-ENDIAN
bb.order(ByteOrder.nativeOrder());
// Lets now store those vertex points into a FloatBuffer please.
mVertBuffer = bb.asFloatBuffer();
mVertBuffer.put(pathCoords);
mVertBuffer.position(0);
ByteBuffer dlb = ByteBuffer.allocateDirect(pathDrawOrder.length*2);
dlb.order(ByteOrder.nativeOrder());
mDrawListBuffer = dlb.asShortBuffer();
mDrawListBuffer.put(pathDrawOrder);
mDrawListBuffer.position(0);
int vertexShader = ImageGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vsShader);
int fragmentShader = ImageGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fsShader);
// We've compiled our vertex and our texture shader programs. Let us create the program..
mCropProgram = GLES20.glCreateProgram();
// Attach the vertex shader string
GLES20.glAttachShader(mCropProgram, vertexShader);
// Attach the shader program string
GLES20.glAttachShader(mCropProgram, fragmentShader);
// Link the program code to the OpenGL Context
GLES20.glLinkProgram(mCropProgram);
Log.d(TAG, "Crop Program Linked: " + mCropProgram);
}
public void draw(float[] matrix){
GLES20.glUseProgram(mCropProgram);
vPositionHandle = GLES20.glGetAttribLocation(mCropProgram, "vPosition");
GLES20.glEnableVertexAttribArray(vPositionHandle);
GLES20.glVertexAttribPointer(vPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, mVertBuffer);
// set the color
vColorHandle = GLES20.glGetUniformLocation(mCropProgram, "vColor");
GLES20.glUniform4fv(vColorHandle, 1, color, 0);
// set the Matrix projection for the View;
uMVPMatrixHandle = GLES20.glGetUniformLocation(mCropProgram, "uMVPMatrix");
GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, matrix, 0);
GLES20.glLineWidth(2.7f);
// Draw those elements
GLES20.glDrawElements(GLES20.GL_LINES, pathDrawOrder.length, GLES20.GL_UNSIGNED_SHORT, mDrawListBuffer);
GLES20.glDisableVertexAttribArray(vPositionHandle);
GLES20.glDisable(vColorHandle);
}
mDrawListBuffer的位置和內容,與pathDrawOrder相同; mCropProgram是否有效並對應於fsShader和vsShader中的源代碼?手柄是否有效(vPositionHandle)與非負值相同?什麼是矩陣值? –
mDrawListBuffer是我將繪製順序列表存儲在其中的shortBuffer。我不認爲這可能是需要的,但我也不認爲它可能會受到傷害。我爲這個示例添加了更多代碼。 mCropProgram是有效的,並且對應於fsShader和vsShader,我肯定是99%。我很抱歉,這不是第一次更完整。我會盡快更新矩陣值謝謝。 – Devsil