OpenGL ES 1.1 Android Cubemapping - android

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.

Related

diffuse lighting shader in OpenGL ES on android not wroking

i just spend my whole day trying to figur out, how to get diffuse lighting in opengl to run. for some reason, my model aka a simple cube just stays completely black, when i use the diffuse Shader. this is the VertexShader(just ignore the lines until gl_Position, those are for my own projektion algotythm and the only important things in here are the v_Position the v_Normal, which are basicly just the position and the normal after all the transforms like for example rotation):
#version 300 es
in vec3 aPos;
in vec3 normal;
uniform vec4 vm;
uniform vec4 aColor;
out vec3 v_Position;
out vec4 v_Color;
out vec3 v_Normal;
void main()
{
vec3 lightPos = vec3(2.0f, 2.0f, 3.0f);
float x2 = (aPos.x * cos(vm.y) - (aPos.z) * sin(vm.y));
float z2 = ((aPos.x * sin(vm.y) + (aPos.z) * cos(vm.y))) + 7.0f;
float y2 = (aPos.y * cos(vm.z) - (z2 - 7.0f) * sin(vm.z));
float z3 = ((aPos.y * sin(vm.z) + (z2 - 7.0f) * cos(vm.z))) + 7.0f;
float nx2 = (normal.x * cos(vm.y) - (normal.z) * sin(vm.y));
float nz2 = ((normal.x * sin(vm.y) + (normal.z) * cos(vm.y))) + 7.0f;
float ny2 = (normal.y * cos(vm.z) - (nz2 - 7.0f) * sin(vm.z));
float nz3 = ((normal.y * sin(vm.z) + (nz2 - 7.0f) * cos(vm.z))) + 7.0f;
float dst = sqrt((x2 * x2) + (y2 * y2) + (z3 * z3));
gl_Position = vec4(x2 / z3 * vm.x,y2 / z3 * vm.x * vm.w, dst * 0.0001,1.0f);
v_Position = vec3(x2, y2, z3);
v_Color = aColor;
v_Normal = vec3(nx2, ny2, nz3);
}
this is the FragmentShader :
#version 300 es
precision mediump float;
uniform vec3 u_LightPos;
in vec3 v_Position;
in vec4 v_Color;
in vec3 v_Normal;
out vec4 FragColor;
void main()
{
float distance = length(vec3(u_LightPos) - v_Position);
vec3 lightVector = normalize(vec3(u_LightPos) - v_Position);
float diffuse = max(dot(v_Normal, lightVector), 0.1);
diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
FragColor = vec4(vec3(v_Color) * (diffuse), 1.0f);
}
for u_LightPos i passed in 0.0f, 0.0f, 0.0f, 0.0f
for aColor : 1.0f, 0.0f, 0.0f, 1.0f
in case it helps, this is my vertexBuffer(the stride is 6 and the first three floats are for position the next three for the normals) :
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
I have no idea why, but i yeeted out everything which had something to do with u_LightPos and now it works

Android OpenGL ES 2.0 lighting?

I am new to GLES 2.0. It is really making me nervous because OpenGL ES 1 II just set GL_LIGHTNING1 and then the lighting was set. In GLES 2.0 it is not working. In fact the whole screen is black. So please tell me what should I do with the code below. These are the vertices I am setting:
static float quadrateCoords[] = { // in counterclockwise order:
// 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,
// up
-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
};
static float normal[] = {
0.0f, 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,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.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, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.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, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f
};
static float quadrateColors[] = { // in counterclockwise order:
// front
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
};
And the shader code is:
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uMVMatrix;\n" +
"uniform vec3 uLightPos;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aColor;\n" +
"attribute vec4 aNormal;\n" +
"varying vec4 vColor;\n" +
"void main() {\n" +
"vec3 modelViewVertex = vec3(uMVMatrix * a_Position);\n" +
"vec3 modelViewNormal = vec3(uMVMatrix * vec4(aNormal, 0.0));\n" +
"float distance = length(uLightPos - modelViewVertex);\n" +
"vec3 lightVector = normalize(uLightPos - modelViewVertex);\n" +
"float diffuse = max(dot(modelViewNormal,lightVector), 0.1):\n" +
"diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));\n" +
"vColor = aColor * diffuse;\n" +
"gl_Position = aPosition * uMVPMatrix;\n" +
"}\n";
private final String fragmentShaderCode =
"precision mediump float;\n" +
"varying vec4 vColor;\n" +
"void main() {\n" +
"gl_FragColor = vColor;\n" +
"}\n";
And last the draw function:
public void draw(float[] mvpMatrix, float[] mVMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's aPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
nNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
nColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
// Enable a handle to the triangle vertices
// Prepare the triangle coordinate data
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
GLES20.glEnableVertexAttribArray(nNormalHandle);
GLES20.glVertexAttribPointer(nNormalHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer2);
GLES20.glEnableVertexAttribArray(nColorHandle);
GLES20.glVertexAttribPointer(nColorHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer3);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0);
muMVMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVMatrix");
GLES20.glUniformMatrix4fv(muMVMatrixHandle, 1, false, mVMatrix, 0);
muLightPosHandle = GLES20.glGetUniformLocation(mProgram, "uLightPos");
GLES20.glUniform3f(muLightPosHandle, 10.0f, 10.0f, 15.0f);
// Front
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
// Back
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);
// Left
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4);
// Right
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4);
// Top
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4);
// Right
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
Debug like this:
gl_FragColor = vec4(0.5, 0, 0, 0.5) + vColor;
find errors if any, although your shader is correct.
If there are still errors after you have tested for errors (by directly using colors in place of custom values) look at this ADS shader
How to implement shadow in OpenglES 1.x (on iPhone)
And another point to remember, disable culling if you have enabled it
You're attempting a dot product between a projection space vector (newNormal), and a world space vector (lightPos).
To perform dot product both vectors must be in the same coordinate space. Either provide the normals in world space coordinates (modelMatrix * aNormal), or provide the lightPos in projection space (viewProjectionMatrix * lightPos).

OpenGL drawing a Hexigon with vertices

Im trying to make a basic shape ( Hexigon ) to start and learn some basics about OpenGL on the Android platform and i have a little problem.
I have successfully made a pointy Hexagon, but when i convert it into a flat Hexygon it doesnt render correctly. Here is some code, for you guys to figure out the problem ( probably a easy fix.. )
vertices / indices
//flat hexagon
private float verticesFlat[] = {
0.0f, 0.0f, 0.0f, //center
-0.5f, 1.0f, 0.0f, // left top
0.5f, 1.0f, 0.0f, // right top
1.0f, 0.0f, 0.0f, // right
0.5f, 1.0f, 0.0f, // right bottom
-0.5f, -1.0f, 0.0f, // left bottom
-1.0f, 0.0f, 0.0f, // left
};
private short[] indices = { 0, 1, 2, 3, 4, 5, 6, 1 };
standard vertex / index Buffers..
// a float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer vbb = ByteBuffer.allocateDirect(verticesFlat.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(verticesFlat);
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);
The onDraw(GL10 gl):
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
The result im getting is illustrated in the image below:
Should be
private float verticesFlat[] = {
0.0f, 0.0f, 0.0f, //center
-0.5f, 1.0f, 0.0f, // left top
0.5f, 1.0f, 0.0f, // right top
1.0f, 0.0f, 0.0f, // right
0.5f, -1.0f, 0.0f, // right bottom (notice sign)
-0.5f, -1.0f, 0.0f, // left bottom
-1.0f, 0.0f, 0.0f, // left
};

Need help understanding glDrawElements for GLES 2.0

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

OpenGL ES (Android) Cube disabling lighting when glEnable(GL_COLOR_MATERIAL) is added?

This is probably a really simple question, and I'm just not getting it. I'd like to have lighting on a cube, but have some faces have different colors. The following code results in a cube with no lighting, but one black face:
private float[] lightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };
private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
private float[] lightPosition = { 0.0f, 0.0f, 3.0f, 1.0f};
float matAmbient[] = new float[] { 0.3f, 0.3f, 0.3f, 1.0f };
float matDiffuse[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
private FloatBuffer vertexBuffer;
private FloatBuffer normalBuffer;
private FloatBuffer colorBuffer;
float vertices[] = 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,
};
float colors[] = new float[] {
// FRONT
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
// BACK
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,
// LEFT
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,
// RIGHT
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,
// TOP
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,
// BOTTOM
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,
};
float normals[] = new float[] {
// FRONT
0f, 0f, 1f,
0f, 0f, 1f,
0f, 0f, 1f,
0f, 0f, 1f,
// BACK
0f, 0f, -1f,
0f, 0f, -1f,
0f, 0f, -1f,
0f, 0f, -1f,
// LEFT
-1f, 0f, 0f,
-1f, 0f, 0f,
-1f, 0f, 0f,
-1f, 0f, 0f,
// RIGHT
1f, 0f, 0f,
1f, 0f, 0f,
1f, 0f, 0f,
1f, 0f, 0f,
// TOP
0f, 1f, 0f,
0f, 1f, 0f,
0f, 1f, 0f,
0f, 1f, 0f,
// BOTTOM
0f, -1f, 0f,
0f, -1f, 0f,
0f, -1f, 0f,
0f, -1f, 0f
};
...
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepthf(1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
}
public GLScreen(Context context) {
super(context);
this.setRenderer(this);
this.requestFocus();
vertexBuffer = makeFloatBuffer(vertices);
normalBuffer = makeFloatBuffer(normals);
colorBuffer = makeFloatBuffer(colors);
}
float xrot = 0.0f;
float yrot = 0.0f;
#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();
GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0);
gl.glRotatef(xrot, 1, 0, 0);
gl.glRotatef(yrot, 0, 1, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
xrot += 1.0f;
yrot += 0.5f;
}
Uncommenting glEnable(GL10.GL_COLOR_MATERIAL) results in a lighted cube with no black face. How would I get a lighted cube with one black face?
I think here you'll find the answer. Both Android Emulator and HTC phones (I don't know about others, I haven't tested apps on them) have bugs with lighting.

Categories

Resources