square in opengl fills whole display - android

assuming that some opengl-veterans might facepalm now :-)
I am working on a tilebased game (2D only) on android with open gl es.
After days of introducing myself into the concepts of opengl basics, I still don't know how to keep the relation between object and screen size.
To be concrete: My mobile display has 480x800 pixels. When I specify the simple square with its following dimensions, it fills the whole screen (?):
object-vertices:
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
};
short[] indices = { 0, 1, 2, 2, 3, 0 };
onSurfaceCreated:
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
GLU.gluOrtho2D(gl, 0, Shared.dm.widthPixels, Shared.dm.heightPixels, 0);
onDrawFrame:
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Replace the current matrix with the identity matrix
gl.glLoadIdentity();
//Move this only
gl.glPushMatrix();
Log.d(Shared.LOGTAG, "X: " + offset.x + "Y: " + offset.y);
gl.glTranslatef(offset.x, offset.y, 0);
root.draw(gl);
gl.glPopMatrix();
}
Can anyone give me a hint into the right direction? I assume it has something to do with the matrices?

It looks like this is happening because your projection matrix is getting reset. I see in onSurfaceCreated, you call gluOrtho2D. That's fine, but it doesn't look like you're switching back with glMatrixModel(GL_MODELVIEW). So when glLoadIdentity gets called in onDrawFrame, it will reset your projection matrix.
When the project matrix is identity, coordinates just get passed through. -1 and +1 on the X axis correspond to the left and right sides of the window (respectively). On the Y axis, they are the bottom and top of the window. On the Z axis they are the far and near clipping planes.

Typical, misconception done by OpenGL newbies: They think there is some kind of "one time projection initialization".
You normally setup the whole requires OpenGL state anew everytime you start drawing things. This also catches mishaps like accidently overwriting projection matrices.
onSurfaceCreated:
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
// Just upload textures and shaders here.
onDrawFrame:
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// set everything just right before you need it
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, Shared.dm.widthPixels, Shared.dm.heightPixels, 0);
// order in which different matrices are set is not important,
// but order in which each matrix is manipulated is!
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// enable and configure blending on a as-needed base
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
//Move this only
// make sure this is operating really on the modelview matrix
// redundant in this rather simple example, but in large codebases
// inevitable.
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
Log.d(Shared.LOGTAG, "X: " + offset.x + "Y: " + offset.y);
gl.glTranslatef(offset.x, offset.y, 0);
root.draw(gl);
gl.glPopMatrix();
}

Related

OpenGL ES 1.0 GLU Ortho Projection not rendering basic triangle

I am attempting a 2d layer ontop of my 3d world using android's OpenGL 1.0 ES.
I have a triangle that renders fine when I use 3d perspective but is not rendering when I try to do it with a 2d ortho projection. The below code renders the triangle correctly.
public void prepareLayerPerspective(GL10 gl)
{
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl,45, (float)(1080/1920f),0.1f, 700f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
However, when I attempt to set it up as a Orth projection like this
public void prepareLayerPerspective(GL10 gl)
{
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl,0f,500f,0f,700f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
there are no triangles.
Here is the code used to actually draw the triangles: (but remember, the triangles do render correctly with the perspective frustrum set)
public void drawFrame(GL10 gl)
{
super.drawFrame(gl);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT,0,triangleVB);
gl.glDrawArrays(GL10.GL_TRIANGLES,0,3);
}
Here is the constructor to the Triangle class. (scale = 1.0f)
public Triangle(float scale)
{
float triangleCoords[] = {
-1.0f*scale, -1.0f*scale, 0,
0.0f, 1.0f*scale, 0,
1.0f*scale, -1.0f*scale, 0
};
ByteBuffer vbb = ByteBuffer.allocateDirect(triangleCoords.length*4);
vbb.order(ByteOrder.nativeOrder());
triangleVB = vbb.asFloatBuffer();
triangleVB.put(triangleCoords);
triangleVB.position(0);
rotation = 0.0f;
}
As I pointed out on the comment, the specified frustum was so big.
If there were a vertex at (700,0,0) with the frustum you specified, the vertex will be located on left bottom of the display.

Creating a 3D road in Android OpenGL ES

I'm trying to create a simple game in android. By road I meant games like Temple Run or Subway Surf but much simpler and abstract so I could do it only with the OpenGL ES without any other libraries.
So I've read a lot of basic tutorials that explains the 3D construction logic and used the basic sample of creating a 3D cube that rotates.
I am now trying to use that sample to create the game road. I made the square to look more like a rectangle and duplicate it to a 30x5 square road. I've tried many combinations and the internet to find a solution and yet I have this problems\questions:
How do I set all 30x5 squares to be one next to another? I'm always
getting the squares with some unwanted gap
I want to set the vieweye point (the "camera") 45 degrees to the
middle of the first row, so the player could see the road upon him
Next, I would want to move along the road. So Iv'e seen the rotate
and how it works. Is there a way to do the same to the viewpoint or
do I need to change the squares drawing Z's?
I see that onDrawFrame() is calling over and over many times. To
control the FPS, I've seen on the internet that people have used
there own FPS calculation with a sleep(). Isn't there a built one
already?
GLRenderer code:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.Log;
class GLRenderer implements GLSurfaceView.Renderer {
private static final String TAG = "GLRenderer" ;
private final Context context;
private float mCubeRotation = 70.0f;
private Triangle triangle;
private Cube[][] cube;
GLRenderer(Context context) {
this.context = context;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
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 onSurfaceChanged(GL10 gl, int width, int height) {
Log.d("MyOpenGLRenderer", "Surface changed. Width=" + width
+ " Height=" + height);
System.out.println("arg");
//get map
cube = new Cube[30][5];
for(int i = 0; i < cube.length; i++)
for(int j = 0; j < cube[i].length; j++)
cube[i][j] = new Cube();
//draw triangle
triangle = new Triangle(0.5f, 1, 0, 0);
// Define the view frustum
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio = (float) width / height;
GLU.gluPerspective(gl, 45.0f, ratio, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onDrawFrame(GL10 gl) {
// Clear the screen to black
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//translate(dx, dy, dz)
// Position model so we can see it
//gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10.0f);
gl.glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f);
gl.glTranslatef(0.0f, 0.0f, -10.0f);
cube[0][0].draw(gl);
gl.glTranslatef(0.0f, 0.0f, -10.0f);
cube[0][1].draw(gl);
gl.glTranslatef(0.0f, 0.0f, -10.0f);
cube[0][2].draw(gl);
gl.glLoadIdentity();
//set rotation
mCubeRotation -= 0.15f;
System.out.println("mCubeRotation: "+mCubeRotation);
}
}
Cube code:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
class Cube {
private FloatBuffer mVertexBuffer; //vertex
private FloatBuffer mColorBuffer; //color
private ByteBuffer mIndexBuffer; //face indices
float width = 1.0f;
float height = 0.5f;
float depth = 1.0f;
private float vertices[] = {
-width, -height, -depth, // 0
width, -height, -depth, // 1
width, height, -depth, // 2
-width, height, -depth, // 3
-width, -height, depth, // 4
width, -height, depth, // 5
width, height, depth, // 6
-width, height, depth, // 7
};
private float colors[] = {
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.5f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f
};
private byte indices[] = {
0, 4, 5,
0, 5, 1,
1, 5, 6,
1, 6, 2,
2, 6, 7,
2, 7, 3,
3, 7, 4,
3, 4, 0,
4, 7, 6,
4, 6, 5,
3, 0, 1,
3, 1, 2
};
public Cube() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
Eventually I'll draw the square array using glDrawArrays() or glDrawElements() but for now I've used only 3 objects.
There's a lot of questions here. I can't cover everything in detail, but hopefully I can give you some pointers to steer you in the right direction.
To draw 150 squares, you have a number of options:
Create a vertex buffer with a single square, and draw it 150 times, with translations applied. This is probably the easiest to get you off the ground, so I would recommend getting it working first. It's a reasonable approach if all your squares look the same.
Create 150 vertex buffers, with different coordinates. I wouldn't recommend it because it's the least efficient, and doesn't have any benefits over other approaches.
Store the vertices for all 150 squares in a single vertex buffer. This will be the most efficient of the first 3 options, but only works well as long as the relative orientation of the squares remains the same. You may want to try this once you have the basics working.
Use instanced rendering. This is a more advanced feature, and only available in ES 3.0. Just mentioning it for future reference.
What you attempted is sort of a hybrid between option 1 and 2. If you want to go for option 1, you only need one instance of your Cube class. If you look at what you did, this makes sense. You created 150 objects that are all exactly the same, which is not very useful.
Now, on your questions:
To draw the squares without gaps between them, the amount of your translations needs to be the same as the size of each square. Your squares are 2 units wide, but you translate each one by 10 units. You also translate them in the z-direction, which I don't quite understand.
If you want to stick with the kind of functionality you have been using, check out GLU.gluLookAt(). It allows you to place your camera where you want it, and point it in any direction.
Same as 2. Call GLU.gluLookAt() every time you want to move the viewpoint.
Android caps the frame rate at 60 frames per second. That's normally what you should be shooting for anyway, IMHO. If you want to limit it to 30 fps later to save power, I think you can cross that bridge when you get there. Based on what I researched recently, there's no clean and portable way to do this on Android. The proposed solutions I have seen all look kind of hacky to me.
A couple more things on your code:
Your color definitions look odd. You specify colors in 4 components, and the size of the array is correct for that. But you write the array with 3 values per line, which makes it look like you want 3 component colors. Either one can be done, but you need to make sure that you're consistent. 3 components are enough, unless you need transparency.
You are using ES 1.0. That's valid, and might be easier to get started with. But you should be aware that many of its features are considered obsolete, and using ES 2.0 would let you learn more modern and current OpenGL features. The initial hurdle will be higher, so there's definitely a tradeoff here.

GLSurfaceView: position of camera, looking at Surface, changes with android version?

Here is my problem:
I have a GLSurfaceView with Renderer and stuff. Everything works just as I wanted, on older Android versions. But on newer versions (I guess > 4.X) it just shows a black screen without any Bitmaps. For example if I use gl.glClearColor(0.1f, 0.2f, 0.3f, 0.5f); in my onSurfaceCreated method, it changed from black to the color. So I think the problem must be the camera looking in the wrong direction or something, because the background-color is drawn.
Since I am pretty new to OpenGL, I wanted to ask if there are any connections between Android versions and the OpenGL-camera or something like that?
Many people say my Bitmap-Sizes have to be powers of 2, but it doesnt solve anything.
Here is my Renderer:
public class GlRenderer implements Renderer {
#Override
public void onDrawFrame(GL10 gl) {
// clear Screen Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Reset the Modelview Matrix
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f); // move 5 units INTO the screen
// is the same as moving the camera 5 units away
updateLogic(gl);
drawEverything(gl);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION); // or some matrix uniform if using shaders
gl.glLoadIdentity();
gl.glOrthof(0, width, height, 0, -1, 1); // this will allow to pass vertices in 'canvas pixel' coordinates
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Set Background
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
}
}
Not sure if this is the issue you are having, but try changing the following line:
gl.Orthof(0, widht, height, 0, -1, 1);
to
gl.Orthof(0, widht, height, 0, 1, -1);
Notice that the near/far values are inverted. See this for a description of this maddness :)

Android and glOrthof

I am developing a simple tile world game for Android 2.3. I am trying to set up an orthographic view in opengl but my 1x1 tiles show up oblong -- oriented to the direction of the screen -- if the screen is in vertical position then the square is stretched vertically, if horizontal then it is stretched horizontally. Here is what I have in my onSurfaceChanged method:
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 1.0f, 100.0f);
In my onDrawFrame method I have:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
GLU.gluLookAt(gl,
0.0f, 0.0f, 6.0f, // eye translation
0.0f, 0.0f, 0.0f, // eye center
0.0f, 1.0f, 0.0f // eye world up
);
// draw objects
From the object onDraw method:
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFloatVertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glPushMatrix();
gl.glTranslatef(localTranslation.getX(), localTranslation.getY(), localTranslation.getZ());
gl.glScalef(localScale.getX(), localScale.getY(), localScale.getZ());
gl.glRotatef(mAngle, localRotation.getX(), localRotation.getY(), localRotation.getZ());
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glNormal3f(0.0f, 0.0f, -1.0f);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
I have used the same onSurfaceCreated, onDrawFrame, and onDraw methods with perspective mode and it worked without any distortion to the objects being drawn. I have tried a few different things pulled from the net (like replacing the right/bottom of the ortho call with width/height aspect ratio) but nothing seemed to correct the problem.
Here is what the screen looks like:
Any ideas? What am I missing? All that I want to see are perfect 1x1 squares that I can throw textures on without distortion.
Try this
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 1.0f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
Ok this setting is what i use for both iOS and Android but when the screen ratio differs then positions of objects change.
glViewport(0, 0, backingWidth, backingHeight);
glOrthof(-1.0, //LEFT
1.0, //RIGHT
-1.0 * backingHeight / backingWidth, //BOTTOM
1.0 * backingHeight / backingWidth, //TOP
-2.0, //NEAR
100.0); //FAR
NOTE: That this works for normalised coordinates, which is what i use for all of my models.

OpenGLES10: Apply Projection & Camera View

I need a little help with this:
android developers, Tutorials: OpenGLES10.
a link
It all works fine for the first Triangle, until I put in the code for Projection & Camera View. This should rezise OpenGLES Square view to match Phone's screen, so object stay in propotions.
As a Newbie watching, the code looks fine and i have cheked with referencefiles, that there's not missing a parameter or something like that. But now i'm lost..! Can't see what's wrong.
If Projection and Camera code are applied, there is no triangle, but the app. is runing and the View with backgroundcolor are shown.
Here is my code:
package notme.helloopengles10;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
public class HelloOpenGLES10Renderer implements GLSurfaceView.Renderer {
// Set the background frame color
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// initialize the triangle vertex array
initShapes();
//enable use of vertex arrays
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
public void onDrawFrame(GL10 gl) {
// Redraw background color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
/* // set GL_MODELVIEW transformation mode (If outline from here to after GLU.gluLookAt() - it works when also outlines further down i code!
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity(); // reset Matrix to its default state
// when using GL_MODELVIEW, you must set the view point
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); */
//Draw Triangel
gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
// Redraw on orientation changes // adjust for screen size ratio
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
// Make adjustments for screen ratio
/*(If outline from here to after gl.Frumstumf() - it works!
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // apply the projection */
}
/*
* Draw a shape, a triangle. first add new member variable to contain
* the vertices of a triangle
*/
private FloatBuffer triangleVB;
//Create a method, initShaoe(), which populate the members variable
private void initShapes(){
//create a array
float triangleCoords[] = {
// X, Y, Z
-0.5f, -0.25f, 0,
0.5f, -0.25f, 0,
0.0f, 0,559016994f, 0
};
// initialize vertex Buffer for triangle
ByteBuffer vbb= ByteBuffer.allocateDirect(
//(# of coordinates values * 4 bytes per float)
triangleCoords.length * 4 );
vbb.order(ByteOrder.nativeOrder()); // use device hardware's native byte order
triangleVB = vbb.asFloatBuffer(); //create floating point buffer from the ByteBuffer
triangleVB.put(triangleCoords); // add coordinates to the FloatBuffer
triangleVB.position(0); // set the buffer to read the first coordinate
}
} // end
I hope some one can tell me, where things go wrong?
DevTool: Eclipse.
I had the same problem with this tutorial and it got solved when I changed the order of multiplying in the vertex shader code in the Triangle class. So instead of having uMVPMatrix * vPosition, replace it with vPosition * uMVPMatrix. I guess the reason for this is because vPosition is a row vector.
The code looks resonable (if you uncomment the parts that are commented out at the moment). Your matrix modification code is quite correct and all transformations are applied to the correct matrices.
But at the moment you are looking from the point (0,0,-5) to the point (0,0,0) and therefore along the +z axis. But since the default OpenGL view looks along the -z axis, you actually rotate the view 180 degrees around the y-axis. Whereas this is absolutely no problem, you now see the back-side of the triangle. So can it be, that you have back-face culling enabled and this back-side is just optimized away? Just try disabling back-face culling by calling glDisable(GL_CULL_FACE) or change the -5 in the gluLookAt call to a 5, so that you look along the -z axis.
You can also try to use gluPerspective(45, ratio, 3, 7) instead of the glFrustum call, but your arguments to glFrustum look quite reasonable. Of course, keep in mind that both calls create a perspective view, with farther objects getting smaller, like in reality. If you actually want a parallel/orthographic view (where size on screen is independent on depth) you should replace the glFrustum with a glOrtho, though the parameters can stay the same.
Your call to gluLookAt trashes your modelview matrix. You should call this function with the projection matrix active.
http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml
This code shows the triangle for me:
public void onDrawFrame(GL10 gl) {
// Redraw background color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// when using GL_MODELVIEW, you must set the view point
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// set GL_MODELVIEW transformation mode (If outline from here to after GLU.gluLookAt() - it works when also outlines further down i code!
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity(); // reset Matrix to its default state
//Draw Triangel
gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

Categories

Resources