UV 贴图的 OpenGL-ES 纹理加载问题

发布于 2024-11-28 08:47:35 字数 3276 浏览 1 评论 0原文

我在 openGL-ES 纹理方面遇到了一些问题。我在 3ds max 中创建了一个模型并为其使用了 UV 贴图,如您所见 这里(第一张图片)是我的 UV 贴图。 如果没有 UV 贴图,我的纹理加载是“完美的”,但是使用 UV 贴图...(第二张图片 这里),看看转向架)。

我从 obj 文件加载这个模型(转向架),我的代码或 obj 没有任何问题,因为它正在处理简单的纹理,也许我的 loadtexture 方法不好,请检查它,或者你有任何问题吗?有想法吗? 谢谢你的回答,我没有想法了。

Loadtexture 代码:

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.glTexParameterf(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;
            }
        }

PS 抱歉我的英语。

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.glTexParameterf(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-12-05 08:47:35
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)

所以我垂直翻转了它,因为加载地图在 opengl 中有点不同。我希望我能帮助那些遇到同样问题的人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文