gpt4 book ai didi

ios - GL_TRIANGLE_STRIP 在顶点末端绘制回原点

转载 作者:行者123 更新时间:2023-11-29 01:13:13 26 4
gpt4 key购买 nike

我是 open gl 的 biggener。首先,我只是尝试使用 GL_TRIANGLE_STRIP 绘制一个具有三个顶点的简单三角形。为什么 open gl 在末尾的原点添加额外的点?这是代码片段:

- (void)setUpGL {
glEnable(GL_DEPTH_TEST);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount, sphereTriangleStripVertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), BUFFER_OFFSET(3*sizeof(float)));
}

- (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);

glUseProgram(_program);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount);

}

- (void)getSimpleTriangle:(VertexData **)triangleStripVertexHandle
:(GLuint *)triangleStripVertexCount {

VertexData *triangleStripVertices;
*triangleStripVertexCount = 3;

triangleStripVertices = calloc(*triangleStripVertexCount, sizeof(VertexData));

float y = 1.0f;
float x = 0;
float z = 0;

Vertex3D position = Vertex3DMake(x, y, z);
Vertex3D normal = Vertex3DMake(x, y, z);
Vector3DNormalize(&normal);
triangleStripVertices[0] = VertexDataMake(position, normal);

y = 0.5f;
x = 1;
z = 0;

position = Vertex3DMake(x, y, z);
normal = Vertex3DMake(x, y, z);
Vector3DNormalize(&normal);
triangleStripVertices[1] = VertexDataMake(position, normal);

y = 0.5f;
x = 0.5;
z = 0;

position = Vertex3DMake(x, y, z);
normal = Vertex3DMake(x, y, z);
Vector3DNormalize(&normal);
triangleStripVertices[2] = VertexDataMake(position, normal);

*triangleStripVertexHandle = triangleStripVertices;

}

这是输出:

Output

最佳答案

调用 glDrawArrays 时的第三个参数需要顶点数,而不是字节数。尝试更改:

glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount);

glDrawArrays(GL_TRIANGLE_STRIP, 0, sphereTriangleStripVertexCount);

关于ios - GL_TRIANGLE_STRIP 在顶点末端绘制回原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520597/

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