OpenGL Lighting with Display Lists - java

I'm developing some OpenGL game using JOGL library.
When I draw objects regulary, e.g. using GL_QUADS directly in the display method, the scene lighting appears on the object.
However, when I prepare objects in the init method and load using glCallList in the display list, those objects doesn't seem to be affected from the lighting.
To be precise, they are affected in SOME way: altering the materialfv function parameters or the ambient parameters brighten or darken the scene, but the color I'm trying to diffuse is not working (only on the regular drawn objects without lists).
Code
Here's the lighting code:
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL2.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL2.GL_LEQUAL); // The Type Of Depth Testing To Do
glu = new GLU();
// Really Nice Perspective Calculations
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
// Light
float ambient[] = {0.1f,0.1f,0.1f,1.0f};
float diffuse0[] = {1f,0f,0f,1.0f};
float diffuse1[] = {0f,0f,1f,1.0f};
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambient, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse0, 0);
gl.glEnable(GL2.GL_LIGHT0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, ambient, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, diffuse1, 0);
gl.glEnable(GL2.GL_LIGHT1);
gl.glEnable(GL2.GL_LIGHTING);
// make display lists here...
The lighting part of display:
// display method...
// apply light
float position0[] = {500, 300, 3500,1.0f};
float position1[] = {500, 300, 500,1.0f};
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position0, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, position1, 0);
// draw objects directly
// draw objects using glCallLists
Here's an example of how I draw regular object in the display method:
gl.glPushMatrix();
gl.glTranslatef(1000, 500, 2000);
gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT );
gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT );
float material[] = {0.8f,0.8f,0.8f,1.0f};
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE, material, 0);
gl.glBegin(GL2.GL_QUADS);
// Front Face
gl.glNormal3f(0,0,1);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(2f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(2f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
gl.glNormal3f(0,0,-1);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
// Top Face
gl.glNormal3f(0,1,0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom Face
gl.glNormal3f(0,-1,0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
gl.glNormal3f(1,0,0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
// Left Face
gl.glNormal3f(-1,0,0);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glEnd();
gl.glPopMatrix();
Here's the class I use to load obj files.
I use the method loadWavefrontObjectAsDisplayList which accepts a path to obj file and then returns the list integer.
Another Edit:
The scene (lamp is being lighten up, crate is not):
Link to lamp and crate .obj files.

The normals are probably missing in the WaveFront OBJ file of the crate model.
Then, the OBJ importer that you use generates plain wrong normals whose coordinates are (0, 0, 0), it has no chance to work with the lightning.

Related

Circle has edges instead of being smooth using libgdx

Any ideas why this is not producing a smooth circle?
public void draw(ShapeRenderer sRenderer) {
sRenderer.begin(ShapeType.Filled);
sRenderer.setColor(1.0f, 0.0f, 0.0f, 0.0f);
sRenderer.identity();
sRenderer.translate(1.0f, 1.0f, 0);
sRenderer.rotate(0.0f, 0.0f, 1.0f, (float) Math.toDegrees(getBody().getAngle()));
sRenderer.circle(0.0f, 0.0f, 1.0f);
sRenderer.end();
}
circle() takes another argument for setting the number of segments manually. You have it set to estimate and since it's thinking in pixels and not world units it assumed a 1 pixel radius circle.
public void draw(ShapeRenderer sRenderer) {
sRenderer.begin(ShapeType.Filled);
sRenderer.setColor(1.0f, 0.0f, 0.0f, 0.0f);
sRenderer.identity();
sRenderer.translate(1.0f, 1.0f, 0);
sRenderer.rotate(0.0f, 0.0f, 1.0f, (float) Math.toDegrees(getBody().getAngle()));
sRenderer.circle(0.0f, 0.0f, 1.0f, 100);
sRenderer.end();
}
That should get you somewhere, 100 is just a number I threw in, tune it to your needs.

Drawing 3D polygon using vertex array

I have a short program which draw a 3D GL.GL_QUADS , here its display() method -
public void display(GLAutoDrawable drawable) {
....
gl.glBegin(GL.GL_QUADS); // of the color cube
// Top-face
gl.glColor3f(0.0f, 1.0f, 0.0f); // green
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom-face
gl.glColor3f(1.0f, 0.5f, 0.0f); // orange
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
// Front-face
gl.glColor3f(1.0f, 0.0f, 0.0f); // red
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
// Back-face
gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(1.0f, 1.0f, -1.0f);
// Left-face
gl.glColor3f(0.0f, 0.0f, 1.0f); // blue
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right-face
gl.glColor3f(1.0f, 0.0f, 1.0f); // violet
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
....
}
Now I want to change the above drawing to "Vertex Arrays" mode and to get same results , so I did the following :
Created a float array of all the vertices coodinates of the GL.GL_QUADS .
put this array to a Buffer .
Tell OpenGL where the vertices are (8 vertices) .
Tell OpenGL which indices to draw .
Here is the main code I wrote (edited) -
public class DisplayWithArray extends GLCanvas implements GLEventListener,
KeyListener {
private float[] cubeVertices = { 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
1.0f, -1.0f,
-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f };
private float[] colorVertices ={ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f,
1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
// 1st edit - 24 positions of the GL.GL_QUADS
private int[] indices = new int[24] ;
private IntBuffer indicesBuf ;
}
public DisplayWithArray(GLCapabilities capabilities, int width, int height) {
for (int i=0 ; i<24 ; i++) {
this.indices[i] = i ;
}
public void init(GLAutoDrawable drawable) {
...
final GL gl = drawable.getGL();
...
setupPointer(gl);
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
// draw
gl.glDrawArrays(GL.GL_QUADS, 0, 24);
}
public void setupPointer(GL gl) {
FloatBuffer tmpVerticesBuf = BufferUtil
.newFloatBuffer(cubeVertices.length);
;
FloatBuffer tmpColorVerticesBuf = BufferUtil
.newFloatBuffer(colorVertices.length);
for (int i = 0; i < cubeVertices.length; i++) {
tmpVerticesBuf.put(cubeVertices[i]);
}
for (int i = 0; i < colorVertices.length; i++) {
tmpColorVerticesBuf.put(colorVertices[i]);
}
tmpVerticesBuf.rewind();
tmpColorVerticesBuf.rewind();
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, tmpVerticesBuf);
gl.glColorPointer(3, GL.GL_FLOAT, 0, tmpColorVerticesBuf);
// Indices of polygon
IntBuffer tmpIndicesBuf = BufferUtil.newIntBuffer(this.indices.length);
for (int i = 0; i < indices.length; i++) {
tmpIndicesBuf.put(indices[i]);
}
tmpIndicesBuf.rewind();
indicesBuf = tmpIndicesBuf ;
}
When I run it I get an exception Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: glGetError() returned the following error codes after a call to glDrawElements(): GL_INVALID_ENUM at DisplayWithArray.display(DisplayWithArray.java:152)
point to the line - gl.glDrawElements(GL.GL_QUADS, indices.length, GL.GL_INT, indicesBuf);
What is wrong here ?
Edit:
I changed the indices array to range [0,5] - according to 6 face of the GL.GL_QUADS.
And extend tmpColorVerticesBuf array to 72 indices (4 times each color) .
Still I have same exception as mentioned above .
Edit 2: (now it works well)
Solve by comparing between cubeVertices and colorVertices sizes (72 each array) , and in display() use gl.glDrawArrays(GL.GL_QUADS, 0, 24) (24 elements of size 3)
What's wrong is that your arrays are not the same size. Your vertex position array has 24 vec3's in it, while your color array only has six. So when your index array says "6", it's going to try to access the 7'th entry in an array with 6 elements. Hence the crash.
Every array uses the same index. Therefore, if you want to have colors and positions, then each color must have a single corresponding position. And vice-versa. You can't have 6 colors working with 32 positions. Nor can each attribute use different indices. If you're doing indexed rendering, they all must use the same index.

Questions on OpenGL VBOs

I'm currently trying to make a basic game with cubey terrain, similar to Minecraft classic. My first question:
How do I store a list of cubes in a VBO and then rebuild the list when I change that data? I currently render cubes like this:
vertices = BufferUtils.createFloatBuffer(3 * 4 * 6);
vertices.put(new float[] {
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f});
vertices.rewind();
vertexBufferID = createVBOID();
bufferData(vertexBufferID, vertices);
public static int createVBOID() {
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
IntBuffer buffer = BufferUtils.createIntBuffer(1);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer.get(0);
}
return 0;
}
public static void bufferData(int id, FloatBuffer buffer) {
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_DYNAMIC_DRAW_ARB);
}
public void renderGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glLoadIdentity();
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.f, 1.0f, 0.0f);
glTranslated(-xpos, -ypos - 19, -zpos - 5);
glEnableClientState(GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
glPushMatrix();
glTranslatef(x, y, z);
glDrawArrays(GL_QUADS, 0, 24);
glPopMatrix();
}
}
}
glDisableClientState(GL_VERTEX_ARRAY);
}
I know I need to use glMapBuffer and glUnMapBuffer to modify the VBO, but how do I store the cubes and remove them when needed? Should I use an ArrayList? Also, I've been told my way of rendering the cubes is wrong because everyone says I'm rendering one cube only with new coordinates. They say my vertex data stays the same, what do they mean by that?
So basically my questions are,
1. How do I use a Array to hold a list of cubes and load them into a VBO and then remove some of the cubes?
2. How can I change my vertex data so I'm rendering different cubes, not just one cube over and over?
as antonijn wrote you can have 1 cube as VBO and translating its modelview matrix
the translation is sometimes slower (when there are too much cubes).
the translation can be done even inside shader by some uniform offset which is little bit faster than matrix translations
faster way is to have all of the cubes in VBO
it wastes memory but cubes are not that much big (6 faces only)
I would have the cubes stored permanently in CPU accessible memory
when scene changes copy it to VBO for rendering.
do not transfering data between CPU<->GPU back and forward
only copy data from CPU to GPU.
also is better to use non indexed VBOs (drawarrays not drawelements)
indexing is not very good supported
often slows thing down even crashes on bad drivers
for the code look here: https://stackoverflow.com/a/18672821/2521214
but that post use VAO+VBO for GLSL purposes so if you not using GLSL then some changes are needed.
Mostly you have a CPU representation of your scene (which is in your case a list of cubes) and a GPU representation (which is the vertex buffers etc.)
In your case a cube is defined by its position only. They all have the same size and so on. Here are some options how you can render several cubes:
Define a uniform position variable in your vertex shader and add this to the vertex positions. If you don't use shaders, specify the offset as a translation matrix in the model view matrix. Render the same vertex buffer several time with different matrices / offsets.
Let the GPU create the cube in its geometry shader. Therefore, you would just specify the cubes' positions in the vertex buffer. When you map a vertex buffer, you will get a pointer to the data where you can copy the new data to.
Use instancing. Provide an array of positions to the shader and render the cube in instanced mode. Use the instance id to determine the offset vector.

OpenGL es 2.0 Android Texture

I cannot seem to render a Texture to my square. I have gotten my program to render the blank square with color. Any help is greatly appreciated.
I've redesigned my code to the following and believe that the problem exists with how I'm setting up my Vertex Coordinates and Texture Coordinates. I also get a libc Fatal signal 11 at my glDrawArrays function.
Here are the Vertex and Texture Coordinates:
private final FloatBuffer vertexBuffer;
private final FloatBuffer textureBuffer;
static final int COORDS_PER_VERTEX = 3;
static float positionCoords[] = { // in counterclockwise order:
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
};
static final int COORDS_PER_TEXTURE = 2;
static float textureCoords[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
};
Here's my draw function in my square class:
public void draw(float[] mvpMatrix)
{
int MVPMatrixHandle = GLES20.glGetUniformLocation(shader.getProgram(), "u_MVPMatrix");
int textureHandler = GLES20.glGetUniformLocation(shader.getProgram(), "u_s_texture");
int positionHandler = GLES20.glGetAttribLocation(shader.getProgram(), "a_position");
int texCoordHandler = GLES20.glGetAttribLocation(shader.getProgram(), "a_texCoord");
Log.d(TAG, "Setting up GLProgram Handlers");
GlRenderer.checkGlError("Setup GLProgram Handlers");
GLES20.glEnableVertexAttribArray(positionHandler);
GLES20.glEnableVertexAttribArray(texCoordHandler);
GlRenderer.checkGlError("EnableVertexAttribArrays");
GLES20.glVertexAttribPointer(positionHandler, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
GLES20.glVertexAttribPointer(textureHandler, COORDS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
textureStride, textureBuffer);
GlRenderer.checkGlError("VertexAttribPointers (Position, Texture)");
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
GLES20.glUniform1i(textureHandler, 0);
GlRenderer.checkGlError("Binding Texture");
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
GlRenderer.checkGlError("Draw Arrays");
GLES20.glDisableVertexAttribArray(positionHandler);
GLES20.glDisableVertexAttribArray(texCoordHandler);
GlRenderer.checkGlError("DisableVertexAttribArrays");
}
Your SetupGLPositionHandle function looks wrong to me. Why disable the PositionHandle attribute at the end of the function?
The attribute must be enabled at the time glDrawArrays is called.
My main problem in the above code was the fact that I was using the textureHandleinstead of the texCoordHandler in the glVertexAttribPointer //Texture
The code should look like this:
GLES20.glVertexAttribPointer(texCoordHandler, COORDS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
textureStride, textureBuffer);
Since this problem arose I've rewritten my code yet again. Adding indices (draworder), combining my Vertex and Texture arrays into a Vertices array, and just reference a vertexBuffer //Contains both position coordinates and texture coordinates instead of a textureBuffer and a vertexBuffer //Only contains position coordinates
I've also changed the GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); to:
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
GLES20.GL_UNSIGNED_SHORT, indexBuffer);

OpenGL function refusing to render triangle

This has probably something to do with my transformations, but right now I can't figure this out and this is driving me instane. I have wrapped the draw code so that I can easily define new triangles. However, when I put this into a function, it just shows a grey screen. Te function code is as follows:
public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle, int mColorHandle, int mMVPMatrixHandle)
{
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
//aBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
The code which IS working is:
public void onDrawFrame(GL10 glUnused)
{
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
// Do a complete rotation every 10 seconds.
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
// Draw the triangle facing straight on.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle1Vertices);
// Draw one translated a bit down and rotated to be flat on the ground.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle2Vertices);
// Draw one translated a bit to the right and rotated to be facing to the left.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle3Vertices);
*/
/*
for (int x = 0; x < staticHolder.objectList.size(); x++)
{
staticHolder.objectList.get(x).Draw(mViewMatrix, mModelMatrix, mProjectionMatrix, mPositionHandle, mColorHandle, mMVPMatrixHandle);
}
*/
}
/**
* Draws a triangle from the given vertex data.
*
* #param aTriangleBuffer The buffer containing the vertex data.
*/
private void drawTriangle(final FloatBuffer aTriangleBuffer)
{
// Pass in the position information
aTriangleBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aTriangleBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
I am passing in the same variables and the final variables used here are initialized the same. There is some other work that happens in the function for encapsulation. Any idea why it is refusing to render in the function?
The following code loads the objects in the list:
final float[] triangle1VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f};
final float[] triangle2VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
1.0f, 0.0f, 1.0f, 1.0f};
// This triangle is white, gray, and black.
final float[] triangle3VerticesData = {
// X, Y, Z,
// R, G, B, A
-0.5f, -0.25f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.25f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
staticHolder.objectList.add(new Triangle(triangle1VerticesData));
staticHolder.objectList.add(new Triangle(triangle2VerticesData));
staticHolder.objectList.add(new Triangle(triangle3VerticesData));
The receiving class is:
public class Triangle extends shape
{
public Triangle(float[] data)
{
verts = data;
}
}
After the following bit of code:
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
You must put the vertices into the buffer (otherwise, it's blank!):
aBuffer.put(verts);
The reason this isn't in the bit of code that works, is because those three sets of vertices' buffers are pre-allocated, and the vertices are put into it then (at initialization). They are simply passed to the method each time, so they don't have to be put() in again.
On that note, you will want to avoid allocations in your Draw method, as it's called many times per frame and could lead to slow rendering. Allocate aBuffer once, and put new vertices into it each time.

Categories