I'm putting together a simple test made up of two tutorials available online for OpenGL ES on Android. This is really just so that I can learn about the basics of OpenGL ES to better understand how I have to design my program.
Right now, when it tries to render, the mouse movement effect works, but I get no square drawn on the screen.
Here are the two source files I'm dealing with:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
//Simple clear-screen renderer.
class ClearRenderer implements GLSurfaceView.Renderer {
private float mRed;
private float mGreen;
private float mBlue;
private TileUI testTile;
public ClearRenderer() {
this.testTile = new TileUI();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
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);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)w / (float)h, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onDrawFrame(GL10 gl) {
gl.glClearColor(mRed, mGreen, mBlue, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, 0.0f);
testTile.draw(gl);
}
public void setColor(float r, float g, float b) {
mRed = r;
mGreen = g;
mBlue = b;
}
}
The second one is the tile object itself:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class TileUI {
private float[] vertices = {
-1.0f, -1.0f, 0.0f, // Bottom left
1.0f, -1.0f, 0.0f, // Bottom right
-1.0f, 1.0f, 0.0f, // Top left
1.0f, 1.0f, 0.0f // Top right
};
private FloatBuffer vertexBuffer;
public TileUI() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
}
// Performs the actual drawing of a Tile.
public void draw(GL10 gl) {
// Set the face rotation.
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// Enable vertex buffer.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Draw the vertices as a triangle strip.
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
// Disable the client state before leaving.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
I will definitely apologize in advance, this is a bit of a Frankenstein, any help and explanation about what's going on here would be greatly appreciated.
I think that the line that's causing you problems is gl.glTranslatef(0.0f, 0.0f, 0.0f);. That means that you are drawing your quad at the origin and that's also where the camera is so you don't see it. Try something like gl.glTranslatef(0.0f, 0.0f, -10.0f);. That should move the quad into the screen and put it in front of your camera. Alternatively, if you're doing 2D drawing you could use glOrtho() instead of gluPerspective() and that will give you an orthographic, or flat, projection.
Related
This is the actual code from NeHe's ports but it isn't drawing anything. Why ??
My main activity class :
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
/**
* Our OpenGL program's main activity
*/
public class Main extends Activity {
private GLSurfaceView glView; // Use GLSurfaceView
// Call back when the activity is started, to initialize the view
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glView = new GLSurfaceView(this); // Allocate a GLSurfaceView
glView.setEGLContextClientVersion(1);
glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glView.setRenderer(new mainRenderer(this)); // Use a custom renderer
this.setContentView(glView); // This activity sets to GLSurfaceView
}
// Call back when the activity is going into the background
#Override
protected void onPause() {
super.onPause();
glView.onPause();
}
// Call back after onPause()
#Override
protected void onResume() {
super.onResume();
glView.onResume();
}
}
My renderer class :
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
/**
* OpenGL Custom renderer used with GLSurfaceView
*/
public class mainRenderer implements GLSurfaceView.Renderer {
Context context; // Application's context
Patrat quad;
// Constructor with global application context
public mainRenderer(Context context) {
this.context = context;
quad = new Patrat();
}
// Call back when the surface is first created or re-created
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set color's clear-value to black
gl.glClearDepthf(1.0f); // Set depth's clear-value to farthest
gl.glEnable(GL10.GL_DEPTH_TEST); // Enables depth-buffer for hidden surface removal
gl.glDepthFunc(GL10.GL_LEQUAL); // The type of depth testing to do
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); // nice perspective view
gl.glShadeModel(GL10.GL_SMOOTH); // Enable smooth shading of color
gl.glDisable(GL10.GL_DITHER); // Disable dithering for better performance
// You OpenGL|ES initialization code here
// ......
}
// Call back after onSurfaceCreated() or whenever the window's size changes
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1; // To prevent divide by zero
float aspect = (float)width / height;
// Set the viewport (display area) to cover the entire window
gl.glViewport(0, 0, width, height);
// Setup perspective projection, with aspect ratio matches viewport
gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix
gl.glLoadIdentity(); // Reset projection matrix
// Use perspective projection
GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.f);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix
gl.glLoadIdentity(); // Reset
// You OpenGL|ES display re-sizing code here
// ......
}
// Call back to draw the current frame.
#Override
public void onDrawFrame(GL10 gl) {
// Clear color and depth buffers using clear-value set earlier
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// You OpenGL|ES rendering code here
// ......
gl.glLoadIdentity();
quad.draw(gl);
}
}
My square class :
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
/*
* A square drawn in 2 triangles (using TRIANGLE_STRIP).
*/
public class Patrat {
private FloatBuffer vertexBuffer; // Buffer for vertex-array
private float[] vertices = { // Vertices for the square
-1.0f, -1.0f, 0.0f, // 0. left-bottom
1.0f, -1.0f, 0.0f, // 1. right-bottom
-1.0f, 1.0f, 0.0f, // 2. left-top
1.0f, 1.0f, 0.0f // 3. right-top
};
// Constructor - Setup the vertex buffer
public Patrat() {
// Setup vertex array buffer. Vertices in float. A float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
}
// Render the shape
public void draw(GL10 gl) {
// Enable vertex-array and define its buffer
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// Draw the primitives from the vertex-array directly
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
Any solutions please ? I can't undersant why this doesn't work ...
1st off change vertices set 10% from yours:
private float[] vertices = { // Vertices for the square
-0.1f, -0.1f, 0.0f, // 0. left-bottom
0.1f, -0.1f, 0.0f, // 1. right-bottom
-0.1f, 0.1f, 0.0f, // 2. left-top
0.1f, 0.1f, 0.0f // 3. right-top
};
After that I found problem in your onDrawFrame(GL10 gl):
You don't have nor glTranslatef neitherglScalef
change it to:
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// Drawing
gl.glTranslatef(0.0f, 0.0f, -1.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 triangle to 50%
// otherwise it will be too large
quad.draw(gl);
}
ı try to draw a square.. but when ı run the code, ı see a tringle , not a square... :)) what is the problem here???
public class MyGL20Renderer implements GLSurfaceView.Renderer {
private FloatBuffer square1;
private void initShapes(){
float square1Coords[]={
-0.5f, -0.5f, 0.0f, // 0. left-bottom
0.5f, -0.5f, 0.0f, // 1. right-bottom
0.0f, 0.0f, 0.0f, // 2. left-top
0.5f, 0.5f, 0.0f // 3. right-top
};
// initialize vertex Buffer for square
ByteBuffer vbb4 = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
square1Coords.length * 4);
vbb4.order(ByteOrder.nativeOrder());
square1 = vbb4.asFloatBuffer();
square1.put(square1Coords);
square1.position(0);
}
.
.
.
.
public void onDrawFrame(GL10 gl) {
// Redraw background color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Draw the square
gl.glColor4f(0.0f, 0.0f, 1.0f, 0.0f); //blue
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, square1);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 4);
}
You're specifying GL_TRIANGLES but only have four vertices. Try six.
Or use GL_TRIANGLE_STRIP.
Left top point seem to be wrong, should be -0.5, 0.5, 0.0 and I also agree with genpfault that think you should use GL_TRIANGLE_STRIP
ok.. ı found a way to solve the problem, ı think..
//this is our Square class
public class Square {
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
-0.3f, -0.3f, 0.0f, // 0. left-bottom
0.3f, -0.3f, 0.0f, // 1. right-bottom
-0.3f, 0.3f, 0.0f, // 2. left-top
0.3f, 0.3f, 0.0f // 3. right-top
};
public Square() {
// a float has 4 bytes so we allocate for each coordinate 4 bytes
ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
vertexByteBuffer.order(ByteOrder.nativeOrder());
// allocates the memory from the byte buffer
vertexBuffer = vertexByteBuffer.asFloatBuffer();
// fill the vertexBuffer with the vertices
vertexBuffer.put(vertices);
// set the cursor position to the beginning of the buffer
vertexBuffer.position(0);
}
/** The draw method for the square with the GL context */
public void draw(javax.microedition.khronos.opengles.GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// set the colour for the square
gl.glColor4f(1.0f, 0.0f, 0.0f, 0.0f); //red
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// 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);
}
}
// and this is our renderer class
public class MyRenderer implements GLSurfaceView.Renderer {
// the square to be drawn
private Square square;
public MyRenderer() {
this.square = new Square();
#Override
public void onDrawFrame(GL10 gl) {
// 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(0.0f, 0.0f, -5.0f);
// Draw the square
square.draw(gl);
}
#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, width, height); //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) {
}
}
I have the following code for my Renderer:
package hello.project;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
public class HelloOpenGLES10Renderer implements Renderer {
private Square square;
private Square2 square2;
private Square3 square3;
private SquareAccesories squareAcc;
private SquareEyes squareEyes;
private SquareLips squareLips;
private SquarePants squarePants;
private SquareShoes squareShoes;
private Context context;
//public static int w,h;
/** Constructor to set the handed over context */
public HelloOpenGLES10Renderer(Context context) {
this.square = new Square();
this.square2 = new Square2();
this.square3 = new Square3();
this.squareAcc = new SquareAccesories();
this.squareEyes = new SquareEyes();
this.squareLips = new SquareLips();
this.squarePants = new SquarePants();
this.squareShoes = new SquareShoes();
this.context=context;
}
public void onDrawFrame(GL10 gl) {
if (Project.ifDraw){
Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
Square3.loadGLTexture(gl, this.context,Square3.getDress());
SquareAccesories.loadGLTexture(gl, this.context,SquareAccesories.getAcc());
SquareEyes.loadGLTexture(gl, this.context,SquareEyes.getEyes());
SquareLips.loadGLTexture(gl, this.context,SquareLips.getLips());
SquarePants.loadGLTexture(gl, this.context,SquarePants.getPants());
SquareShoes.loadGLTexture(gl, this.context,SquareShoes.getShoes());
Project.ifDraw=false;
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
//GLU.gluPerspective(gl, 45.0f, (float)w / (float)h, 0.1f, 100.0f);
GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);
square.draw(gl);
square2.draw(gl);
square3.draw(gl);
squareEyes.draw(gl);
squareAcc.draw(gl);
squareLips.draw(gl);
squareShoes.draw(gl);
squarePants.draw(gl);
/*// clear Screen and Depth Buffer
Square.loadGLTexture(gl, this.context,Square.getSex());
Square2.loadGLTexture(gl, this.context,Square2.getHair());
gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// Drawing
gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
square.draw(gl);
square2.draw(gl);*/
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
//w=width;
//h=height;
//Square.loadGLTexture(gl, this.context,Square.getSex());
//Square2.loadGLTexture(gl, this.context,Square2.getHair());
gl.glViewport(0, 0, width, height); //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 Mode lview Matrix
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Load the texture for the square
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_FLAT); //Enable Smooth Shading
//gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//Square.loadGLTexture(gl, this.context,Square.getSex());
//Square2.loadGLTexture(gl, this.context,Square2.getHair());
//Square3.loadGLTexture(gl, this.context,Square3.getDress());
//SquareAccesories.loadGLTexture(gl, this.context,SquareAccesories.getAcc());
//SquareEyes.loadGLTexture(gl, this.context,SquareEyes.getEyes());
//SquareLips.loadGLTexture(gl, this.context,SquareLips.getLips());
//SquarePants.loadGLTexture(gl, this.context,SquarePants.getPants());
//SquareShoes.loadGLTexture(gl, this.context,SquareShoes.getShoes());
//gl.glAlphaFunc(GL10.GL_GREATER, 0.5f);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glDepthFunc(GL10.GL_NEVER); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
On the emulator, it works perfect. I used an Xperia Play for debugging, and also tried it on an HTC xplorer and Galaxy Nexus, and it worked.
2day i tried it on a Samsung Galaxy and an HTC phone but the SurfaceView did not show nothing, it was blank, any ideeas why this could happen?
Made the pictures dimensions powers of 2 and know it works
I'm building an Android app with OpenGL.I created 2 squares, each with their own textures(PNG), and overlayed them. From hints i got from a previous question, i used gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
My problem is that the transparency effect, affects the second square, therefor i can see the background through the second square's texture.Is there a way arround this?
Here is the Renderer and at the Bottom the Square.java class :
package hello.project;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class Square {
private FloatBuffer vertexBuffer; // buffer holding the vertices
static int sex=R.drawable.girl;
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 float vertices[] = {
-1.0f, -2.0f, 0.0f, // V1 - bottom left
-1.0f, 2.0f, 0.0f, // V2 - top left
0.8f, -2.0f, 0.0f, // V3 - bottom right
0.8f, 2.0f, 0.0f // V4 - top right
};
public Square() {
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);
}
/** The draw method for the square with the GL context */
public void draw(GL10 gl) {
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// 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);
// 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);
}
/** The texture pointer */
private int[] textures = new int[1];
public void loadGLTexture(GL10 gl, Context context,int sex ) {
// loading texture
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
sex);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
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);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
public static int getSex() {
return sex;
}
public static void setSex(int sex) {
Square.sex = sex;
}
}
---------------------------------------------------------------------------
package hello.project;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
public class HelloOpenGLES10Renderer implements Renderer {
private Square square; // the square
private Square2 square2; // the square
private Context context;
/** Constructor to set the handed over context */
public HelloOpenGLES10Renderer(Context context) {
this.square = new Square();
this.square2 = new Square2();
this.context=context;
}
public void onDrawFrame(GL10 gl) {
// 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(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
square.draw(gl);
square2.draw(gl);
}
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, width, height); //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
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Load the texture for the square
square.loadGLTexture(gl, this.context,Square.getSex());
square2.loadGLTexture(gl, this.context,Square2.getSex());
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glDepthFunc(GL10.GL_NEVER); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
Don't you just have a problem with the graphics pipeline state? Remember whichever square you tell to get drawn first gets drawn using the blend function that's currently active (and this will remain active until you change the OpenGL state). Maybe you want to add some more state changes to the blend function, or change the order of drawing to get the effect you want? You might want to also try enabling/disabling the depth test between drawing calls to make a square opaque.
Hope this helps a bit, you might need to provide a bit more detail to your question.
I have an GlSurfaceView that is drawing a square that i am trying to texturize with a image.png I have played with two different png files both in the same drawable folder.. When i reference one it loads just fine but if i switch the R.image.png to the other, my sqaure just turns white.. can anyone help? below is my square class, and my glView.
Square:
package com.eliddell.AR;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class Square {
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
-1.0f, -1.0f, 0.0f, // V1 - bottom left
-1.0f, 1.0f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 - bottom right
1.0f, 1.0f, 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)
};
public Square() {
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);
}
/** The texture pointer */
private int[] textures = new int[1];
public void loadGLTexture(GL10 gl, Context context) {
// loading texture
InputStream is = context.getResources().openRawResource(R.drawable.android);
Bitmap bitmap = BitmapFactory.decodeStream(is);
//Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.android);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
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);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
public void draw(GL10 gl) {
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// 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);
// 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);
}
}
and my GLSurfaceView and Render:
package com.eliddell.AR;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.view.SurfaceHolder;
public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {
private Context context;
private Square square; // the triangle to be drawn
public GLLayer(Context context) {
super(context);
this.context = context;
this.square = new Square();
// settings for translucent glView
this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
// set render to inline
this.setRenderer(this);
}
#Override
public void onDrawFrame(GL10 gl) {
// 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(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
// is the same as moving the camera 5 units away
square.draw(gl); // 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, width, height); //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 arg1) {
// Load the texture for the square
square.loadGLTexture(gl, this.context);
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.0f); //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);
}
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
}
}
Propably your texture dosent have power of two dimensions.
Opengl can load only textures witch have pot(power of two) size like: 32X32 , 64X64 , 128X128 ..... and not 36X36 , 48X48 ....
Change your image size.