我有一个简单的方形(多边形),我想,它填补了80% 屏幕的宽度
在换句话说,我想要放置在广场上的中心上 中心的画面,但随着80%的总宽度的宽度 屏幕
我怎样才能做到这一点?我找不到实例/关于本教程
感谢
这是在code我方:
公共类方{
//缓冲区德顶点
私人FloatBuffer vertexBuffer;
//缓冲区德coordenadas德texturas
私人FloatBuffer textureBuffer;
// Puntero德texturas
私人INT []纹理=新INT [3];
//厄尔尼诺项目重新presentar
私人诠释RESOURCEID;
//Definición德顶点
私人浮动顶点[] =
{
-1.0F,-1.0F,0.0,//左下
1.0F,-1.0F,0.0,//右下
-1.0F,1.0F,0.0,//左上
1.0F,1.0F,0.0 //右上
};
// Coordenadas(U,V) - 德拉斯texturas
/ *
私人浮纹[] =
{
//贴图坐标的顶点
0.0,0.0,
0.0,1.0F,
1.0F,0.0,
1.0F,1.0F
};
* /
私人浮纹[] =
{
//贴图坐标的顶点
0.0,1.0F,
1.0F,1.0F,
0.0,0.0,
1.0F,0.0
};
// Inicializamos洛杉矶缓冲区
市民广场(INT RESOURCEID){
ByteBuffer的byteBuf = ByteBuffer.allocateDirect(vertices.length *
4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(顶点);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(纹理);
textureBuffer.position(0);
this.resourceId = RESOURCEID;
}
// Funcion德dibujado
公共无效画(GL10 GL){
gl.glFrontFace(GL10.GL_CCW);
//gl.glEnable(GL10.GL_BLEND);
//在这种情况下pviously生成的纹理绑定我们唯一的$ P $
gl.glBindTexture(GL10.GL_TEXTURE_2D,纹理[0]);
//指向我们的顶点缓冲
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer);
//启用顶点缓冲区
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//绘制顶点三角形带
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,vertices.length / 3);
临行前//禁用客户端状态
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//gl.glDisable(GL10.GL_BLEND);
}
// Carga德texturas
公共无效loadGLTexture(GL10 GL,上下文语境){
// Generamos未puntero德texturas
gl.glGenTextures(1,纹理,0);
//Ÿ本身LO asignamos一个NUESTRO阵列
gl.glBindTexture(GL10.GL_TEXTURE_2D,纹理[0]);
// Creamos筛选现在去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德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);
/ *
字符串的ImagePath =radiocd5.png;
AssetManager mngr = context.getAssets();
InputStream的是= NULL;
尝试 {
是= mngr.open(的ImagePath);
}赶上(IOException异常E1){e1.printStackTrace(); }
* /
//获取纹理从Android资源目录
InputStream的是= NULL;
/ *
如果(item.equals(RIM))
是=
。context.getResources()openRawResource(R.drawable.rueda);
否则,如果(item.equals(选择))
是=
。context.getResources()openRawResource(R.drawable.selector);
* /
,是= context.getResources()openRawResource(RESOURCEID);
点阵位图= NULL;
尝试 {
位= BitmapFactory.de codeStream(是);
} 最后 {
尝试 {
is.close();
是=无效;
}赶上(IOException异常E){
}
}
// CON EL siguientecódigoredimensionamos拉斯imágenes阙肖恩
马斯GRANDES德256×256。
INT东西方妇女网络= bitmap.getWidth();
INT newH = bitmap.getHeight();
浮动事实;
如果(newH> 256 ||东西方妇女网络> 256)
{
如果(newH> 256)
{
其实=(浮点)255 /(浮点)newH; // porcentaje POR EL阙
multiplicar对SER玉野256
newH =(INT)(newH *事实); // Altura的reducida人porcentaje
necesario
东西方妇女网络=(INT)(东西方妇女网络*事实); // anchura reducida人porcentaje
necesario
}
如果(东西方妇女网络> 256)
{
其实=(浮点)255 /(浮点)东西方妇女网络; // porcentaje POR EL阙
multiplicar对SER玉野256
newH =(INT)(newH *事实); // Altura的reducida人porcentaje
necesario
东西方妇女网络=(INT)(东西方妇女网络*事实); // anchura reducida人porcentaje
necesario
}
位= Bitmap.createScaledBitmap(位图,东西方妇女网络,newH,真正的);
}
// CON EL siguientecódigotransformamosimágenes没有potencia德
2间imágenespotencia德2(壶)
//梅托埃尔位NOPOT恩未位POT对阙没有aparezcan
texturas卡斯。
INT nextPot = 256;
INT H = bitmap.getHeight();
INT W = bitmap.getWidth();
INT offx =(nextPot-瓦特)/ 2; // DISTANCIA respecto一拉左翼,
第阙拉imagen画质quede centrada EN LA努埃瓦imagen画质POT
INT offy =(nextPot-H)/ 2; // DISTANCIA respecto一个阿里巴,对阙
拉imagen画质quede centrada EN LA努埃瓦imagen画质POT
位图bitmap2 = Bitmap.createBitmap(nextPot,nextPot,
Bitmap.Config.ARGB_8888); // CREA未位transparente格拉西亚斯人
ARGB_8888
帆布comboImage =新的Canvas(bitmap2);
comboImage.drawBitmap(位图,offx,offy,NULL);
comboImage.save();
// Usamos的Android GLUtils对espcificar UNA textura日2
dimensiones对NUESTRO位图
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bitmap2,0);
// Checkeamos SI EL GL上下文ES版1.1ÿgeneramos洛杉矶
贴图上电复位标志。思无,llamamos一个圣母propiaimplementación
如果(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);
} 其他 {
buildMipmap(GL,bitmap2);
}
// Limpiamos洛杉矶位图
bitmap.recycle();
bitmap2.recycle();
}
//圣母implementación德MIPMAP。 Escalamos埃尔位原
hacia阿瓦霍POR因素去2 Y LO asignamos科莫索尔NIVEL德的mipmap
私人无效buildMipmap(GL10 GL,位图位图){
INT级别= 0;
INT高= bitmap.getHeight();
INT宽度= bitmap.getWidth();
而(高度> = 1 ||宽度GT; = 1){
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,水平,位图,0);
如果(高度== 1 ||宽度== 1){
打破;
}
++级;
高度/ = 2;
宽度/ = 2;
位图bitmap2 = Bitmap.createScaledBitmap(位图,宽度,
高度,真正的);
bitmap.recycle();
位= bitmap2;
}
}
}
解决方案
使用的投影,可以让你解决视口比例的条款。像这样的:
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
glOrtho(0,1,0,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
现在你的OpenGL坐标映射区间[0,1]²到屏幕上。 80%的宽度将在x坐标0.8。
I have a simple square (polygon), and i want that it fills the 80% of the width of the screen
In other words, i want to position the square on the center on the center of the screen, but with a width of the 80% of the total width of the screen
How can i do it? I can't find examples/tutorials about this
thanks
THis is the code for my square:
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;
}
}
}
解决方案
Use a projection that allows you to address the viewport in terms of ratios. Like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Now your OpenGL coordinates map the range [0,1]² to your screen. A 80% width would be 0.8 in the x coordinate.