gpt4 book ai didi

ios - OpenGL ES 旋转二维纹理

转载 作者:行者123 更新时间:2023-11-28 21:59:04 24 4
gpt4 key购买 nike

我是 OpenGL 的新手。我正在 iPad 上构建一个绘画应用程序,我有点关注 Apple 的 GLPaint例子。基本上我想在每次触摸移动时将纹理绘制到触摸位置。我能够从图像加载纹理并使用以下代码绘制它以查看:

初始化 GL:

- (BOOL)initGL {

glGenFramebuffers(1, &viewFramebuffer);
glGenRenderbuffers(1, &viewRenderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return NO;
}

glViewport(0, 0, backingWidth, backingHeight);

glGenBuffers(1, &vboId);

brushTexture = [self textureFromName:@"Brush2.png"];

[self setupShaders];

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

return YES;
}

加载纹理:

- (textureInfo_t)textureFromName:(NSString *)name {
CGImageRef brushImage;
CGContextRef brushContext;
GLubyte *brushData;
size_t width, height;
GLuint texId;
textureInfo_t texture;

brushImage = [UIImage imageNamed:name].CGImage;

width = CGImageGetWidth(brushImage);
height = CGImageGetHeight(brushImage);

if(brushImage) {
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);

CGContextRelease(brushContext);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);

free(brushData);

texture.id = texId;
texture.width = (int)width;
texture.height = (int)height;
}

return texture;
}

着色器:

- (void)setupShaders {
for (int i = 0; i < NUM_PROGRAMS; i++)
{
char *vsrc = readFile(pathForResource(program[i].vert));
char *fsrc = readFile(pathForResource(program[i].frag));
GLsizei attribCt = 0;
GLchar *attribUsed[NUM_ATTRIBS];
GLint attrib[NUM_ATTRIBS];
GLchar *attribName[NUM_ATTRIBS] = {
"inVertex",
};
const GLchar *uniformName[NUM_UNIFORMS] = {
"MVP", "pointSize", "vertexColor", "texture",
};

for (int j = 0; j < NUM_ATTRIBS; j++)
{
if (strstr(vsrc, attribName[j]))
{
attrib[attribCt] = j;
attribUsed[attribCt++] = attribName[j];
}
}

glueCreateProgram(vsrc, fsrc,
attribCt, (const GLchar **)&attribUsed[0], attrib,
NUM_UNIFORMS, &uniformName[0], program[i].uniform,
&program[i].id);
free(vsrc);
free(fsrc);

if (i == PROGRAM_POINT)
{
glUseProgram(program[PROGRAM_POINT].id);

glUniform1i(program[PROGRAM_POINT].uniform[UNIFORM_TEXTURE], 0);

GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, backingWidth, 0, backingHeight, -1, 1);
GLKMatrix4 modelViewMatrix = GLKMatrix4Identity; // this sample uses a constant identity modelView matrix
GLKMatrix4 MVPMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);

glUniformMatrix4fv(program[PROGRAM_POINT].uniform[UNIFORM_MVP], 1, GL_FALSE, MVPMatrix.m);

glUniform1f(program[PROGRAM_POINT].uniform[UNIFORM_POINT_SIZE], brushTexture.width / kBrushScale);

glUniform4fv(program[PROGRAM_POINT].uniform[UNIFORM_VERTEX_COLOR], 1, brushColor);
}
}

glError();

绘制:

- (void)renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end {
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;

[EAGLContext setCurrentContext:context];

glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);

CGFloat scale = self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;

if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}

vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}

glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);

glUseProgram(program[PROGRAM_POINT].id);
glDrawArrays(GL_POINTS, 0, (int)vertexCount);

glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];

我缺少的是一种在每次绘制之前旋转纹理的方法。我尝试使用旋转的 CGContext 初始化纹理,但是每次绘制一个点时都需要进行太多处理才能调用。有人可以告诉我一种更好的旋转纹理的方法吗?我是 OpenGL 的新手,我们将不胜感激。

最佳答案

您可以使用 GL_TEXTURE 矩阵模式将变换应用于纹理坐标。例如,将纹理坐标绕z轴逆时针旋转90度:

glMatrixMode(GL_TEXTURE);
glTranslatef(1.0f, 0.0f, 0.0f);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

注意在绕原点旋转后将纹理坐标范围保持在 [0.0, 1.0] 范围内的平移。我之前对类似问题的回答在这里更详细地解释了其中的一些内容:gluCylinder with rotated texture .

我通常会尽量避免推荐与张贴者询问的技术不同的技术,但在这种情况下我无能为力:如果您是 OpenGL ES 的新手,我强烈建议您跳过 ES 1.x。我相信现在大多数人会认为它是一个过时的 API。例如,如果您导航到 www.khronos.com (https://www.khronos.org/opengles/sdk/docs/) 上的手册页,您会注意到不再有指向 ES 1.x 的链接。如果您浏览更多内容,内容当然仍然存在,但它清楚地暗示 ES 2.0 如今被视为基线。

关于ios - OpenGL ES 旋转二维纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25560854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com