gpt4 book ai didi

opengl-es - 为什么我得到 "for static data use buffer objects to store the indices warning"

转载 作者:行者123 更新时间:2023-12-02 03:37:49 26 4
gpt4 key购买 nike

我正在使用 GLKit 制作一个 iPhone 应用程序。我从 Blender 的 .obj 文件加载顶点,然后尝试绘制顶点。这是我第一次尝试使用 glDrawElements 这样做。我选择这个是因为我只需要与我的对象中的顶点一样多的顶点,并且我可以只渲染一个带有这些顶点索引的三角形带。

这是我尝试做的简化版本,它只是强调了我试图在 ViewController 之外绘制的事实the entire source code for this simplified version is here

#import "Character.h"

@interface Character()
{
GLuint _vertexBuffer;
GLuint _indexBuffer;
GLuint _vertexArray;
}

@property(nonatomic, weak) GLKBaseEffect *effect;

@end

typedef struct {
float Position[3];
float Color[4];
} Vertex;

const Vertex Vertices[] = {
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {0, 1, 0, 1}},
{{-1, 1, 0}, {0, 0, 1, 1}},
{{-1, -1, 0}, {0, 0, 0, 1}}
};

const GLushort Indices[] = {
0, 1, 2,
2, 3, 0
};

@implementation Character

- (id)initWithEffect:(GLKBaseEffect *)effect
{
if (self = [super init])
{
self.effect = effect;
[self setupGL];
}

return self;
}

- (void)setupGL
{
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);


glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);



glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));



glBindVertexArrayOES(0);

}

- (void)teardownGL
{
glDeleteBuffers(1, &_vertexBuffer);

glDeleteBuffers(1, &_indexBuffer);

}

- (void)render
{
self.effect.transform.modelviewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.position.x, self.position.y, self.position.z);
self.effect.transform.modelviewMatrix = GLKMatrix4Rotate(self.effect.transform.modelviewMatrix, self.rotation, 0.0f, 0.0f, 1.0f);

[self.effect prepareToDraw];

glBindVertexArrayOES(_vertexArray);

glDrawElements(GL_TRIANGLES, sizeof(Indices) / sizeof(Indices[0]), GL_UNSIGNED_SHORT, (const GLvoid*)0);

}

@end

这是在 ViewControllerviewDidLoad 中初始化角色的方式

- (void)viewDidLoad
{
[super viewDidLoad];

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

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


[self setupGL];
character = [[Character alloc] initWithEffect:self.effect];
character.position = GLKVector3Make(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2, 0.0f);
}

下面是如何调用角色的渲染方法:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[character render];
}

这是我的项目结构project structure:

最佳答案

警告意味着您应该为静态数据使用 VBO(顶点缓冲区对象)。从您的代码中,我看到您正在使用正确的 GL_ELEMENT_ARRAY_BUFFER 创建索引缓冲区 VBO,但是您在调用渲染时没有设置它。

VAO 不存储索引缓冲区数据,因此您不应在 VAO 创建代码中执行 GL_ELEMENT_ARRAY_BUFFER 工作。

就在调用[字符渲染]之前,调用 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);并且警告应该消失。

关于opengl-es - 为什么我得到 "for static data use buffer objects to store the indices warning",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22266812/

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