android - 我怎样才能使多边形填充屏幕宽度的 80%?

标签 android opengl-es

我有一个简单的正方形(多边形),我希望它填充 80% 屏幕宽度

换句话说,我想把正方形放在 屏幕中心,但宽度为总宽度的 80% 屏幕的

我该怎么做?我找不到关于此的示例/教程

谢谢

这是我的方 block 的代码:

    public class Square {
        //Buffer de vertices
        private FloatBuffer vertexBuffer;
        //Buffer de coordenadas de texturas
        private FloatBuffer textureBuffer;
        //Puntero de texturas
        private int[] textures = new int[3];
        //El item a representar
        private int resourceId;
        //Definición de vertices
        private float vertices[] =
        {
                -1.0f, -1.0f, 0.0f,     //Bottom Left
                1.0f, -1.0f, 0.0f,              //Bottom Right
                -1.0f, 1.0f, 0.0f,              //Top Left
                1.0f, 1.0f, 0.0f                //Top Right
        };
        //Coordenadas (u, v) de las texturas
        /*
        private float texture[] =
        {
                //Mapping coordinates for the vertices
                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,
                1.0f, 1.0f
        };
        */
        private float texture[] =
        {
                //Mapping coordinates for the vertices
                0.0f, 1.0f,
                1.0f, 1.0f,
                0.0f, 0.0f,
                1.0f, 0.0f
        };
        //Inicializamos los buffers
        public Square(int resourceId) {
                ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length *
4);
                byteBuf.order(ByteOrder.nativeOrder());
                vertexBuffer = byteBuf.asFloatBuffer();
                vertexBuffer.put(vertices);
                vertexBuffer.position(0);

                byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
                byteBuf.order(ByteOrder.nativeOrder());
                textureBuffer = byteBuf.asFloatBuffer();
                textureBuffer.put(texture);
                textureBuffer.position(0);

                this.resourceId=resourceId;
        }
        //Funcion de dibujado
        public void draw(GL10 gl) {
                gl.glFrontFace(GL10.GL_CCW);
                //gl.glEnable(GL10.GL_BLEND);
                //Bind our only previously generated texture in this case
                gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
                //Point to our vertex buffer
                gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
                gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
                //Enable vertex buffer
                gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
                //Draw the vertices as triangle strip
                gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
                //Disable the client state before leaving
                gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
                //gl.glDisable(GL10.GL_BLEND);

        }
        //Carga de texturas
        public void loadGLTexture(GL10 gl, Context context) {
                //Generamos un puntero de texturas
                gl.glGenTextures(1, textures, 0);
                //y se lo asignamos a nuestro array
                gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
                //Creamos filtros de texturas
                gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
                gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
                //Diferentes parametros de textura posibles
GL10.GL_CLAMP_TO_EDGE
                gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_REPEAT);
                gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);

                /*
                String imagePath = "radiocd5.png";
            AssetManager mngr = context.getAssets();
            InputStream is=null;
                try {
                        is = mngr.open(imagePath);
                } catch (IOException e1) {      e1.printStackTrace();   }
                */
                //Get the texture from the Android resource directory
                InputStream is=null;
            /*
                if (item.equals("rim"))
                is =
context.getResources().openRawResource(R.drawable.rueda);
            else if (item.equals("selector"))
                is =
context.getResources().openRawResource(R.drawable.selector);
                */
                is = context.getResources().openRawResource(resourceId);
                Bitmap bitmap = null;
                try {
                        bitmap = BitmapFactory.decodeStream(is);
                } finally {
                        try {
                                is.close();
                                is = null;
                        } catch (IOException e) {
                        }
                }

                //con el siguiente código redimensionamos las imágenes que sean
mas grandes de 256x256.
                int newW=bitmap.getWidth();
                int newH=bitmap.getHeight();
                float fact;
                if (newH>256 || newW>256)
                {
                        if (newH>256)
                        {
                                fact=(float)255/(float)newH; //porcentaje por el que
multiplicar para ser tamaño 256
                                newH=(int)(newH*fact); //altura reducida al porcentaje
necesario
                                newW=(int)(newW*fact); //anchura reducida al porcentaje
necesario
                        }
                        if (newW>256)
                        {
                                fact=(float)255/(float)newW; //porcentaje por el que
multiplicar para ser tamaño 256
                                newH=(int)(newH*fact); //altura reducida al porcentaje
necesario
                                newW=(int)(newW*fact); //anchura reducida al porcentaje
necesario
                        }
                        bitmap=Bitmap.createScaledBitmap(bitmap, newW, newH, true);
                }

                //con el siguiente código transformamos imágenes no potencia de
2 en imágenes potencia de 2 (pot)
                //meto el bitmap NOPOT en un bitmap POT para que no aparezcan
texturas blancas.
                int nextPot=256;
                int h = bitmap.getHeight();
                int w = bitmap.getWidth();
                int offx=(nextPot-w)/2; //distancia respecto a la izquierda,
para que la imagen quede centrada en la nueva imagen POT
                int offy=(nextPot-h)/2; //distancia respecto a arriba, para que
la imagen quede centrada en la nueva imagen POT
                Bitmap bitmap2 = Bitmap.createBitmap(nextPot, nextPot,
Bitmap.Config.ARGB_8888); //crea un bitmap transparente gracias al
ARGB_8888
                Canvas comboImage = new Canvas(bitmap2);
                comboImage.drawBitmap(bitmap, offx, offy, null);
                comboImage.save();

                //Usamos Android GLUtils para espcificar una textura de 2
dimensiones para nuestro bitmap
                GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);

                //Checkeamos si el GL context es versión 1.1 y generamos los
Mipmaps por Flag. Si no, llamamos a nuestra propia implementación
                if(gl instanceof GL11) {
                        gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP,
GL11.GL_TRUE);
                        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);
                } else {
                        buildMipmap(gl, bitmap2);
                }
                //Limpiamos los bitmaps
                bitmap.recycle();
                bitmap2.recycle();
        }
        //Nuestra implementación de MipMap. Escalamos el bitmap original
hacia abajo por factor de 2 y lo asignamos como nuevo nivel de mipmap
        private void buildMipmap(GL10 gl, Bitmap bitmap) {
                int level = 0;
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();
                while(height >= 1 || width >= 1) {
                        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);
                        if(height == 1 || width == 1) {
                                break;
                        }
                        level++;
                        height /= 2;
                        width /= 2;
                        Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width,
height, true);
                        bitmap.recycle();
                        bitmap = bitmap2;
                }
        }
        } 

最佳答案

使用允许您根据比率处理视口(viewport)的投影。像这样:

glMatrixMode(GL_PROJECTION);
glLoadIdentity()
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

现在您的 OpenGL 坐标将范围 [0,1]² 映射到您的屏幕。 80% 的宽度在 x 坐标中为 0.8。

关于android - 我怎样才能使多边形填充屏幕宽度的 80%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7951216/

相关文章:

iphone - GLKit 和向纹理添加色调

android - Android : Pointing out the correct image in texture coordinate buffers 上的 VBO

android - 使用 opengl 绘制 3D Sprite

android - 需要 bool 类型的值 : Popup Menu

android - 在 ActionbarSherlock 中设置进度条样式

java - 克隆列表 - 它是如何完成的?

iphone - 将 480 x 320 背景渲染为 iPhone OpenGL ES 纹理的最快方法

android - Typeface.ITALIC 不适用于 Galaxy Note 2

安卓应用内容更新

c++ - 将 OpenGL 1.x 代码移植到 WebGL