开发者

OpenGL-ES texture loading problem with UV maps

开发者 https://www.devze.com 2023-03-26 18:28 出处:网络
Hy I have a little problem in openGL-ES texturing. I created a modell in 3ds max and used UV map for it, as you can see here(1st. picture) there is my UV map.

Hy I have a little problem in openGL-ES texturing. I created a modell in 3ds max and used UV map for it, as you can see here(1st. picture) there is my UV map. Without UV mapping my texture loading is "perfect", but with UV maps...(2nd picture here), look at the bogie).

I load this modell(bogie) from an obj file, there isn't any problem with my code or with the obj, because it is working with simple textures, maybe my loadtexture method is bad, pls check it, or do you have any Ideas? Thanks for your answers, I'm out of ideas.

Loadtexture code:

private int[] textures = new int[3];
    public void loadtexture(GL10 gl, Context mContext, String map_source) {
            try {
                InputStream is = mContext.getAssets().open(map_source);
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                is.close();
                gl.glGenTextures(3, textures, 0);

                gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
                gl.gl开发者_如何学JAVATexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                        GL10.GL_NEAREST);
                gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                        GL10.GL_NEAREST);
                GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);


                gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
                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);

                gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]);
                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_MIPMAP_NEAREST);
                if (gl instanceof GL11) {
                    gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP,
                            GL11.GL_TRUE);
                    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
                } else {
                    buildMipmap(gl, bitmap);
                }
                bitmap.recycle();
            } catch (IOException e) {
                // Should never happen
            }
        }

        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;
                }

                // Increase the mipmap level
                level++;

                //
                height /= 2;
                width /= 2;
                Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height,
                        true);

                // Clean up
                bitmap.recycle();
                bitmap = bitmap2;
            }
        }

P.S. Sorry for my english.


private int[] textures = new int[3];
    public void loadtexture(GL10 gl, Context mContext, String map_source) {
            try {
InputStream is = mContext.getAssets().open(map_source);
            Bitmap bitmap2 = BitmapFactory.decodeStream(is);
            is.close();
            Matrix flip = new Matrix();
            flip.postScale(1f, -1f);
            Bitmap bitmap =  Bitmap.createBitmap(bitmap2, 0, 0, bitmap2.getWidth(), bitmap2.getHeight(), flip, true);
            bitmap2.recycle();
... (same like before)

So I've flipped it vertically because loading maps is little bit diffrent in opengl. I hope that I helped some1 who get in to trouble with the same problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消