UPDATE: Summary:
I can draw a circle using TRIANGLE_FAN and, separately, I also can draw two squares with bitmaps as textures. But the problem is when I draw the textures and then the circles. Circles aren't drawn.
I'm drawing two texturized squares (4 vertex each). Then I draw a circle using GL_TRIANGLE_FAN but it isn't being drawn correctly (see images).
When I draw the circles without the squares, it is drawn correctly.
Any ideas where could be the problem?
Please, ask for more information. Thanks
Adding some code that I think is important:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_BLEND);
gl.glCullFace(GL10.GL_BACK);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glOrthof(0, w, -0, h, -1, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
circle.draw(gl);
needle.draw(gl);
synchronized (tokens) {
for (Token d : tokens) {
d.draw(gl);
}
}
}
Update:
Some screenshots.
Without drawing circle and needle objects:
Drawing circle and needle:
(Look at those red lines where should be a circle)
The only change in the code between those images is commenting the lines
circle.draw(gl);
needle.draw(gl);
Token:
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glScalef(radius, radius, 0f);
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, nVertices+2);
gl.glPopMatrix();
}
Circle and Needle:
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
if (shouldLoadTexture) {
loadGLTexture(gl);
shouldLoadTexture = false;
}
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glRotatef((float) angle, 0f, 0f, 1f);
angle += rotAngle;
if(angle+rotAngle > 360.0)
angle -= 360.0;
gl.glScalef(width, height, 0f);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glPopMatrix();
}
private void loadGLTexture(GL10 gl) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
textureId = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
Update:
Following what Arne says in the first answer, I changed:
public void draw(GL10 gl) {
//New line
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
if (shouldLoadTexture) {
loadGLTexture(gl);
shouldLoadTexture = false;
}
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glRotatef((float) angle, 0f, 0f, 1f);
angle += rotAngle;
if(angle+rotAngle > 360.0)
angle -= 360.0;
gl.glScalef(width, height, 0f);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glPopMatrix();
//New line
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
Now I get no circle at all. Neither red line as in the second image.
Update:
When I don't load the texture of circle and needle (just commenting the call to loadTexture() ), I get this:
So the problem should be with the textures.
You enable texturing (by calling glEnable(GL_TEXTURE_2D)) at the beginning, but you don't provide your circles (tokens) any texture coordinates. As I suppose these shouldn't be textured, you should only enable texturing for the objects that are really textured and disable texturing again after drawing them, the same way you enable and disable the texCoord array in your updated code.
Try to disable GL_TEXTURE_COORD_ARRAY before drawing the circles with
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
Related
Setup: OpenGL ES 1.0, Android
Term background image here means a full screen texture which is drawn over two triangles which make the full screen rectangle.
If I draw full scene, including background image, I get black background color instead of clear color background, and I do not see my texture for background displayed
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
drawParticles(scene.getNumDots());
Render image attached:
However, if I draw only the background image, I see it.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
// drawParticles(scene.getNumDots()); commented out
Render image attached:
Implementations:
// how it was setup
public void setupGl(#NonNull final GL10 gl) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
// This was called
public void setDimensions(#NonNull final GL10 gl, final int width, final int height) {
gl.glViewport(0, 0, width, height);
gl.glOrthof(0, width, 0, height, 1, -1);
}
// textures were loaded like this
private void loadTexture(
#NonNull final GL10 gl,
#NonNull final Bitmap texture,
final int handleOffset) {
gl.glGenTextures(1, textureHandle, handleOffset);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[handleOffset]);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
}
private void drawBackground() {
backgroundTextureCoordinates.position(0);
backgroundCoordinates.position(0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_BACKGROUND]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, backgroundTextureCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, backgroundCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
private void drawParticles(final int count) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
particlesTexturesCoordinates.position(0);
particlesTrianglesCoordinates.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_PARTICLE]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, particlesTexturesCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, particlesTrianglesCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, count * VERTICES_PER_PARTICLE);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
public void setClearColor(
#NonNull final GL10 gl,
#ColorInt int color) {
gl.glClearColor(
Color.red(color) / 255f,
Color.green(color) / 255f,
Color.blue(color) / 255f, 0f);
}
The problem was in the way I used to draw a background
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
There, I specified the number of triangles: 2.
Whereas I had to specify the number of vertices for the two triangles: 6.
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
I have next problem.
I'm write some android app which uses opengl es 1.0
I'm using GLSurfaceView and custom Renderer. Here are methods of my custom renderer onDrawFrame, onSurfaceChanged, onSurfaceChanged:
#Override
public void onDrawFrame(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// clear Screen and Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// Drawing
gl.glTranslatef(0f, 0f, -3.0f); // move 5 units INTO the screen
// is the same as moving the camera 5 units away
// gl.glScalef(0.5f, 0.5f, 0.5f); // scale the square to 50%
// otherwise it will be too large
square.draw(gl, mScrollable.getIndexA(), mScrollable.getIndexB(), mScrollable.getAlphaValue()); // Draw the triangle
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, mWidth, mHeight); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix
//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Load the texture for the square
square.loadGLTexture(gl, this.context);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
And my Square class to render textures:
private int[] textures;
private GL10 mGl;
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
-1.0f, -1.8f, 0.0f, // V1 - bottom left
-1.0f, 1.3f, 0.0f, // V2 - top left
1.0f, -1.8f, 0.0f, // V3 - bottom right
1.0f, 1.3f, 0.0f // V4 - top right
};
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
// Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
private GlAnimationCache cacheHelper;
public void loadGLTexture(GL10 gl, Context context) {
// loading texture
mGl = gl;
gl.glGenTextures(mImageEntityList.size(), textures, 0);
cacheHelper = new GlAnimationCache(mContext, mImageEntityList, mGlRenderer, mGlSurfaceView);
cacheHelper.initCache(); //it init cache in background thread
}
public void removeTextureAt(int position){
mGl.glDeleteTextures(1, textures, position);
}
public void bindTextureAt(final Bitmap bitmap, int position){
// generate one texture poi, 0);
// ...and bind it to our array
mGl.glBindTexture(GL10.GL_TEXTURE_2D, textures[position]);
mGl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
mGl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// create nearest filtered texture
mGl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
mGl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
//Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLUtils.getInternalFormat(bitmap), bitmap, GLUtils.getType(bitmap), 0);
bitmap.recycle();
}
private int prevIndex = 0;
/** The draw method for the square with the GL context */
public void draw(GL10 gl, int indexA, int indexB, float alpha) {
// bind the previously generated texture
if(prevIndex != indexA){
cacheHelper.synchronizeCache(indexA);
prevIndex = indexA;
}
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[indexA]);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glColor4f(1, 1, 1, 1f);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[indexB]);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glColor4f(1, 1, 1, Math.abs(alpha));
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
So, the problem is next: On Samsung S5, Alcatel One Touch 4033D and Motorolla Droid Razr textures are rendered fine. But on Samsung Galaxy Nexus I see only 'white box'.
I have tried to change size of texture to power of 2(512 x 512) but it not helpes.
In Square class add function:
public boolean isNPOTSupported(GL10 gl) {
String extensions = gl.glGetString(GL10.GL_EXTENSIONS);
return extensions.indexOf("GL_OES_texture_npot") != -1;
}
And when you Generate textures, add this:
final boolean isNPOTSupported = isNPOTSupported(gl);
if(isNPOTSupported) {
gl.glGenTextures(mImageEntityList.size(), textures, 0);
}else{
for(int i = 0; i < mImageEntityList.size(); i++){
gl.glGenTextures(1, textures, i);
}
}
Instead this:
gl.glGenTextures(mImageEntityList.size(), textures, 0);
I completed the 3D earth. Now I am trying to draw a triangle on the earth? but I failed to complete it, could you give me some cues or code to complete it? the following is drawing part in the sphere code. I could draw the triangle on the screen, but I do not know how to draw the triangle or point on the surface of 3D earth to render the latitude and longitude data on the earth.
public void draw(GL10 gl)
{
float vertexArray[] = {
SphereCoordinate(lat1, long1, 2).x,SphereCoordinate(lat1, long1, 2).y,SphereCoordinate(lat1, long1, 2).z,0.0f,
SphereCoordinate(lat2, long2, 2).x,SphereCoordinate(lat2, long2, 2).y,SphereCoordinate(lat2, long2, 2).z,0.0f,
SphereCoordinate(lat3, long3, 2).x,SphereCoordinate(lat3, long3, 2).y,SphereCoordinate(lat3, long3, 2).z,0.0f,
};
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_BLEND);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(vertexArray));
gl.glColor4f(.0f, 1.0f, 0.0f, 1);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // I want to draw the triangle on earth.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_VertexData);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glClientActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, m_Texture0);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, m_TextureData);
gl.glClientActiveTexture(GL10.GL_TEXTURE1);
gl.glTexCoordPointer(2, GL10.GL_FLOAT,0,m_TextureData);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glNormalPointer(GL10.GL_FLOAT, 0, m_NormalData);
gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, m_ColorData);
multiTextureBumpMap(gl, m_Texture0, m_Texture1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, (m_Slices+1)*2*(m_Stacks-1)+2);
}
At the beginning, the SphereCoordinate(lat1, long1, 2(r)), the r is 1 which is equal with radius of sphere, but the triangle could not be viewed. Then I made the r be 2. And run, the final slot is the following image:
Actually, I want to draw the triangle on the surface of earth.
I use 2D sprite texture applied to a square with transparent or semi-transparent area but I can't see anything through them (except the background) if there are other textures behind. I can see the background image because I use a transparent GLSurfaceView, the background is applied in a ImageView behind the GLSurfaceView.
My problem in an image:
Actually I use this code in the onSurfaceCreated method:
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
this in onDrawFrame method:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
mObject1.draw(gl);
mObject2.draw(gl);
and this in draw method of mObject1 and mObject2:
gl.glPushMatrix();
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTranslatef((float) mPosition.getX(), (float) mPosition.getY(), 0f);
float scaleFactor = (float) (2 * mRadius);
gl.glScalef(scaleFactor, scaleFactor, 0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glColor4f (1f, 1.0f, 1, 1f);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glPopMatrix();
after
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
put
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
Greetings. I've dabbled in OpenGL in the past and as far as I understood it the best way to do 2d games with sprite-based animation was eliminate the z plane and set up an orthographic projection so you can just use Cartesian coordinates for textures and drawing positions.
So I've been trying to implement this in android. I was just using all the build in draw functions but rendering a few hundred images separately with the vanilla drawbitmap functions was killing my framerate.
A rect shows up on the screen all right but the texture refuses to show. if you could take a look and let me know where I'm going wrong here I'd really appreciate it. Be warned I don;t have nearly as comprehensive understanding of opengl as I would like. I respect anyone that can wrap their heads around this stuff.
This is the sprite class which draws the rect with the bound texture:
public class Sprite {
private FloatBuffer vertexBuffer; // buffer holding the vertices
private FloatBuffer textureBuffer;
private int[] textures = new int[1];
private float vertices[] = {
0.0f, 0.0f,
0.0f, 32.0f,
32.0f, 0.0f,
32.0f, 32.0f
};
private float texture[] = {
0.0f, 0.0f,
0.0f, 16.0f,
16.0f, 0.0f,
16.0f, 16.0f
};
public Sprite() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
public void loadGLTexture(GL10 gl, Context context)
{
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.charactersprites);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//Not sure if I need these...
//gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
//gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
/** The draw method for the triangle with the GL context */
public void draw(GL10 gl) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//gl.glFrontFace(GL10.GL_CW);//is this necessary?
// set the colour for the triangle
//gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
// Point to our vertex buffer
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 2);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
And this is the renderer...i pass in the srfaceview to hold onto so i can get the height and width for the projection
public class GlRenderer implements Renderer {
private Sprite sprite;
private GLSurfaceView surfaceView;
private Context context;
/** Constructor to set the handed over context */
public GlRenderer(GLSurfaceView surfaceView, Context context) {
this.sprite = new Sprite();
this.surfaceView = surfaceView;
this.context = context;
}
#Override
public void onDrawFrame(GL10 gl) {
// clear Screen and Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the Modelview Matrix
gl.glLoadIdentity();
gl.glOrthof(0.0f, surfaceView.getWidth(), surfaceView.getHeight(), 0.0f, -1.0f, 1.0f);
sprite.draw(gl);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
sprite.loadGLTexture(gl, this.context);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
Have you tried
glActiveTexture(GL10.GL_TEXTURE0);
before rendering sprite?
Try setting the texture environment to GL_REPLACE (using glTexEnv). If it is set to GL_MODULATE, the texture color will be multiplied by the current color (or the color from lighting, if enabled) and if this color is black, the result is just black.
Below works on my Galaxy S but I'm having trouble getting it to work on my friend's HTC using projective. Though they do work in model space... Could you try?
public void draw(GL10 gl) {
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mCoordinateBuffer);
// Draw the mesh
gl.glFrontFace(GL10.GL_CCW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalBuffer);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, mIndexBuffer.limit(), GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
// Disable texture
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadTexture(Context context, GL10 gl) {
Bitmap bmp;
try {
bmp = BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.texture_loading));
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
mTextureID = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
// Mendatory, tells openGL how to render the texture, nearest will look sharp, smooth will look blurry
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
// Not mendatory, tells openGL what to do when sprite is bigger than object
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
// Mendatory,
// GL_REPLACE replaces all color info,
// GL_MODULATE modulates, texture will be affected by lightning
gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
bmp.recycle();
} catch (Error e) {
}
}