Sign in to follow this  
afkboard

World to screen coordinates - once again

Recommended Posts

Hi, I have couple of questions related to finding OpenGL world coordinates mapping in the real screen coordinates, I know there are many tuts out there, but I can't make it happen..

I initialize my world like this:


gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearColor(0f, 0f, 0f, 0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_BLEND);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();

GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();




The code is Java. It works fine.

Then I set the vertice for a rectangle I want to draw like this:


private float[] vertices = {
-1.0f, 1.0f, 0,
-1.0f, -1.0f, 0,
1.0f, -1.0f, 0,
1.0f, 1.0f, 0
};




And draw the rectangle like this:


gl.glPushMatrix();

// pay attention here
gl.glTranslatef(mx, my, 0);
gl.glScalef(0.3, 0.3, 0.3);

gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDrawElements(GL10.GL_TRIANGLES, indicies.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);




Then I want to set [bold]mx[/bold] and [bold]my[/bold] to screen coordinates, like: 240, 120 (pixels). And then I translate those values into 'opengl' world like this:


mx = (realx - (displayWidth()/2f)) * (2f / displayHeight());
my = displayHeight() - realy) - (displayHeight()/2)) * (1.5f/displayHeight());




The 'y' is ALMOST correct, but X is far from correct. I sow this kind of transformation somewhere, but it seems that it doesn't work due to the scaling and translate staff I've set.

According to this scenario, what is the easy way to set position of Rect in real screen coordinate system (like setPosition(240, 120) and then call needed transformation methods)?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this