gpt4 book ai didi

ios - 使用指针设置顶点不起作用

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

我目前正在使用 OpenGL ES 2.0 研究一些 OpenGL 基础知识。

如果我将顶点设置为顶部的常量,则会绘制三角形;如果我尝试使用指针在方法中设置它们,则不会绘制任何内容。我错过了什么?

#import "CometGLViewController.h"

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

//vertices store information for each "point" used to draw a triangle
Vertex* Vertices; // NOT DRAWING
//const Vertex Vertices[] = {
// {{0, 0, 0},{0, 0, 0, 1}},
// {{1, 1, 0}, {1, 0, 0, 1}},
// {{0, 1, 0}, {1, 0, 0, 1}},
//}; // THIS WORKS

//Used to reuse vertices
const GLubyte Indices[] = {
0, 1, 2,
};

...一些其他的东西

- (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.drawableMultisample = GLKViewDrawableMultisample4X;
[self setupVertices];
[self setupGL];
}

...

#pragma mark - GLKViewDelegate

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(_curRed, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

[self.effect prepareToDraw];

glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

...

#pragma mark - OpenGL stuff
- (void)setupGL {

[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);

glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
// ----- create new buffer, work with "vertexBuffer", glBufferData sends data for opengl usage
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);

// ----- Setup vertices attributes
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));
}

- (void)tearDownGL {

[EAGLContext setCurrentContext:self.context];

glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
glDeleteVertexArraysOES(1, &_vertexArray);

self.effect = nil;

}

- (void)setupVertices
{ // test
int points = 3;
Vertices = malloc(sizeof(Vertex) * points);
Vertex v = {{0, 0, 0}, {0, 0, 0, 1}};
Vertices[0] = v;
Vertex v1 = {{1, 1, 0}, {0, 1, 0, 1}};
Vertices[1] = v1;
Vertex v2 = {{0, 1, 1}, {1, 0, 0, 1}};
Vertices[2] = v2;
}

@end

最佳答案

改变

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * 3, Vertices, GL_STATIC_DRAW);

您的问题是 sizeof(vertices) 对指针的工作方式不同于对分配在堆栈上的数组的工作方式。

关于ios - 使用指针设置顶点不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19319886/

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