gpt4 book ai didi

iphone - GLKit 和向纹理添加色调

转载 作者:行者123 更新时间:2023-12-03 19:18:39 25 4
gpt4 key购买 nike

我在使用 GLKit 对 PNG 图像着色时遇到问题。

我有一个白色 PNG 图像,我将其加载到应用程序中,然后使用它来创建纹理:

UIImage *image = [ UIImage imageNamed:@"brushImage" ];
NSError *error = nil;
texture_m = [[ GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error] retain ];
if (error) {
NSLog(@"Error loading texture from image: %@",error);
}

纹理创建时没有错误。

但是,当我想在激活混合的情况下渲染纹理时,颜色似乎被忽略,并且我得到了白色图像。当我在 OpenGL1 中执行此操作时,我没有遇到任何问题,因为图像会拾取 glColor4f() 中定义的颜色。这是我的渲染代码:

-(void)render{
if (texture_m != nil) {
effect_m.texture2d0.enabled = GL_TRUE;
effect_m.texture2d0.envMode = GLKTextureEnvModeReplace;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}

[effect_m prepareToDraw];

glClear(GL_COLOR_BUFFER_BIT);


GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};
GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};


glColor4f( 1, 0, 0, 1 );

glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );


glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}

谁能帮忙解决这个问题谢谢

雷扎

最佳答案

我已经成功解决了我的问题,这是解决方案

-(void)render
{
if (texture_m != nil) {

effect_m.texture2d0.enabled = GL_TRUE;

// here you need to env mode to GLKTextureEnvModeModulate rather than GLKTextureEnvModeReplace

effect_m.texture2d0.envMode = GLKTextureEnvModeModulate;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}

// then here I have added the tint colour to the GLKBaseEffect class as constant colour which I imagine replaces the calls to glColor4f for OpenGL1.1

effect_m.useConstantColor = YES;
float alphaValue = 0.7;
GLKVector4 colour = GLKVector4Make( 0* alphaValue, 1* alphaValue, 1* alphaValue, alphaValue );
effect_m.constantColor = colour;

// remember multiplying the alpha value to each colour component

[effect_m prepareToDraw];

glClear(GL_COLOR_BUFFER_BIT);

GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};

GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};



// glColor4f not necessary
// glColor4f( 1, 0, 0, 1 );

glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);

glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );

glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}

关于iphone - GLKit 和向纹理添加色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7936267/

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