我想从一些位图中拉伸或平铺一个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比萨
你的代码中有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)是通过自己定义几何来固定的,提供适当的纹理坐标。