Previously I did a Tutorial on OpenGL-ES 1.0. For reference, this can be found here:
SpaceInvaders (it's in german though)
My goal now is to port the game to OpenGL-ES 2.0. So far, I am able to load meshes and textures and render them.
Now I would like to have a simple rectangle as my background with a texture on it. This should be rendered in Ortho-Perspective. I then change to Projection-Perspective and draw a simple box. When I now call setLookAtM(...), I get a blank sceen. Here is the code:
public void onDrawFrame(GL10 gl) {
long currentFrameStart = System.nanoTime();
deltaTime = (currentFrameStart - lastFrameStart) / 1000000000.0f;
lastFrameStart = currentFrameStart;
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(programHandle);
checkGlError("glUseProgram");
mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVPMatrix");
mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVMatrix");
mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "uLightPos");
mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "uTexture");
mPositionHandle = GLES20.glGetAttribLocation(programHandle, "aPosition");
mColorHandle = GLES20.glGetAttribLocation(programHandle, "aColor");
mNormalHandle = GLES20.glGetAttribLocation(programHandle, "aNormal");
mTextureCoordinateHandle = GLES20.glGetAttribLocation
(programHandle,"aTexCoordinate");
Matrix.setIdentityM(mLightModelMatrix, 0);
Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0,
mLightPosInModelSpace, 0);
Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);
Matrix.orthoM(mProjectionMatrix, 0, -width / 2, width / 2, -height / 2, height / 2,
0, 100);
drawBackground();
GLES20.glEnable(GLES20.GL_CULL_FACE);
Matrix.setIdentityM(mProjectionMatrix, 0);
final float ratio = (float) width / height;
final float left = -ratio;
final float right = ratio;
final float bottom = -1.0f;
final float top = 1.0f;
final float near = 1.0f;
final float far = 100.0f;
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
Matrix.setLookAtM(mViewMatrix, 0, 0, 6, 2, 0, 0, -4, 0, 1, 0);
drawBlock();
}
Here the drawBackground method:
private void drawBackground() {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, backgroundTextureHandle);
GLES20.glUniform1i(mTextureUniformHandle, 0);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.setIdentityM(mProjectionMatrix, 0);
mBackgroundPositions.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT,
false, 0, mBackgroundPositions);
GLES20.glEnableVertexAttribArray(mPositionHandle);
mBackgroundColors.position(0);
GLES20.glVertexAttribPointer(mColorHandle, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false,
0, mBackgroundColors);
GLES20.glEnableVertexAttribArray(mColorHandle);
mBackgroundNormals.position(0);
GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT,
false, 0, mBackgroundNormals);
GLES20.glEnableVertexAttribArray(mNormalHandle);
mBackgroundTextureCoordinates.position(0);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORD_DATA_SIZE,
GLES20.GL_FLOAT, false, 0, mBackgroundTextureCoordinates);
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]);
ShortBuffer buf =
ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asShortBuffer();
buf.put(new short[] {0, 1, 2, 0, 2, 3});
buf.position(0);
// Draw the rectangle.
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, buf);
checkGlError("glDrawArrays");
}
And finally the drawBlock method:
private void drawBlocks() {
GLES20.glUseProgram(colorProgramHandle);
checkGlError("glUseProgram");
mMVPMatrixHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uMVPMatrix");
mMVMatrixHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uMVMatrix");
mLightPosHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uLightPos");
mTextureUniformHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uTexture");
mPositionHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aPosition");
mColorHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aColor");
mNormalHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aNormal");
mTextureCoordinateHandle = GLES20.glGetAttribLocation(colorProgramHandle,
"aTexCoordinate");
Matrix.setIdentityM(mProjectionMatrix, 0);
Matrix.setIdentityM(mModelMatrix, 0);
blockMesh.vertexBuffer.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT,
false, 0, blockMesh.vertexBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
blockMesh.colorBuffer.position(0);
GLES20.glVertexAttribPointer(mColorHandle, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false,
0, blockMesh.colorBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
blockMesh.normalBuffer.position(0);
GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT,
false, 0, blockMesh.normalBuffer);
GLES20.glEnableVertexAttribArray(mNormalHandle);
// 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.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT,
blockMesh.indexBuffer);
checkGlError("glDrawElements");
}
I am not sure what I am missing about ortho and projection perspective. Any help is appreciated.
You're wiping the projection matrix as part of drawBlocks. I don't think you want to do that, if you want to draw it using the perspective projection already computed.
...
Matrix.setIdentityM(mProjectionMatrix, 0); <-----
Matrix.setIdentityM(mModelMatrix, 0);
blockMesh.vertexBuffer.position(0);
...
Related
I am trying to move the object output is not correct.
X is moving in opposite direction and the Y is much more up then original coordinate.
Here's my code to take out the normalized x and y.
#Override
public boolean onTouch(View v, MotionEvent event) {
requestRender();
float normalisedX=(event.getX()/getWidth())*2.0f -1.0f;
float normalisedY=(event.getY()/getHeight())*-2.0f+1.0f;
mRender.setXY(normalisedX, normalisedY);
return true;
}
Matrix is used in render to move the object. Here's the code
#Override
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
float[] scratch = new float[16];
setLookAtM(mViewMatrix, 0, 0, 0, -2, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
setIdentityM(mMoveMatrix, 0);
translateM(mMoveMatrix, 0, x, y, 0);
multiplyMM(scratch, 0, mMVPMatrix, 0, mMoveMatrix, 0);
table.draw(mMVPMatrix, tableTextureObj);
userMallet.draw(scratch, userMalletTextureObj);
}
Here's the object code to draw:-
public void draw(float[] matrix, int texture)
{
if (ShaderProgram.validateProgram(mProgram))
{
glUseProgram(mProgram);
aPositionHandle=glGetAttribLocation(mProgram, "a_Position");
uMatrixHandle=glGetUniformLocation(mProgram, "u_Matrix");
aTextureCoordinateHandle=glGetAttribLocation(mProgram, "a_TextureCoordinates");
//shader
vColorHandle=glGetUniformLocation(mProgram, "v_Color");
vertexBuffer.position(0);
glVertexAttribPointer(aPositionHandle, COORDS_PER_VERTEX, GL_FLOAT, false, STRIDE, vertexBuffer);
glEnableVertexAttribArray(aPositionHandle);
vertexBuffer.position(0);
vertexBuffer.position(COORDS_PER_VERTEX);
glVertexAttribPointer(aTextureCoordinateHandle, TEXTURE_COORDINATES_COMPONENT_COUNT, GL_FLOAT, false, STRIDE, vertexBuffer);
glEnableVertexAttribArray(aTextureCoordinateHandle);
vertexBuffer.position(0);
uTexureUnitHandle=glGetUniformLocation(mProgram, "u_TextureUnit");
glUniformMatrix4fv(uMatrixHandle, 1, false, matrix, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uTexureUnitHandle, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, outerVertexCount);
}
}
Please tell me what I am doing wrong so I can resolve the issue.
I have a project featuring a 3d object. The user can rotate and zoom it in/out with finger gestures. The problem I have is while rotating a cube (other solids also cause the same error, but I'll set an example on cube), the edges of it kind of disintegrate (as shown in screenshots). The faster the rotation is the more substantial the problem becomes.
I tried to mask it by disable face culling, so that the user sees the inside of the cube, which basically has the same colour. However the project manager isn't pleased with that solution (I admitt it doesn't work that well).
The cube has 6 faces, each containing about 242 polygons.
Here are the code snippets relevant to rendering the 3d sceene (I'm using VBO):
public void onDrawFrame(GL10 glUnused)
{
GLES20.glDisable(GLES20.GL_BLEND);
isBlendingEnabled = false;
Matrix.setLookAtM(mViewMatrix, 0, mEyeX, mEyeY, mEyeZ, mLookX, mLookY, mLookZ, mUpX, mUpY, mUpZ);
if(!mSolid.equals(((ActivityMain) mContext).getSolid())){
refreshSolid();
refreshTextureData();
}
if(mSolid.hasChanged){
refreshSolid();
}
if(mSolid.hasTexturesChanged){
refreshTextureData();
mSolid.hasTexturesChanged = false;
}
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Set per-vertex lighting program.
GLES20.glUseProgram(mProgramHandle);
// Set program handles for solid drawing.
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
mLightPosHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_LightPos");
mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture");
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal");
mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");
// Calculate position of the light. Handle it's rotation
Matrix.setIdentityM(mLightModelMatrix, 0);
Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -2.0f);
Matrix.rotateM(mLightModelMatrix, 0, mAngleInDegrees, 0.0f, 1.0f, 0.0f);
Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 3.5f);
Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);
//Doing that step for each of the faces
for(int i = 0; i < mSolid.get3dMesh().size(); i++){
// Draw a solid.
// Translate the solid into the screen.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.5f);
// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f);
//handle inertia
if(Math.abs(mDeltaX) > 2f){
mDeltaX = Math.signum(mDeltaX);
mDeltaX = 0.5f*mDeltaX;
}else if(Math.abs(mDeltaX) > 0.05f){
mDeltaX = 0.99f*mDeltaX;
}else
mDeltaX = 0.0f;
if(Math.abs(mDeltaY) > 2f){
mDeltaY = Math.signum(mDeltaY);
mDeltaY = 0.5f*mDeltaY;
}else if(Math.abs(mDeltaY) > 0.05f){
mDeltaY = 0.99f*mDeltaY;
}else
mDeltaY = 0.0f;
// Multiply the current rotation by the accumulated rotation, and then set the accumulated rotation to the result.
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
// Rotate the cube taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);
// 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, mTextureDataHandle[i]);
// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
GLES20.glUniform1i(mTextureUniformHandle, 0);
drawCube(i);
}
if(ModelCorePrefs.getDrawMesh())
drawVertices();
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc (GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
isBlendingEnabled = true;
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, -3.5f, -3.5f);
Matrix.scaleM(mModelMatrix, 0, 2f, 1.0f, 2f);
// 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, mTextureDataHandle[mSolid.get3dMesh().size()]);
// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
GLES20.glUniform1i(mTextureUniformHandle, 0);
drawShadow();
}
The drawCube() method used above:
private void drawCube(int i)
{
// Pass in the position information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubePositionsBufferIdx[i]);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);
// Pass in the color information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeColorsBufferIdx[i]);
GLES20.glEnableVertexAttribArray(mColorHandle);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, 0);
// Pass in the normal information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeNormalsBufferIdx[i]);
GLES20.glEnableVertexAttribArray(mNormalHandle);
GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, 0);
// Pass in the texture coordinate information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeTexCoordsBufferIdx[i]);
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, 0);
// 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(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
System.arraycopy(mTemporaryMatrix, 0, mMVPMatrix, 0, 16);
// 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, mSolid.get3dMesh().get(i).length/3);
}
And the screenshot of the problem:
I would strongly suggest you remove the glGet*Location (...) calls from your onDrawFrame (...) method. Those things do not change after you link your program initially, and searching for those locations by string name each frame is going to hinder your performance. Likewise, you only have to set the sampler uniform once per-program since it is always using texture image unit 0.
As for your real issue:
I see no evidence that each face in your loop has its own unique model matrix, so you should move the logic that incrementally transforms the matrix out of the loop. Otherwise you are going to rotate each face in the cube individually, which is what your diagram is showing. To be honest, I do not know why you even have to draw the faces one at a time.
// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
GLES20.glUniform1i(mTextureUniformHandle, 0);
// ^^^^^ ONLY DO THAT ONCE!
// Translate the solid into the screen.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.5f);
// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f);
//handle inertia
if(Math.abs(mDeltaX) > 2f){
mDeltaX = Math.signum(mDeltaX);
mDeltaX = 0.5f*mDeltaX;
}else if(Math.abs(mDeltaX) > 0.05f){
mDeltaX = 0.99f*mDeltaX;
}else
mDeltaX = 0.0f;
if(Math.abs(mDeltaY) > 2f){
mDeltaY = Math.signum(mDeltaY);
mDeltaY = 0.5f*mDeltaY;
}else if(Math.abs(mDeltaY) > 0.05f){
mDeltaY = 0.99f*mDeltaY;
}else
mDeltaY = 0.0f;
// Multiply the current rotation by the accumulated rotation, and then set the accumulated rotation to the result.
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
// Rotate the cube taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);
//Doing that step for each of the faces
for(int i = 0; i < mSolid.get3dMesh().size(); i++){
// Draw a solid.
// 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, mTextureDataHandle[i]);
drawCube(i);
}
I am drawing texture in opengl using Triangles and vertices
when it is drawn it is something like this
Problem is it is drawing same triangle on both sides?
what's the solution to this?
here is my code?
public float m_cameraX=26.036f;
public float m_cameraY=45.126f;
public float m_cameraZ=5f;
private final float[][] vertData = {
{
25.457f, 45.534f, 3.0f,
26.595f, 45.534f, 3.0f,
25.457f, 44.718f, 3.0f,
26.595f, 44.718f, 3.0f
};
vertBuffer = ByteBuffer.allocateDirect(12 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertBuffer.put(vertData[i]).position(0);
loadtexture();
vertBuffer.position(0);
GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, vertBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, imgHandle);
GLES20.glUniform1i(mColorHandle, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
You need to set texture coordinates. Check out these tutorials:
http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/
http://insanitydesign.com/wp/projects/nehe-android-ports/
http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/
I've tried following the lessons about drawing shapes and applying projections to them as found here, but the projection just doesn't seem to work. As far as I know I've copy-pasted all the pieces of code in my project, here are the important code parts:
In my Renderer class:
#Override
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Draw shape
mTriangle.draw(mMVPMatrix);
}
#Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
The draw method of my Triangle class:
public void draw(float[] mvpMatrix) {
GLES20.glUseProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // get handle to vertex shader's vPosition member
GLES20.glEnableVertexAttribArray(mPositionHandle); // Enable a handle to the triangle vertices
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // Prepare the triangle coordinate data
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // get handle to fragment shader's vColor member
GLES20.glUniform4fv(mColorHandle, 1, color, 0); // Set color for drawing the triangle
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // get handle to shape's transformation matrix
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Apply the projection and view transformation
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Draw the triangle
GLES20.glDisableVertexAttribArray(mPositionHandle); // Disable vertex array
}
The triangle still is being stretched. What am I doing wrong?
Did you try to debug in onSurfaceChanged? What happens in those lines? Especially:
what are the passed values width and height?
which value does float ratio = (float) width / height; calculate?
do you see any changes to the values of mProjMatrix after the call Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);?
how does your private final String fragmentShaderCode in class Triangle look like? As the order of the multiplication in the line " gl_Position = uMVPMatrix * vPosition;" + is important.
Your provided code looks quiet good, difficult issue here.
I am getting a Fatal Signal 11 after quite a few
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
calls within my OnDrawFrame function. The textures are been drawn correctly before the crash, which I believe it means that my vertex and fragment shaders work fine.
What I am trying to do, is to blend two textures together using GLSurfaceView.Renderer and SurfaceTexture.OnFrameAvailableListener.
The first texture is a live camera preview SurfaceTexture which works fine by its own. The second tetxure is just a bitmap image, which also works fine by its own. but combining them gives the crash.
My OnDrawFrame is as follows:
public void onDrawFrame(GL10 glUnused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
synchronized(this) {
if (updateSurface) {
mSurface.updateTexImage();
mSurface.getTransformMatrix(mSTMatrix);
updateSurface = false;
}
}
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
muCRatioHandle = GLES20.glGetUniformLocation(mProgram, "uCRatio");
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[0]);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
// GLES20.glUniform1i(t1h, textures[0]);
GLES20.glUniform1i(t1h, 1);
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[1]);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
int t2h = GLES20.glGetUniformLocation ( mProgram, "sTexture2" );
GLES20.glUniform1i(t2h, 2);
GLES20.glUseProgram(mProgram);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
mTriangleVertices.position(0);
GLES20.glEnableVertexAttribArray(maPositionHandle);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
4*5, mTriangleVertices);
mTriangleVertices.position(3);
GLES20.glEnableVertexAttribArray(maTextureHandle);
GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
4*5, mTriangleVertices);
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
GLES20.glUniform1f(muCRatioHandle, mCameraRatio);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
GLES20.glDisableVertexAttribArray(maPositionHandle);
GLES20.glDisableVertexAttribArray(maTextureHandle);
// GLES20.glFlush();
}
where mTriangleVertices is:
mTriangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length
* FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);
and mTriangleVerticesData is:
private final float[] mTriangleVerticesData = {
// X, Y, Z, U, V
-1.0f, -1.0f, 0, 0.f, 0.f,
1.0f, -1.0f, 0, 1.f, 0.f,
-1.0f, 1.0f, 0, 0.f, 1.f,
1.0f, 1.0f, 0, 1.f, 1.f,
};
Any tips, links or even code snippets would be much appreciated!
Thank you in advance
Don't know if this is the cause for the crash but you're assigning one too high texture ids for shader at least. For example;
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
GLES20.glUniform1i(t1h, 1);
Should read
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
GLES20.glUniform1i(t1h, 0);
Edit: Ah, this might be the cause for the crash:
GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
4*5, mTriangleVertices);
It should be
GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false,
4*5, mTriangleVertices);
More likely since you have two texture values per vertex.