【问题标题】:opengl es texturing problem androidopengl es纹理问题android
【发布时间】:2011-11-12 23:31:01
【问题描述】:

我想从某个位图中拉伸或平铺 opengl 对象。我使用位图的一部分的图像。 但我的戒指和中间的纹理具有平均图像颜色。如果我改变图像颜色变化,但图片没有显示。

这里是我的完整代码

  //  ..in init///


           gl.glEnable(GL10.GL_TEXTURE_2D);   
           textures = new int[3];
           bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pizzaf);
           bitmap = Bitmap.createBitmap(   bitmap, 0, 0,    bitmap.getWidth(),
                   bitmap.getHeight(), null, true);
           bitmap1 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pizslice);
           bitmap1 = Bitmap.createBitmap(   bitmap1, 0, 0,    bitmap1.getWidth(),
                   bitmap1.getHeight(), null, true);
        // Tell OpenGL to generate textures.
           gl.glTexParameterf(GL10.GL_TEXTURE_2D,
                   GL10.GL_TEXTURE_WRAP_S,
                   GL10.GL_CLAMP_TO_EDGE);
//         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
//                  GL10.GL_LINEAR);
//          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
//                  GL10.GL_LINEAR);

           gl.glGenTextures(1, textures, 0);

    }

和绘图功能

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap1, 0);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);

                 GLUT.glutSolidTorus(gl,0.175f, 0.95f, 12, 48);
                 gl.glTranslatef(0,0, 0.2f);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
                GLUT.glutSolidCone(gl, 1,0.01f, 12, 48);
            gl.glTranslatef(0,0, -0.4f);
            gl.glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap1, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
        GLUT.glutSolidCone(gl, 1,0.01f, 12, 48);

请帮助任何人。我想做一个 3d 比萨

【问题讨论】:

    标签: android opengl-es texturing


    【解决方案1】:

    你的代码有3个问题:

    1. 您没有正确使用纹理对象
    2. 您在每次绘制调用时重新初始化纹理
    3. glutSolidCone 不提供纹理坐标(意味着:您无法正确纹理化它)

    修复(1)

    纹理总是按照这个方案加载的:

    GLuint texture_object_name;
    
    glGenTextures(1, 
                  &texture_object_name); // you can create texture object names in batches
    
    glBindTexture(GL_TEXTURE_2D, // may differ, depending on texture target (i.e. kind of texture)
                  texture_object_name);
    
    glPixelStorei(GL_UNPACK_ALIGNMENT, …);   // </ depends on data format
    glPixelStorei(GL_UNPACK_ROW_LENGTH, …);  // <|
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, …); // <|
    glPixelStorei(GL_UNPACK_SKIP_ROWS, …);   // <|
    
    glTexImage2D(GL_TEXTURE_2D, // again the target, depends on the kind of texture creates
                 0, // mipmap level
                 GL_RGBA, // internal format, GL_RGBA is one possibility, there are others
                 width, height, border, // border is usually 0
                 GL_BGRA,                  // format of the data input
                 GL_UNSIGNED_INT_8_8_8_8,  // type of the data input -- those must match the data
                 texture_data );
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, …); // mipmapping on/off and other settings
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, …); // you should really set those
    

    修复(2)

    一旦你修复了 (1),你就可以修复 (2)。要在渲染时切换纹理,您只需调用

    glBindTexture(GL_TEXTURE_2D, texture_object_name);
    

    在后续的绘图调用中使用它。

    修复(3)

    (3) 是通过自己定义几何图形来修复的,提供适当的纹理坐标。

    【讨论】:

    • 我在下面的代码中添加了 init float textureCoordinates[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }; ByteBuffer byteBuf = ByteBuffer.allocateDirect(textureCoordinates.length * 4); byteBuf.order(ByteOrder.nativeOrder()); FloatBuffer textureBuffer = byteBuf.asFloatBuffer(); textureBuffer.put(textureCoordinates); textureBuffer.position(0); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);它的opengl es不是opengl
    • 提供纹理坐标指针对您没有帮助,只要您使用 glutSolidCode。 OpenGL 应该如何知道如何将纹理坐标与顶点相关联?位置、纹理坐标等一起形成一个称为“顶点”的向量。仅提供一些与顶点无关的纹理坐标对您没有帮助。
    • gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);是不是用坐标绑定纹理,如果不是什么方法?
    • 从技术上讲,这绑定了纹理坐标,是的。但是您只是绑定了一些不相关的值,这些值与 glutSolidCone 发送到 OpenGL 的值不对应。它根本行不通。您需要提供完整的顶点属性集。即:位置、法线、纹理坐标。为此,请勿使用 glutSolidCone——它根本不支持纹理化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多