Jump to content
  • Advertisement
Sign in to follow this  

OpenGL [Android] Perspective results wrong

This topic is 2125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone


Yesterday I found my android tablet and decided to dive back in in android development. I already did something with OpenGL ES and it worked, so i took a lot of classes and functions from my old example. For everything UI-related with orthographic projection its fine, it renders as expected but as soon as i render my world with perspectivic projection the output looks terribly wrong, which is rather strange as it used to work in my old project.


Let me first show you the "result"



I already know that kind of weird output moving to the center point from one of my desktop projects where the projection matrix was wrong. So my first guess was that its somehow related to that. My shader looks like this:

attribute vec3 position0;
attribute vec2 texcoord0;
attribute vec3 normal0;

uniform mat4 matView;
uniform mat4 matProj;

varying vec2 texCoord;
varying vec3 varNormal;

void main() {
	texCoord = texcoord0;
	varNormal = normal0;

	gl_Position = matProj * matView * vec4(position0, 1.0);

And in the fragment shader im just returning the varNormal normalized to [0, 1] for each component.


While usually the matrices are set automatically for debug purposes ive set them manually before rendering to make sure the error does not come from there:

prog.setMatrix(prog.getUniform("matProj"), Matrix.Perspective(45.0f, UIManager.getInstance().getWidth(), UIManager.getInstance().getHeight(), 2.0f, 600.0f));
prog.setMatrix(prog.getUniform("matView"), Matrix.LookAt(new Vector3(-10, 0, 100.0f), new Vector3(-9, 0, 100.0f), new Vector3(0, 0, 1)));

I have verified width and height, they return 1280/752 which is correct. Both uniforms are retreived correctly (matProj == 0, matView == 1). The matrix class is the same i used in my other example so that should not cause any problems, but the code looks like this:

public static Matrix Perspective(float fov, int width, int height, float zNear, float zFar) {
	Matrix ret = new Matrix();

	android.opengl.Matrix.perspectiveM(ret.mMatrixFloats, 0, fov, ((float)width / height), zNear, zFar);

	return ret;

public static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) {
	Matrix ret = new Matrix();

	android.opengl.Matrix.setLookAtM(ret.mMatrixFloats, 0, eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

	return ret;

And the uniforms are set using:

public void setMatrix(int index, Matrix mat) {
	GLES20.glUniformMatrix4fv(index, 1, false, mat.getFloats(), 0);

(to be sure i also set transposed to true but then everything vanishes)


So far i could not find any problem whatsoever with the matrices so i thought it has to be something with the input data beeing wrong. All the data is stored in a ByteBuffer and then transfered to a vertex buffer. I have verified the contents of the ByteBuffer, they are all correct.


The entire input layout code is used in the user interface as well so there should be no error, if you need it i can post it, but there is quite a bunch of classes involved, so i hope the error is somewhere in the code above.


Thanks in advance and greetings


Share this post

Link to post
Share on other sites



I solved my problem. It was an issue in writing the indices. I forgot that i am using a ByteBuffer and not an IntBuffer so my putInt values had to have their index multiplied by 4... Now the result is ok:





Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!