Opengl es 我在opengles中获得纹理,现在我想旋转纹理,但失败了
我有一段代码可以得到opengles的纹理Opengl es 我在opengles中获得纹理,现在我想旋转纹理,但失败了,opengl-es,rotation,textures,Opengl Es,Rotation,Textures,我有一段代码可以得到opengles的纹理 -(void)textureFromName:(UIImage *)name { CGImageRef brushImage; CGContextRef brushContext; GLubyte *brushData; size_t width, height; GLuint texId; brushImage = [name CGImage]
-(void)textureFromName:(UIImage *)name
{
CGImageRef brushImage;
CGContextRef brushContext;
GLubyte *brushData;
size_t width, height;
GLuint texId;
brushImage = [name CGImage];
// Get the width and height of the image
width = CGImageGetWidth(brushImage)*4;
height = CGImageGetHeight(brushImage);
// Make sure the image exists
if(brushImage) {
// Allocate memory needed for the bitmap context
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
// Use the bitmatp creation function provided by the Core Graphics framework.
brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the image to the context.
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(brushContext);
// Use OpenGL ES to generate a name for the texture.
glGenTextures(1, &texId);
// Bind the texture name.
glBindTexture(GL_TEXTURE_2D, texId);
**/*************************************************/
//rotate the texture,but didn't work
static float angle = 1;
if(angle < 200)
angle+=2;
else
angle = 1;
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glRotatef(angle,.0,0.0,1.0);
glTranslatef(0.0,0.0,0.0);
glTranslatef(-0.5,-0.5,0.0);
glMatrixMode(GL_MODELVIEW);
/*************************************************/**
// Set the texture parameters to use a minifying filter and a linear filer (weighted average)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Specify a 2D texture image, providing the a pointer to the image data in memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
// Release the image data; it's no longer needed
free(brushData);
}
-(void)textureFromName:(UIImage*)名称
{
CGImageRef-brushImage;
CGContextRef-brushContext;
GLubyte*数据;
尺寸、宽度、高度;
胶状体;
brushImage=[名称CGImage];
//获取图像的宽度和高度
宽度=CGImageGetWidth(画笔图像)*4;
高度=CGImageGetHeight(画笔图像);
//确保图像存在
如果(画笔图像){
//分配位图上下文所需的内存
brushData=(GLubyte*)calloc(宽*高*4,sizeof(GLubyte));
//使用核心图形框架提供的bitmatp创建功能。
brushContext=CGBitmapContextCreate(brushData,宽度,高度,8,宽度*4,CGImageGetColorSpace(brushImage),KCGimageAlphaPremultipledLast);
//创建上下文后,可以将图像绘制到上下文中。
CGContextDrawImage(brushContext,CGRectMake(0.0,0.0,(CGFloat)宽度,(CGFloat)高度),brushImage);
//此时不需要上下文,因此需要释放它以避免内存泄漏。
CGContextRelease(brushContext);
//使用OpenGL ES生成纹理的名称。
glGenTextures(1和texId);
//绑定纹理名称。
glBindTexture(GL_TEXTURE_2D,texId);
**/*************************************************/
//旋转纹理,但不起作用
静态浮动角度=1;
如果(角度<200)
角度+=2;
其他的
角度=1;
glMatrixMode(GL_纹理);
glLoadIdentity();
glRotatef(角度,.0,0.0,1.0);
glTranslatef(0.0,0.0,0.0);
glTranslatef(-0.5,-0.5,0.0);
glMatrixMode(GLU模型视图);
/*************************************************/**
//设置纹理参数以使用缩小过滤器和线性过滤器(加权平均)
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
//指定2D纹理图像,提供指向内存中图像数据的指针
GLTEXAGE2D(GL_纹理_2D,0,GL_RGBA,宽度,高度,0,GL_RGBA,GL_无符号_字节,笔刷数据);
//释放图像数据;不再需要它
免费(数据);
}
}
问题是:
//旋转纹理,但不起作用
静态浮动角度=1如果(角度<200) 角度+=2; 其他的 角度=1; glMatrixMode(GL_纹理); glLoadIdentity(); glRotatef(角度,.0,0.0,1.0); glTranslatef(0.0,0.0,0.0); glTranslatef(-0.5,-0.5,0.0); glMatrixMode(GLU模型视图); 我想旋转纹理,但不起作用,所以
有人能帮我吗?非常感谢你 尝试此操作,而不是将wrap设置为GL_CLAMP的代码。您应该能够看到纹理旋转
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5,0.5,0.0);
glRotatef(angle,.0,0.0,1.0);
glTranslatef(-0.5,-0.5,0.0);
glMatrixMode(GL_MODELVIEW);
这是一个涂鸦演示。我尝试了这个方法,但不起作用,我需要在-(void)touchesMoved:(NSSet*)toucheevent:(UIEvent*)事件中调用函数-(void)textureFromName:(UIImage*)name,这样我就可以画出一条效果很好的线,再次感谢!是的,如果你的意图是实现某种动画效果,那么你必须调用该函数。我只是想知道,你是否每次都为同一张图像调用这个(void)textureFromName:(UIImage*)名称?是的,你知道GLPaint演示吗?我使用了(void)textureFromName:(UIImage*)获取纹理,这样我就可以用纹理绘制一条线,如果可以在TouchsMoved函数中旋转纹理,线看起来会不均匀,(纹理的图像是不规则的)我不知道GLPaint演示,但我检查了代码。由于使用的是相同的纹理,因此不必为了旋转而反复调用(void)textureFromName:(UIImage*)。您已经拥有该纹理的控制柄。相反,您可以将旋转代码块移动到-(void)renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end。谢谢您的热情,我尝试了这个方法,但仍然失败了