gpt4 book ai didi

ios - Opengl 2.0 glColor4f 不工作

转载 作者:行者123 更新时间:2023-12-01 17:52:58 25 4
gpt4 key购买 nike

我正在使用 GLKit 创建一个 View 以在 iOS 中试验 opengl。该 View 具有以下加载方法:

- (void)viewDidLoad
{
[super viewDidLoad];

self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

if (!self.context) {
NSLog(@"Failed to create ES context");
}

GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];

}

在形状的绘制方法中,我有以下代码:
float vertices[] = {
-1, -1,
1, -1,
0, 1};

glEnableVertexAttribArray(GLKVertexAttribPosition);

glColor4f(0.0, 1.0, 0.0, 1.0);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices));

glDisableVertexAttribArray(GLKVertexAttribPosition);

此代码将在屏幕中心绘制一个三角形,但三角形是白色的。我使用行 glColor4f(0.0, 1.0, 0.0, 1.0) 将当前颜色设置为绿色但这不会改变当前的绘图颜色。如何更改三角形的颜色?

最佳答案

OpenGL 2.0 使用着色器来光栅化几何体。你需要告诉你的着色器你想看到什么颜色。这样做的方法通常是将顶点的颜色属性设置为您想要的,我没有使用过 GLKit 但我看到它有一个 GLKVertexAttribColor .尝试将其设置为您想要的颜色:

GLfloat triangle_colors[] = {
1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
};

glVertexAttribPointer(
GLKVertexAttribColor, // attribute
4, // number of elements per vertex, here (r,g,b,a
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
triangle_colors
);

关于ios - Opengl 2.0 glColor4f 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23794053/

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