1

我想从某个位图中拉伸或平铺一个 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 比萨

4

1 回答 1

3

您的代码中有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) 通过自己定义几何图形来固定,提供适当的纹理坐标。

于 2011-09-08T13:08:00.817 回答