OpenGL ES for iPhone - get my current 3d element position

Hello,

I’m a french iPhone developper and went to creat some 3d view with openGL ES 2.0.
Today i have one problem !! I went to get the position of my current 3d element,

How can i do that ?

Thinks

Here you go:

  • you’ll need a projection matrix P
  • you’ll need a world to camera matrix C
  • for each element you’ll need a element to world matrix M

in opengles 1.x P was done with glfrustum or glortho and C*M was called the modelviewmatrix. Then you’ll have to set P, C and M as Uniforms to your shader. If you have no idea what I’m talking about, you should search for “transformations in the affine vector space” :stuck_out_tongue:

Hye,

I’m trying to create a 3D project and so I fulfill to load a cube.
However after changing from “glOrtho” to “glPerspective” I can’t see my cube.
I don’t understand why because I just add new functions in order to translate gluLookAt and glPerspective.

Here are the code.


const GLfloat cubeVertices[] = {
	// face de devant
	-0.5, 0.5, -0.5,             
	-0.5, -0.5, -0.5,           
	0.5, -0.5, -0.5,             
	0.5, 0.5, -0.5,              
	
	// haut
	-0.5, 0.5, -0.5,           
	-0.5, 0.5, 0.5,             
	0.5, 0.5, 0.5,              
	0.5, 0.5, -0.5,             
	
	// arrière
	0.5, 0.5, 0.5,         
	0.5, -0.5, 0.5,           
	-0.5, -0.5, 0.5,           
	-0.5, 0.5, 0.5,            
	
	// dessous
	-0.5, -0.5, 0.5,
	-0.5, -0.5, -0.5,
	0.5, -0.5, -0.5,
	0.5, -0.5, 0.5,
	
	// gauche
	-0.5, 0.5, -0.5,
	-0.5, 0.5, 0.5,
	-0.5, -0.5, 0.5,
	-0.5, -0.5, -0.5,
	
	// droit
	0.5, 0.5, 0.5,
	0.5, 0.5, -0.5,
	0.5, -0.5, -0.5,
	0.5, -0.5, 0.5
}; 


Cube code


Mat4x4 cameraView = Camera::getInstance()->gluLookAtMatrix();
Mat4x4 proj = Mat4x4::setPerspective((float)(768.0f/1024.0f), 60.0f, 1.0, 20.0f);

//Mat4x4::setOrtho(proj, -4.0f, 4.0f, -6.0f, 6.0f, -4.0f, 4.0f); I can see the cube with this
	
	
	
Mat4x4 rot,trans = Mat4x4::setTranslation(0.0f, 1.0f, 5.0f);
rot.rotY(r_angle);
	
Mat4x4 mvp = cameraView * proj * rot * trans;
	
r_angle += 0.05f;
glUseProgram([s_loader getProgramIdentifier]);
glVertexAttribPointer(attribPositionOnShader, 3, GL_FLOAT, 0, 0, cubeVertices);
glEnableVertexAttribArray(attribPositionOnShader);
glVertexAttribPointer(attribColorOnShader, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
glEnableVertexAttribArray(attribColorOnShader);
glUniformMatrix4fv(projMatOnShader, 16, GL_FALSE, mvp.toArray());
	
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
glDrawArrays(GL_TRIANGLE_FAN, 8, 4);
glDrawArrays(GL_TRIANGLE_FAN, 12, 4);
glDrawArrays(GL_TRIANGLE_FAN, 16, 4);
glDrawArrays(GL_TRIANGLE_FAN, 20, 4);
	
glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);


Mat4x4 Camera::gluLookAtMatrix(){
	Vec3 vForward, vUpNorm, vSide;
	Mat4x4 result,translation = Mat4x4::setTranslation(-(*position)[0], -(*position)[1], -(*position)[2]);
	Point3D tmp = *lookAt - *position;
	vForward = tmp.ptToVec();
	
	vForward.normalize();
	vUpNorm = up->normalized();
	vSide   = vUpNorm * vForward;
	vUpNorm = vSide * vForward;
	
	result[0] = vSide[0];		result[1] = vSide[1];		result[2] = vSide[2];		result[3] = 0;
	result[4] = vUpNorm[0];		result[5] = vUpNorm[1];		result[6] = vUpNorm[2];		result[7] = 0;
	result[8] = -vForward[0];	result[9] = -vForward[1];	result[10] = -vForward[2];	result[11] = 0;
	result[12] = 0;				result[13] = 0;				result[14] = 0;				result[15] = 1;
	
	
	result *= translation;
	return result;
}


Mat4x4 Mat4x4::setPerspective(float aspect,float fov,float zFar,float zNear){
	Mat4x4 perspectiveMat;
	float f = atanf(fov / 2);
	
	perspectiveMat[0] = f/aspect;	
	perspectiveMat[5] = f;
	perspectiveMat[10] = (zFar - zNear)/(zNear+zFar);
	perspectiveMat[11] = 2 * (zFar * zNear)/(zNear - zFar);
	perspectiveMat[14] = -1;

	return perspectiveMat;
	
}

Could anyone help me please?

I didn’t read your code, but have you noticed that in the orthographic mode, the dimension is in pixels? It looks like your code is then only 1 pixel, so you have to scale it…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.