Need help understanding glDrawElements for GLES 2.0 - android

I'm trying to learn OpenGL ES 2.0 for Android, but I am finding it hard to locate good introduction tutorials... I found This, but it only explains "glDrawArrays" and not "glDrawElements"... I'm trying to convert code from my model loading class ES 1.1, but I feel that drawing arrays might be too slow...
So what I'm asking is how would I convert the following to work in ES 2.0?
How an object is stored (seems to be fine):
private ShortBuffer SindexBuffer;
private FloatBuffer SvertexBuffer;
private FloatBuffer StexBuffer;
private void initSprite()
{
float[] fcol = {1,1,1,1};
float[] coords = {
0.5f, 0.5f, 0f,
-0.5f, 0.5f, 0f,
0.5f, -0.5f, 0f,
-0.5f, -0.5f, 0f
};
short[] index = {
0,1,2,
1,3,2
};
float[] texCoords ={
0,1,
1,1,
0,0,
1,0
};
//float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
SvertexBuffer = vbb.asFloatBuffer();
ByteBuffer tC = ByteBuffer.allocateDirect(texCoords.length * 4);
tC.order(ByteOrder.nativeOrder());
StexBuffer = tC.asFloatBuffer();
StexBuffer.put(texCoords);
StexBuffer.position(0);
//short has 2 bytes
ByteBuffer ibb = ByteBuffer.allocateDirect(index.length*2);
ibb.order(ByteOrder.nativeOrder());
SindexBuffer = ibb.asShortBuffer();
SvertexBuffer.put(coords);
SindexBuffer.put(index);
SvertexBuffer.position(0);
SindexBuffer.position(0);
ByteBuffer fbb = ByteBuffer.allocateDirect(fcol.length * 4);
fbb.order(ByteOrder.nativeOrder());
}
And how it is drawn (this is where I need help):
public void drawSprite(GL10 gl, int tex)
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
//defines the vertices we want to draw
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, SvertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, StexBuffer);
//Draw the vertices
gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, SindexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
(though the texture part isn't that important yet... not up to that yet).
Thanks to anyone who can help... oh, and does anyone know any simple introductions to ES 2.0 for Android aimed at people who used 1.1 first?

Translate as this
float[] coords = {
0.5f, 0.5f, 0f, //index 0
-0.5f, 0.5f, 0f, //index 1
0.5f, -0.5f, 0f, //index 2
-0.5f, -0.5f, 0f //index 3
}
And you are telling to opengl that plot using this sequence of points:
0, 1, 2, 1, 3 , 2.
Here you save memory becouse indexes are short type and each vertex needs 3 floats
Regards

Related

Android OpenGL ES doesn't display my triangulated shape using TRIANGLES

I want to display shapes as different as one likes using OpenGL ES on an Android device. Problem is that my code doesn't even work for easy shapes like a rectangle (which I am going to use below).
I think somthing is wrong with the glTranslatef. I've adjusted all the values but I can't figure out what it is.
The Rectangle is defined by the points P(0,0,0), P(0,1,0), P(1,1,0), P(1,0,0). In the Activity I implemented the GLSurfaceView.Renderer like this:
private static FloatBuffer getVertexCoords() {
float coords[] = {
0f, 0f, 0f, // first triangle first point
0f, 1f, 0f, // first triangle second point
1f, 1f, 0f, // first triangle third point
1f, 1f, 0f, // second triangle first point
1f, 0f, 0f, // second triangle second point
0f, 0f, 0f, // second triangle third point
}
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4); // n coords * 4 bytes per float
vbb.order(ByteOrder.nativeOrder());
FloatBuffer trianglesVB = vbb.asFloatBuffer();
trianglesVB.put(coords);
trianglesVB.position(0);
return trianglesVB;
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0f, 0f, -4f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // glBegin
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, getVertexCoords());
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2 * 3 * 3); // triangles * points * coords
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // glEnd
int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR) {
Log.e(TAG, "OpenGL ES Error: " + error);
}
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// think this one doesn't matter
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background
gl.glFrontFace(GL10.GL_CW); // front face is clockwise
}
I think you need a projection matrix in there somewhere. If you don't set one, then you are drawing directly in normalized device coordinates, of which the only valid z-values are from (-1 to 1).
Simply your triangle is outside of the depth range displayed.
Try adding a simple projection matrix to onSurfaceCreated:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, 0, 10);

OpenGL ES 1.1 Android Cubemapping

I want to get a simple cube mapping going without using a texture atlas. I'm able to bind a see any one of my textures at any given time, but can't seem to do more than 1, let alone 6. Below is the code from my drawing loop.
/*Cube to draw */
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cube);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[2]);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, circleTexCoords);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[3]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[4]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[5]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[6]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D,mTextures[7]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
Am I missing something fundamental here?
Things I have done an checked:
Yes, I generated a texture buffer.
Yes, I properly loaded and bound textures from my resources.
Yes, the above code works when instead of binding textures I draw it with glColorf
I appreciate your help.
Edit:
How I generated cube:
private float box[] = new float[] {
// FRONT
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// BACK
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// LEFT
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
// RIGHT
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// TOP
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// BOTTOM
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
};
/* Initialize values for cube */
ByteBuffer bb = ByteBuffer.allocateDirect(Float.SIZE * box.length);
bb.order(ByteOrder.nativeOrder());
cube = bb.asFloatBuffer();
cube.put(box);
cube.rewind();
CircleTexCoords is a misnomer, I'm using it as a unit texture coordinates for several different textures:
bb = ByteBuffer.allocateDirect(Float.SIZE * 8);
bb.order(ByteOrder.nativeOrder());
circleTexCoords = bb.asFloatBuffer();
circleTexCoords.put( new float[] { 0f, 1f, 1f, 1f, 0f, 0f, 1f, 0f});
circleTexCoords.rewind();
Your vertex arrays don't match. When drawing from the currently enabled attribute arrays, in your case vertex and tecCoords, The arrays have to match in size of course. When you call glDrawArrays(..., 8, 4) you instruct OpenGL to draw 4 array elements (vertices) starting at the 8th element. But your texCoord array contains only 4 vertices, what should the texCoords of the vertices 4-23 be in your vertex array? Or how should OpenGL know that you want to repeat the texCoords for each 4 consecutive vertices.
So in order for it to work, you need to repeat the texCoords yourself. Always keep in mind that the sizes of all enabled arrays have to match when drawing from them (at least up to the last drawn element). So your idea of reusing this small circleTexCoords array won't work.
This is because conceptually a vertex is not just a position, but the compaund of all attributes (positions, normals, texCoords, ...). Likewise is a vertex not only an element from the array bound to glVertexPointer, but from all currently enabled arrays. That's also the reason why you cannot have a single vertex with two different texCoords without duplication in OpenGL, because well, then it wouldn't be a single vertex anymore. It was a bit of a bad choice to name the position attribute vertex in OpenGL, though this is more for legacy reasons and has been cleaned up in modern OpenGL.

Problem with GL_TRIANGLES

i am working on a program which draws polygons according to user inputs.
I have problems with drawing triangles using GL_TRIANGLE. I used the same code below to draw square and it worked well. Hovewer, if i want to draw only one triangle it does not work.
Can anyone help me?
public class Triangle extends Shape{
private FloatBuffer vertexBuffer;
private FloatBuffer _colorBuffer;
private ShortBuffer indexBuffer;
private float vertices[] = {
-0.5f, -0.5f, 0.5f, // 0
0.5f, -0.5f, 0.5f, // 1
0f, -0.5f, -0.5f, // 2
};
private short[] indices = { 0, 2, 1 };
float[] colors = {1f, 1f, 0f, 1f };
public Triangle() {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
_colorBuffer = cbb.asFloatBuffer();
_colorBuffer.put(colors);
_colorBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}
edit:
i call my Triangle class from here, maybe i have made mistake here
public class OpenGLRenderer implements Renderer {
String name;
ArrayList myArr ;
private float angle, x,y,z;
public OpenGLRenderer(String nm ) {
name =nm;
myArr = new ArrayList<Shape>();
x=0;
y=0;
z=-3;
}
#Override
public void onDrawFrame(GL10 gl) {
//clear the screen and depth buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(x, y, z);
for (Shape t : myArr)
{
if (t instanceof Rectangle)
{
// gl.glTranslatef(0, 0, -4);
((Rectangle )t).draw(gl);
}
if (t instanceof Square)
{ //gl.glTranslatef(0, 1, 0);
((Square )t).draw(gl);}
if (t instanceof Pyramid){
((Pyramid)t).draw(gl);
if (t instanceof Triangle){
((Triangle)t).draw(gl);
}
if (t instanceof Line){
((Line)t).draw(gl);
}
}
}//for
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity(); //reset the projection matrix
GLU.gluPerspective(gl, 45.0f, (float)width/(float)height,
0.1f, 100.0f); //calculate the aspect ratio of window
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//set the bg as black
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
//depth buffer setup
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 addshape(Shape s)
{y=y+0.1f;
myArr.add(s);
}
}
It looks to me like you don't have enough colors in your colors array. This might result in an invisible triangle depending on the initial garbage values in your _colorBuffer.
(Edit) try:
float[] colors = {
1f, 1f, 0f, 1f,
1f, 1f, 0f, 1f,
1f, 1f, 0f, 1f
};
First, Martin is correct about the colors array. It needs to have a color (4 values in your case) for every vertex (so 12 values at all).
Next, at the moment your triangle lies inside the x-z-plane and as you don't make any changes to the modelview matrix (except translating along the z-axis), you should just see a line, if any, (think of a sheet of paper viewed from the side).
But your real problem is your draw loop. I guess you're not only new to OpenGL, but also to Java and object oriented programming in general. This whole design is complete rubbish. That's what virtual functions are for in object oriented code. Just let each shape implement its correct draw method. So the only thing you need to do is
for (Shape t : myArr)
t.draw(gl);
Given that Shape has an abstract draw method, that the other subclasses implement. But this is more of a design flaw. The actual error is, that the braces of the ifs are broken. At the moment the triangle is only drawn, if t is an instance of Pyramid and of Triangle, so draw is never called for triangles (and also for lines).
here my triangle code from a project which works. Looks like your indices and colour arrays are different
package com.martynhaigh.Vortex;
import android.view.animation.Transformation;
import javax.microedition.khronos.opengles.GL10;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
public class Triangle {
private ShortBuffer _indexBuffer;
private FloatBuffer _vertexBuffer;
private FloatBuffer _colorBuffer;
static float _xAngle, _yAngle;
private int _nrOfVertices;
Triangle() {
float[] coords = {
-0.5f, -0.5f, 0f, // (x1, y1, z1)
0.5f, -0.5f, 0f, // (x2, y2, z2)
0f, 0.5f, 0f // (x3, y3, z3)
}; // 9
_nrOfVertices = coords.length / 3;
float[] colors = {
1f, 0f, 0f, 1f, // point 1
0f, 1f, 0f, 1f, // point 2
0f, 0f, 1f, 1f // point 3
}; // 12
short[] indices = {0, 1, 2}; // 3
// float has 4 bytes, coordinate * 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4); //36
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer = vbb.asFloatBuffer();
// short has 2 bytes, indices * 2 bytes
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); // 6
ibb.order(ByteOrder.nativeOrder());
_indexBuffer = ibb.asShortBuffer();
// float has 4 bytes, colors (RGBA) * 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); // 48
cbb.order(ByteOrder.nativeOrder());
_colorBuffer = cbb.asFloatBuffer();
_vertexBuffer.put(coords);
_indexBuffer.put(indices);
_colorBuffer.put(colors);
_vertexBuffer.position(0);
_indexBuffer.position(0);
_colorBuffer.position(0);
}
public void onDraw(GL10 gl) {
// set rotation
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);
// set the color of our element
//gl.glColor4f(0.5f, 0f, 0f, 0.5f);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
// define the vertices we want to draw
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
// finally draw the vertices
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
}
public void setXAngle(float angle) {
_xAngle = angle;
}
public float getXAngle() {
return _xAngle;
}
public void setYAngle(float angle) {
_yAngle = angle;
}
public float getYAngle() {
return _yAngle;
}
}

Issue drawing triangles with basic VBO in OpenGL ES for Android 2.1

I am starting up work on an Android game and am learning OpenGL ES. I have used OpenGL a bit, though it was quite some time ago by now. I have mostly used DirectX lately with C++, so I understand graphic API concepts fairly well.
When, playing with the API on my own, I was unable to get the results I anticipated, I turned to this tutorial I found online that seemed fairly comprehensive, and though I understood it easily and followed the tutorial fairly strictly, I still can't get the screen to display a simple square (currently not using anything other than a vertex array with no colour).
Below is the code for my renderer class which I have been staring at for some time and am starting to go a little crazy with my inability to find my mistake. I have done far more complicated things with graphics APIs (in both DirectX and OpenGL) so I find this kind of embarrassing and just need somebody to point out my probably glaringly obvious oversight.
Thank you in advance!
public class GameRenderer implements Renderer {
private float red, green, blue = 0.0f;
private final float vertices[] = {
0.5f, -0.5f, 0.0f, // 0, Bottom Right
0.5f, 0.5f, 0.0f, // 1, Top Right
-0.5f, 0.5f, 0.0f, // 2, Top Left
-0.5f, -0.5f, 0.0f, // 3, Bottom Left
};
private final short indices[] = {
0, 1, 2, 0, 2, 3
};
private final float colours[] = {
1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.0f, 0.0f, 0.0f
};
FloatBuffer vFBuff;
FloatBuffer cFBuff;
ShortBuffer iSBuff;
public GameRenderer(){
super();
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glClearColor(red, green, blue, 0.5f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glScalef(2.0f, 2.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, -4f);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, iSBuff);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
// set viewport
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.0f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
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);
ByteBuffer vBBuff = ByteBuffer.allocateDirect(vertices.length * 4);
ByteBuffer cBBuff = ByteBuffer.allocateDirect(colours.length * 4);
ByteBuffer iBBuff = ByteBuffer.allocateDirect(indices.length * 2);
vFBuff = vBBuff.asFloatBuffer();
vFBuff.put(vertices);
vFBuff.position(0);
cFBuff = cBBuff.asFloatBuffer();
cFBuff.put(colours);
cFBuff.position(0);
iSBuff = iBBuff.asShortBuffer();
iSBuff.put(indices);
iSBuff.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vFBuff);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
}
public void setColour(float r, float g, float b) {
red = r;
blue = b;
green = g;
}
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.0f, 100.0f);
Don't set your zNear to zero:
If (r = zFar / zNear) roughly log2(r) bits of depth buffer precision are lost. Because r approaches infinity as zNear approaches 0, zNear must never be set to 0.

mapping a texture on a square (Android)

Im new to openGL, and im trying to map an texture to a square. I followed NeHe's tutorial on texture mapping here:
http://insanitydesign.com/wp/wp-content/uploads/lesson06.zip
Right now i see my image...but its not mapping correctly. Heres the original image:
http://ge.tt/2FzsdIx
...and heres what im seeing.
http://ge.tt/6y3cdIu
I used the vertices and texture arrays from this great iphone tutorial (link below) so im hoping they have been mapped correctly. Below is the link to my code in Square.java, thanks!
public class Square {
// Our vertices.
private float vertices[] = {
-1.0f, 1.0f, 0.0f, // 0, Top Left
-1.0f, -1.0f, 0.0f, // 1, Bottom Left
1.0f, -1.0f, 0.0f, // 2, Bottom Right
1.0f, 1.0f, 0.0f, // 3, Top Right
};
// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };
// Our vertex buffer.
private FloatBuffer vertexBuffer;
// Our index buffer.
private ShortBuffer indexBuffer;
/** The buffer holding the texture coordinates */
private FloatBuffer textureBuffer;
//the texture pointer, holds the texture name which is actually a number.
private int[] textures = new int[1];
public Square() {
// a float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
// short is 2 bytes, therefore we multiply the number if
// vertices with 2.
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
//plot our texture
float textCoords[]={
//Mapping coordinates for the vertices
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
ByteBuffer tbb = ByteBuffer.allocateDirect(textCoords.length * 4); tbb.order(ByteOrder.nativeOrder());
textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textCoords);
textureBuffer.position(0);
}
//load our texture(s)
static void loadTexture(GL10 gl, Context context, int resource) {
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),resource);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
bmp.recycle();
}
/**
* This function draws our square on screen.
* #param gl
*/
public void draw(GL10 gl) {
//use our textures
gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Counter-clockwise winding.
gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
// Enable face culling.
gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
// What faces to remove with the face culling.
gl.glCullFace(GL10.GL_BACK); // OpenGL docs
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,// OpenGL docs
GL10.GL_UNSIGNED_SHORT, indexBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL docs
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs
}
}
iPhone tutorial:
http://www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-6-textures-and-texture-mapping.html
You can draw faster using a triangle fan, in the following order in your indices, faster.
01
32
Then you don't need to use drawElements or indices, you can just feed it to drawArrays and only need 4 elements.
Your bug is that the , the tex coords are wrong
tl is 0,0
bl is 0,1
br is 1,1
tr is 1,0
You have
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
So your UV is wrong.
Normally the rendering of a square in OpenGL looks something like this
gl.glLoadIdentity();
gl.glBindTexture(GL.GL_TEXTURE_2D,0);
gl.glBegin(GL_QUADS)
glVertex(x,y,z);
glTexcoord2f(s,t);
glVertex(-x,y,z);
glTexcoord2f(-s,t);
glVertex(-x,-y,z);
glTexcoord2f(-s,-t);
glVertex(x,-y,z);
glTexcoord2f(s,-t);
gl.glEnd();
I don't see anything like that, but I have never done GLES on Android before so I may be too old.
see https://github.com/ChrisLundquist/Asteroids/blob/master/src/ScenePanel.java#L277

Categories

Resources