0
\$\begingroup\$

I need to know how to get drawn image length and width.

I have this code so far where I draw Radar in left top corner with hard-coded coordinates:

int _SRC = R.drawable._item; 

...

float vertices[] = {
            0f,  0f,  0.0f,
            0f,  2.0f,0.0f,
            2.0f,0f,  0.0f,         
            2.0f,2.0f,0.0f
    };
...

addRadarToView(0,240); 

...

public void addRadarToView(float x, float y) {      
    this.x = x;
    this.y = y;     
}


 ...

private void updateScreenData(float screenWidth, float screenHeight){
    // takes screen Height and Width
    this.mScreenWidth = (screenWidth > 0) ? screenWidth : 1f;
    this.mScreenHeight = screenHeight;
    wRatio = 10f/mScreenWidth;
    hRatio = mScreenHeight/10f;     
}


 @Override
public void loadTextures(GL10 gl) {
    Log.e(LOG_TAG, "loadTextures");  

    InputStream is;

    is = mContext.getResources().openRawResource(_SRC);

    Bitmap bitmap = BitmapFactory.decodeStream(is);

    try {
        is.close();
        is = null;
    } catch (IOException e) {
    }

    gl.glGenTextures(TEXTURE_COUNT, textures, 0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle(); 


}

 protected void drawRadar(GL10 gl){
    gl.glPushMatrix();
    try {           
        float transx =  (wRatio * (x)); 
        float transy =  (mScreenHeight*wRatio) - (wRatio * (y)) - 1/hRatio; 
        // Log.e(LOG_TAG, "transx: " + transx + "; transy: " + transy + "; sprite.x: "+  sprite.x + "; sprite.y: " + sprite.y);  

        gl.glTranslatef(transx, transy, 0.0f);

        //###########  draw ##############

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);

        //update();

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        //###############################

    } catch (NullPointerException e) {
    }
    gl.glPopMatrix();   
}

I used addRadarToView(0,240); to set x=0 and y= 240;

So as I see x represents left border of vertices. When 240 is y that represents bottom border. Strange.

[EDIT]

In canvas i used something like:

 int srcX = currentFrame * width;

Rect  src = new Rect(srcX, 0, srcX+width, height);
Rect  dst = new Rect(x, y, x+width, y+height);
canvas.drawBitmap(bitmap, src, dst, null);

Where width and height are:

bitmap.getWidth();
bitmap.getHeight();

Please, help me to sort it out.

Thanks,

\$\endgroup\$
5
  • \$\begingroup\$ Did you call updateScreenData() with the correct resolution? \$\endgroup\$
    – Mario
    Jul 25, 2013 at 9:54
  • \$\begingroup\$ yep, from Parent class, its working code, just want to replace hard-coded part: addRadarToView(0,240); \$\endgroup\$ Jul 25, 2013 at 10:53
  • \$\begingroup\$ Not really sure what you're asking then? Do you want to calculate the actual coordinates where the radar is drawn to? \$\endgroup\$
    – Mario
    Jul 25, 2013 at 10:54
  • \$\begingroup\$ when I draw vertice on the screen, I tell where to draw it (x,y). But position should be based on Bitmap size. otherwise if image is big, it can be out of the screen \$\endgroup\$ Jul 25, 2013 at 10:57
  • \$\begingroup\$ Still not really sure what you're looking for. Are you unhappy with the origin of the radar screen (i.e. the point you pass to draw it)? \$\endgroup\$
    – Mario
    Jul 25, 2013 at 11:06

1 Answer 1

2
\$\begingroup\$

This is one of the great things about openGL over canvas. OpenGL and android will handle the scaling of the image for you. Depending on how you build your renderer class will determine exactly how you specify your vertices location, but generally if you follow a how to guide you will always be working in a plane from -1 to +1 for 2D games. This means that if you declare a bitmap within this plane you can simply use translatef() to move the image where it needs to be.

After looking at your code I believe the problems you are having is because you are attempting to use pixels to determine an amount to translate your image by. It is important to use raw measurements from a 1.0 dpi device to get your base pixel location, otherwise it will not scale properly.

This is how I did it here and here

To summarize I believe your confusion is coming from the fact that openGL does not use pixels to determine location but instead uses a vertices that are translated during run.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .