Rendering Height Map, glDrawElements Issues - java

I've got this class which I'm developing which I hope to eventually use to draw a terrain map. The code currently looks like this:
IntBuffer ib = BufferUtils.createIntBuffer(3);
int vHandle = ib.get(0);
int cHandle = ib.get(1);
int iHandle = ib.get(2);
FloatBuffer vBuffer = BufferUtils.createFloatBuffer(19);
FloatBuffer cBuffer = BufferUtils.createFloatBuffer(18);
ShortBuffer iBuffer = BufferUtils.createShortBuffer(6);
TerrainFlat(float x,float y,float z, float[] input /* here is where I want to put my draw distance stuff*/){
this.Map = input;
this.x=x;
this.y=y;
this.z=z;
}
#Override
public void initilize(){
float[] vertexData = {50, 20, 100, 50, -20, 100, 10, -20, 100, -10, -20, 100, -50, -20, 100, -50, 20, 100};
float[] colorData = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 };
short[] indexData = {0,1,2,3,4,5};
vBuffer.put(vertexData);
vBuffer.flip();
cBuffer.put(colorData);
cBuffer.flip();
iBuffer.put(indexData);
iBuffer.flip();
}
#Override
public void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glGenBuffersARB(ib);
vHandle = ib.get(0);
cHandle = ib.get(1);
iHandle = ib.get(2);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
glVertexPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, cHandle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, cBuffer, GL_STATIC_DRAW_ARB);
glColorPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iHandle);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iBuffer, GL_STATIC_DRAW_ARB);
glDrawElements(GL_TRIANGLES, /* elements */6, GL_UNSIGNED_SHORT, 0L);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
#Override
public void destroy(){
ib.put(0, vHandle);
ib.put(1, cHandle);
ib.put(2, iHandle);
glDeleteBuffersARB(ib);
}
Now I'm wondering if I can bind my data in the initilize function, instead of the Draw function, in order to move the declarations:
FloatBuffer vBuffer = BufferUtils.createFloatBuffer(19);
FloatBuffer cBuffer = BufferUtils.createFloatBuffer(18);
ShortBuffer iBuffer = BufferUtils.createShortBuffer(6);
into the initilize function, or at the very least after the terrain functions so I can control their lengths via a draw distance function.
UPDATE:
I change my code based on the answer given (thank you btw) to this:
#Override
public void initilize(){
float[] vertexData = {50, 20, 100, 50, -20, 100, 10, -20, 100, -10, -20, 100, -50, -20, 100, -50, 20, 100};
float[] colorData = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 };
short[] indexData = {0,1,2,3,4,5};
vBuffer.put(vertexData);
vBuffer.flip();
cBuffer.put(colorData);
cBuffer.flip();
iBuffer.put(indexData);
iBuffer.flip();
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, cHandle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, cBuffer, GL_STATIC_DRAW_ARB);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iHandle);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iBuffer, GL_STATIC_DRAW_ARB);
}
#Override
public void setUp(float posX, float posY, float posZ){
}
#Override
public void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glGenBuffersARB(ib);
vHandle = ib.get(0);
cHandle = ib.get(1);
iHandle = ib.get(2);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
glVertexPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, cHandle);
glColorPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iHandle);
glDrawElements(GL_TRIANGLES, /* elements */6, GL_UNSIGNED_SHORT, 0L);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
This executes but fails to draw anything, I do not have FACE_CULL enabled either.

You need to bind the buffers before drawing (except you are always drawing the same buffers) but you can move the glBufferData calls to your initialize function.
Something like this should work:
public void initilize() {
...
// set data for vHandle
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
glVertexPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
// ... setup other handles
...
}
public void draw() {
...
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
// ... bind other buffers
glDrawElements(GL_TRIANGLES, /* elements */6, GL_UNSIGNED_SHORT, 0L);
...
}

Related

Some parts are missing when render 3D object in android(java) using OpenGLES 2

I'm new to OpenGL, and I am trying to load a .obj file into my Android application and display it using OpenGLES 2. The object is rendered. But there are some spaces in it like the below image. How can I fix this?
This is the actual object.
Here is my code (additional: I'm using this in MaxST Instant tracker)
objLoader = new ObjLoader(context, "andy.obj");
numFaces = objLoader.numFaces;
positions = ByteBuffer.allocateDirect(objLoader.positions.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
positions.put(objLoader.positions).position(0);
normals = ByteBuffer.allocateDirect(objLoader.normals.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
normals.put(objLoader.normals).position(0);
textureCoordinates = ByteBuffer.allocateDirect(objLoader.textureCoordinates.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
textureCoordinates.put(objLoader.textureCoordinates).position(0);
ByteBuffer bb = ByteBuffer.allocateDirect(objLoader.positions.length * Float.SIZE / 8);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(positions);
vertexBuffer.position(0);
bb = ByteBuffer.allocateDirect(objLoader.textureCoordinates.length * Float.SIZE / 8);
bb.order(ByteOrder.nativeOrder());
textureCoordBuff = bb.asFloatBuffer();
textureCoordBuff.put(textureCoordinates);
textureCoordBuff.position(0);
shaderProgramId = ShaderUtil.createProgram(VERTEX_SHADER_SRC, FRAGMENT_SHADER_SRC);
positionHandle = GLES20.glGetAttribLocation(shaderProgramId, "a_position");
textureCoordHandle = GLES20.glGetAttribLocation(shaderProgramId, "a_texCoord");
mvpMatrixHandle = GLES20.glGetUniformLocation(shaderProgramId, "u_mvpMatrix");
textureHandle = GLES20.glGetUniformLocation(shaderProgramId, "u_texture");
textureNames = new int[1];
GLES20.glGenTextures(1, textureNames, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureNames[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
(ObjLoader class is from here)
Here is my draw method
#Override
public void draw() {
GLES20.glUseProgram(shaderProgramId);
GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false,
0, vertexBuffer);
GLES20.glEnableVertexAttribArray(positionHandle);
GLES20.glVertexAttribPointer(textureCoordHandle, 2, GLES20.GL_FLOAT, false,
0, textureCoordBuff);
GLES20.glEnableVertexAttribArray(textureCoordHandle);
Matrix.setIdentityM(modelMatrix, 0);
Matrix.multiplyMM(modelMatrix, 0, translation, 0, rotation, 0);
Matrix.multiplyMM(modelMatrix, 0, modelMatrix, 0, scale, 0);
Matrix.multiplyMM(modelMatrix, 0, transform, 0, modelMatrix, 0);
Matrix.multiplyMM(localMvpMatrix, 0, projectionMatrix, 0, modelMatrix, 0);
GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, localMvpMatrix, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glUniform1i(textureHandle, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureNames[0]);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, objLoader.positions.length / 3);
GLES20.glDisableVertexAttribArray(positionHandle);
GLES20.glDisableVertexAttribArray(textureCoordHandle);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
}
The model file seems to consist of quads. If the faces are quads, the you have to generate 2 triangles for each face.
Adapt the object loader. e.g.:
class ObjLoader {
// [...]
public ObjLoader(Context context, String file) {
// [...]
switch (parts[0]) {
// [...]
case "f":
// faces: vertex/texture/normal
if (parts.length == 5) {
// triangle 1
faces.add(parts[1]);
faces.add(parts[2]);
faces.add(parts[3]);
// triangle 2
faces.add(parts[1]);
faces.add(parts[3]);
faces.add(parts[4]);
}
else {
faces.add(parts[1]);
faces.add(parts[2]);
faces.add(parts[3]);
}
break;
}
// [...]
}
}

Why is glDrawElements() not drawing anything?

I've seen a couple of questions about the same topic but still couldn't figure out what was wrong with my code.
It worked to get a window running, where a keypress would change the background color with glClearColor().
But now, when trying to draw a quad onto the screen, the screen stays black.
A little summary of how I think it should work:
How mesh is created:
public Mesh(Vertex[] verts, int[] indices) {
this.verts = verts;
this.indices = indices;
}
//Differend class
public Mesh mesh = new Mesh(new Vertex[] {
new Vertex(new Vector3f(-0.5f, 0.5f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.0f))
}, new int[] {
0, 1, 2,
0, 3, 2
});
For initializing the game, this is called:
public void create() {
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(verts.length * 3);
float[] positionData = new float[verts.length * 3];
for (int i = 0; i < verts.length; i++) {
positionData[i * 3] = verts[i].getPosition().getX();
positionData[i * 3 + 1] = verts[i].getPosition().getY();
positionData[i * 3 + 2] = verts[i].getPosition().getZ();
}
positionBuffer.put(positionData).flip();
pbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, pbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
IntBuffer indexBuffer = MemoryUtil.memAllocInt(indices.length * 3);
indexBuffer.put(indices).flip();
ibo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
And lastly, the mesh should be rendered with this:
public void renderMesh(Mesh mesh) {
clear();
GL30.glBindVertexArray(mesh.getVAO());
GL30.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIBO());
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_FLOAT, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
I've checked that the methods are actually invoked by using System.out.println.
The game's input still works. It's just that the screen is black and not showing any quad.
Why is it not drawing anything onto the screen?
The type argument which is set to glDrawElements hs to correspond to the index buffer, rather than the vertex buffer.
In your case it has to be GL_UNSIGNED_INT rather than GL_FLOAT. See glDrawElements.
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_FLOAT, 0);
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
Note, GL_FLOAT is not an accepted value for glDrawElements and will cause an INVALID_ENUM error.

Why does this not make a good looking square?

Ok, first of all I totally agree that "TheNewBoston" tutorials are very... wrong at times. However that is what I was following to try to make a cube in OpenGL on android.
Anyways, I can't say that I have found great material on OpenGL to be honest. I have read through OpenGL Superbible... it was ok.
So here is the code that I have.
GLRenderer.java
package android.gem.opengltest;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.os.SystemClock;
public class GLRenderer implements Renderer {
private GLCube cube;
public GLRenderer() {
cube = new GLCube();
}
#Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);
long time = SystemClock.uptimeMillis() % 4000L;
float angle = .09f * ((int) time);
gl.glRotatef(angle, 1, 1.5f, 2);
cube.draw(gl);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
float ratio = (float) width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
// TODO Auto-generated method stub
// START OF PERFORMANCE //
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// END OF PERFORMANCE //
gl.glClearColor(.8f, 0, .2f, 1f); // Red, Green, Blue, Alpha (0 - 1)
gl.glClearDepthf(1f);
}
}
GLCube.java
package android.gem.opengltest;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
public class GLCube {
private float verticies[] = {
1, 1, -1, // point 0 - topFrontRight
1, -1, -1, // point 1 - bottomFrontRight
-1, -1, -1, // point 2 - bottomFrontLeft
-1, 1, -1, // point 3 - frontTopLeft
1, 1, 1, // point 4 - topBackRight
1, -1, 1, // point 5 - bottomBackRight
-1, -1, 1, // point 6 - bottomBackLeft
-1, 1, 1 // point 7 - frontBackLeft
};
private float rgbaValues[] = {
1, 1, 0, 1,
.25f, 0, .85f, 1,
0, 1, 1, 1,
1, 1, 0, 1,
.25f, 0, .85f, 1,
0, 1, 1, 1,
.5f, .5f, .2f, 1,
.3f, .3f, .3f, 1
};
private FloatBuffer vertBuff, colorBuff;
private short[] pIndex = { // Indicies
3, 4, 0, 0, 4, 1, 3, 0, 1,
3, 7, 4, 7, 6, 4, 7, 3, 6,
3, 1, 2, 1, 6, 2, 6, 3, 2,
1, 4, 5, 5, 6, 1, 6, 5, 4
};
private ShortBuffer pBuffer;
public GLCube() {
ByteBuffer bBuff = ByteBuffer.allocateDirect(this.verticies.length * 4);
bBuff.order(ByteOrder.nativeOrder());
vertBuff = bBuff.asFloatBuffer();
vertBuff.put(this.verticies);
vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(this.pIndex.length * 2);
pbBuff.order(ByteOrder.nativeOrder());
pBuffer = pbBuff.asShortBuffer();
pBuffer.put(pIndex);
pBuffer.position(0);
ByteBuffer cBuff = ByteBuffer.allocateDirect(this.rgbaValues.length * 4);
cBuff.order(ByteOrder.nativeOrder());
colorBuff = cBuff.asFloatBuffer();
colorBuff.put(rgbaValues);
colorBuff.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW); // Clock Wise
gl.glEnable(GL10.GL_CULL_FACE); // Removes back end of cube
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertBuff);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, this.colorBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, this.pIndex.length, GL10.GL_UNSIGNED_SHORT, this.pBuffer);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}
I get a cube that looks like this...
What am I doing wrong? What should I do to make it look like an actual cube?
Also, if it helps all my code is at the following address: https://github.com/gemurdock/OpenGlTest/tree/alpha.0.1.0
First of all, this is a (horribly deformed) cube, not a square. Unless you look at it face-on it is never going to look like a "good square" :)
Your polygon face culling appears to be culling the wrong side of your cube faces. It still looks like a cube, just an inside out cube.
This is likely because you have overridden OpenGL's default front face winding direction (GL_CCW).

drawing multiple objects in OpenGL

I started learning OpenGL. I can load a .obj model and draw it with an elementBuffer. But I'm stuck at trying to two different models at a time. The model I want to draw is in an Entity class.
Most tutorials I can find about this only shows how to load and draw a sinlge model. None explains (In a way I can find/understand at least) how to handle multiple models.
Here is all my code:
public static void main(String[] args) throws LWJGLException, IOException
{
PixelFormat pixelFormat = new PixelFormat();
ContextAttribs contextAtrributes = new ContextAttribs(3, 2);
contextAtrributes.withForwardCompatible(true);
contextAtrributes.withProfileCore(true);
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("Textured quad!");
Display.create(pixelFormat, contextAtrributes);
Mouse.create();
Mouse.setGrabbed(true);
Keyboard.create();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
entity = new Entity("planeTex.obj");
entity2 = new Entity("modelTex2.obj");
Shaders.load();
Textures.load();
Camera.create(new Vector3f(0, 1, -0.75f), new Vector3f(-50, 0, 20), HEIGHT, WIDTH);
while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
entity.draw();
entity2.draw();
Display.update();
Display.sync(60);
}
}
public class Entity
{
private int vao, vbo, ebo;
private int elementSize;
public Entity(String name)
{
vao = glGenVertexArrays();
glBindVertexArray(vao);
vbo = glGenBuffers();
*Load vertex data into buffer*
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
ebo = glGenBuffers();
*load data into elementBuffer*
*Set elementSize to the element count*
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
}
public void draw()
{
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glDrawElements(GL_TRIANGLES, elementSize, GL_UNSIGNED_INT, 0);
}
}
public class Shaders
{
public static int vertexShader, fragmentShader;
public static int shaderProgram;
public static int uniTrans;
public static void load()
{
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, loadFile("vertex.shader"));
glCompileShader(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, loadFile("fragment.shader"));
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
// Specify the layout of the vertex data
int posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, 0);
int colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 3);
int texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 6);
uniTrans = glGetUniformLocation(Shaders.shaderProgram, "model");
}
}
The result is that only the Entity object created last will be drawn. No matter the draw order.
Well, I fixed it by placing this chunk
// Specify the layout of the vertex data
int posAttrib = glGetAttribLocation(Shaders.shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, 0);
int colAttrib = glGetAttribLocation(Shaders.shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 3);
int texAttrib = glGetAttribLocation(Shaders.shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 6);
In the entity class draw loop just before the glDrawElements method. Placing the chunk anywhere else would result in a crashed program:
"Cannot use offsets when Array Buffer Object is disabled"
Otherwise it would simply draw nothing at all. I got the feeling from NicolBolas that I should put it in the Entity constructor, but as I said, it didn't work anywhere.

Drift in rotation about z-axis

In an application, while trying to rotate an object using touch, I noticed drift in position of object after sometime (without any translation applied !!). The rotation is only about z-axis and works perfectly, but drift happens only after few rotations.
ds will be used for translation (using up-down button).
_uNozzleCentreMatrix and _ModelMatrixNozzle will use ds if I correct this.
private static final float[] _uNozzleCentre = new float[]{0.0f, 0.333605f, 0.0f, 1.0f};
protected static float[] _uNozzleCentreMatrix = new float[4];
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
Matrix.setLookAtM(GLES20Renderer._ViewMatrix, 0, 0, 0, 7f, 0, 0, 0, 0, 1, 0);
Matrix.frustumM(GLES20Renderer._ProjectionMatrix, 0, -ratio, ratio, -1, 1, 2, 8);
Matrix.setIdentityM(GLES20Renderer._ModelMatrixNozzle, 0);
}
private static void updateModel(int upDown, float xAngle, float yAngle, float zAngle) {
//ds = GLES20Renderer._upDown - GLES20Renderer._lastUpDown;
ds = 0; // ds changes with button up-down, but now it is made 0, so button up-down will not affect it
Matrix.multiplyMV(GLES20Renderer._uNozzleCentreMatrix, 0, GLES20Renderer._ModelMatrixNozzle, 0, GLES20Renderer._uNozzleCentre, 0);
if(Math.abs(ds) > 0) {
} else {
if(GLES20Renderer._zAngle >= 360) {
GLES20Renderer._zAngle = GLES20Renderer._zAngle - 360;
}
if(GLES20Renderer._zAngle <= -360) {
GLES20Renderer._zAngle = GLES20Renderer._zAngle + 360;
}
Matrix.translateM(GLES20Renderer._ModelMatrixNozzle, 0, GLES20Renderer._uNozzleCentreMatrix[0], GLES20Renderer._uNozzleCentreMatrix[1], 0);
Matrix.rotateM(GLES20Renderer._ModelMatrixNozzle, 0, GLES20Renderer._zAngle, 0, 0, 1);
Matrix.rotateM(GLES20Renderer._ModelMatrixNozzle, 0, -GLES20Renderer._lastZAngle, 0, 0, 1);
Matrix.translateM(GLES20Renderer._ModelMatrixNozzle, 0, -GLES20Renderer._uNozzleCentreMatrix[0], -GLES20Renderer._uNozzleCentreMatrix[1], 0);
}
Matrix.multiplyMM(GLES20Renderer._MVPMatrixNozzle, 0, GLES20Renderer._ViewMatrix, 0, GLES20Renderer._ModelMatrixNozzle, 0);
Matrix.multiplyMM(GLES20Renderer._MVPMatrixNozzle, 0, GLES20Renderer._ProjectionMatrix, 0, GLES20Renderer._MVPMatrixNozzle, 0);
GLES20Renderer._lastZAngle = zAngle;
}
Apk for download:
http://www.pixdip.com/opengles/rotation/rotation.apk
(Try swiping a longer horizontal area from extreme left to right to observe drift early. Please be patient! Drift can take 20 seconds to occur)
For those whom it did not happen, here is the automated apk:
http://www.pixdip.com/opengles/rotation/automatic.apk
and the edited part of code:
Matrix.translateM(GLES20Renderer._ModelMatrixNozzle, 0, GLES20Renderer._uNozzleCentreMatrix[0], GLES20Renderer._uNozzleCentreMatrix[1], 0);
Matrix.rotateM(GLES20Renderer._ModelMatrixNozzle, 0, GLES20Renderer._zAngle, 0, 0, 1);
//Matrix.rotateM(GLES20Renderer._ModelMatrixNozzle, 0, -GLES20Renderer._lastZAngle, 0, 0, 1);
Matrix.translateM(GLES20Renderer._ModelMatrixNozzle, 0, -GLES20Renderer._uNozzleCentreMatrix[0], -GLES20Renderer._uNozzleCentreMatrix[1], 0);
Sometimes floating point errors get accumulated because of matrix stack.
This can be removed by using separate matrices for some critical transformations:
private static float[] _TMatrix = new float[16];
private static float[] _ModelMatrix = new float[16];
Matrix.setIdentity(Renderer._ModelMatrix);
Matrix.setIdentity(Renderer._TMatrix);
Matrix.translate(Renderer._ModelMatrix, xmov,ymov,0);
Matrix.translate(Renderer._TMatrix, -xmov,-ymov,0);
Matrix.multiply(Renderer._ModelMatrix, Renderer._TMatrix, Renderer._ModelMatrix);
// will result in an identity model matrix, without any floating point errors

Categories

Resources