I've been following the tutorials over at http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html and I've encountered a problem.
I've managed to get both of the following examples to work:
2.7 Example 5: 3D Shapes - Rotating Color Cube and Pyramid (Nehe Lesson 5: 3D Shapes)
Example 2: Cube2.java
2.8 Example 6: Texture (Nehe Lesson 6: Texture)
But when I try to draw both a coloured cube and a textured cube I get the following:
http://i.imgur.com/Smbsa.png
(First part of the image)
The coloured cube is invisible but clips the texture cube and the texture cube's textures are coloured by the last colour (yellow) of the coloured cube.
I basically just draw both of the cubes with:
// ----- Render the Color Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(0.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
cube.draw(gl); // Draw the cube (NEW)
// Update the rotational angle after each refresh (NEW)
angleCube += speedCube; // (NEW)
// ----- Render the Texture Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(-1.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
texturecube.draw(gl); // Draw the cube (NEW)
I tried adding a third so called "Photo cube" from the part "2.9 Example 6a: Photo-Cube".
(Can only post 2 links check the imgur link above)
I added:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
to the texture cube and noticed that the blue colour "spills" over to the photo cube.
When I add:
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
the following textures are no longer coloured but this feels like a crude way to remove past colours..
When I remove:
texturecube.loadTexture(gl, context); // Load image into Texture (NEW)
photocube.loadTexture(gl); // Load image into Texture (NEW)
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable texture (NEW)
this happens:
(Can only post 2 links check the imgur link above)
The colour cube is back and all other surfaces are coloured.
My questions:
What am I doing wrong when the coloured cube disappears? (I guess that it should be possible to both use textures and colours at the same time)
Is there a better way to "clear" the colour from previous cubes/objects other than "gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);"?
Your question is very detailed which is good, though it would help if you posted the full code sample of your drawing section. It's hard for me to guess what you might be doing that would cause the problem.
Anyway I'll try your two questions:
First: My guess is that you don't disable texturing before drawing your colored cube. But you don't have the full code so I can't say with certainty. Call glDisable(GL_TEXTURE_2D) before rendering your second cube, and reenable it before drawing the next textured cube.
Second: No, that's the correct way to do it (Though you probably want 1,1,1,1, not 1,1,1,0 in case you ever want to do anything with transparency). Alternatively there's a way to do it via pushing the color state onto an OpenGL stack and popping it later, though that's deprecated and shouldn't be used.
Related
i am following nehe's tutorials.
i intent to make a menu or at least buttons with opengl, yet object overlap on the menu
my code on the drawFrame function in the renderer
gl.glLoadIdentity();
gl.glScalef(0.05f, 0.05f, 0.05f);
gl.glTranslatef(0.0f, 0.0f, z-zKonum);
gl.glRotatef(xAcisi, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yAcisi, 0.0f, 1.0f, 0.0f);
dokukup.ciz(gl);
gl.glLoadIdentity();
gl.glTranslatef(3.6f, -1.5f, z);
tusYukari.ciz(gl);
gl.glLoadIdentity();
gl.glTranslatef(2.5f, -1.5f, z);
tusAsagi.ciz(gl);
how do i get my menu buttons dominant(always on the top) on the overlapping?
You can get the buttons to appear always on top by drawing the buttons last and disabling depth testing when drawing the buttons. Then make sure to enable depth testing again before drawing the next frame so that your 3D geometry renders properly.
In your drawFrame function you would do the following steps:
Enable depth testing
Draw the main scene geometry
Disable depth testing
Draw the buttons
I'm trying to make a hexagon with 6 triangles using rotation and translation. Rather than making multiple translate calls, I instead want to translate the triangle downward once and rotate around the Z axis at 60 degrees six times (my sketch may help with that explanation: http://i.imgur.com/SrrXcA3.jpg). After repeating the drawTriangle() and rotate() methods six times, I should have a hexagon.
Currently my code looks like this:
public void onDrawFrame(GL10 unused)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); //start by clearing the screen for each frame
GLES20.glUseProgram(mPerVertexProgramHandle); //tell OpenGL to use the shader program we've compiled
//Get pointers to the program's variables. Instance variables so we can break apart code
mMVPMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "uMVPMatrix");
mPositionHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "aPosition");
mColorHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "aColor");
//Prepare the model matrix!
Matrix.setIdentityM(mModelMatrix, 0); //start modelMatrix as identity (no transformations)
Matrix.translateM(mModelMatrix, 0, 0.0f, -0.577350269f, 0.0f); //shift the triangle down the y axis by -0.577350269f so that its top point is at 0,0,0
drawTriangle(mModelMatrix); //draw the triangle with the given model matrix
Matrix.rotateM(mModelMatrix, 0, 60f, 0.0f, 0.0f, 1.0f);
drawTriangle(mModelMatrix);
}
Here's my problem: it appears my triangle isn't rotating around (0,0,0), but instead it rotates around the triangle's center (as shown in this picture: http://i.imgur.com/oiLFSCE.png).
Is it possible for to rotate triangle around (0,0,0), where its vertex is located?
Are you really be sure that your constant -0.577350269f is the correct value for the triangle center?
Also your code looks unfinish (You use an mvp handle but never use it in the code), could you provide more information?
I have some questions about using the openGL
I am newbie in using the openGL and I try to set the RGB of the square and it works but it does not work after i use the blend function. And the square finally disappear when it redraw.
The following is the part of the code
#Override
public void onDrawFrame(GL10 gl) {
// clear Screen and Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
// Drawing
gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glEnable(GLES20.GL_BLEND); // is the same as moving the camera 5 units away
square.draw(gl); // Draw the triangle
//square.loadGLTexture(gl, this.context);
gl.glColor4f(red, green, blue, 0f);
red = red - 0.01f;
blue = blue - 0.01f;
green = green - 0.01f;
GLES20.glDisable(GLES20.GL_BLEND);
}
The first time onDrawFrame is called, both color and depth buffers are cleared, then a triangle is drawn and, after that, color is changed with a 0 alpha value.
Next time onDrawFrame gets called, the triangle is drawn once again but this time using the alpha value 0. Being both color and depth buffers cleared as they have been, all you probably see is an empty screen because 0 alpha, using that blend function, can be translated to "completely transparent".
actually I'm drawing a cube, I'm checking rotation problems of the cube, but for this I need to draw a point on the 0,0,-1 opengl coordinate of the screen, I'm using perspective projection, MyGLSurfaceView and android 1.5 opengl es 1.x
How can I draw a black or white point on the 0,0,-1 opengl coordinate of the screen?
If you want to be able to draw directly in window space then the easiest thing would be to load modelview and projection temporarily with the identity matrix and draw a GL_POINT with the location that you need. So that'd be something like:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// draw the point here; specifics depending on whether you
// favour VBOs, VBAs, etc
// e.g. (assuming you don't have any client state enabled
// on entry and don't care about leaving the vertex array
// enabled on exit)
GLfloat vertexLocation[] = {0.0f, 0.0f, -1.0f};
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertexLocation);
glDrawArrays(GL_POINTS, 0, 1);
// end of example to plot a GL_POINT
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// and possibly restore yourself to some other matrix mode
// if, atypically, the rest of your code doesn't assume modelview
i have written code for draw a triangle and rectangle.
// Reset the Modelview Matrix
gl.glLoadIdentity();
gl.glTranslatef(0.0f, -1.2f, -6.0f);
tt.draw(gl); //rectangle class draw method
gl.glTranslatef(0.0f, 2.5f, 0.0f);
tr.draw(gl); //triangle class draw method
i have load a texture on rectangle
// Load the texture for the rectangle in onSurfaceCreated()
tt.loadGLTexture(gl, this.context);
my problem is the texture mapping on triangle shape also
the screen shot is available in following link.
http://www.freeimagehosting.net/uploads/82fe919770.png
how can i resolve this problem?
It seems like this would be a fairly simple problem to fix, and I don't believe it's Android specific... Are you defining the UV Texture Coordinates for your object? If you don't, OpenGL will not display the texture properly. You can use either the glTexCoord3f function in immediate mode, or glTexCoordPointer if you're using C arrays.