gpt4 book ai didi

ios - Metal API 不会绘制简单的三角形 后面是 WWDC2014 讲座

转载 作者:行者123 更新时间:2023-11-29 10:22:37 27 4
gpt4 key购买 nike

我听过 WWDC2014 上关于如何使用 Metal API 的讲座,他们正在用一个简单的三角形创建“Hello world”应用程序。我期待在绿色背景上看到一个红色三角形。一切都很好,但三角形本身不存在,我不明白为什么。请帮忙!

这些线是 Metal 着色器:

struct Vertex
{
float3 position;
};

struct VertexOut
{
float4 position [[position]];
float4 color;
};

vertex VertexOut myVertexShader(
device Vertex* vertexArray [[ buffer(0) ]],
unsigned int vid [[ vertex_id ]])
{
VertexOut out;
float3 pos = vertexArray[vid].position;
out.position = float4(pos[0], pos[1], pos[2], 0.0);
out.color = float4(1.0, 0.0, 0.0, 1.0);
return out;
}

fragment float4 myFragmentShader(VertexOut interpolated [[ stage_in ]])
{
return interpolated.color;
}

这些是 View :

@implementation MyView

- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];

if(self)
{
_device = MTLCreateSystemDefaultDevice();
}
NSLog(@"MyView::initWithCoder called");
return self;
}

+(id)layerClass
{
return [CAMetalLayer class];
}

@end

这些是 View Controller :

static const float zcoord = 0.f;
static const float vertexArrayData[] =
{
-0.3, -0.5, zcoord,
0.3, -0.5, zcoord,
0.f, 0.2, zcoord
};

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

MyView* view = (MyView*) self.view;

id<MTLCommandQueue> queue = [view.device newCommandQueue];
id<MTLBuffer> vertexArray = [view.device newBufferWithBytes:vertexArrayData
length:sizeof(vertexArrayData)
options:0];

MTLRenderPipelineDescriptor* desc = [MTLRenderPipelineDescriptor new];
id<MTLLibrary> lib = [view.device newDefaultLibrary];
desc.vertexFunction = [lib newFunctionWithName:@"myVertexShader"];
desc.fragmentFunction = [lib newFunctionWithName:@"myFragmentShader"];
desc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;

id<MTLRenderPipelineState> renderPipeline = [view.device newRenderPipelineStateWithDescriptor:desc error:nil];

id<MTLCommandBuffer> commandBuffer = [queue commandBuffer];
id<CAMetalDrawable> drawable = [(CAMetalLayer*)view.layer nextDrawable];

MTLRenderPassDescriptor* renderDesc = [MTLRenderPassDescriptor new];
renderDesc.colorAttachments[0].texture = drawable.texture;
renderDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
renderDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 1.0, 0.0, 1.0);

id<MTLRenderCommandEncoder> render = [commandBuffer renderCommandEncoderWithDescriptor:renderDesc];
[render setRenderPipelineState:renderPipeline];
[render setVertexBuffer:vertexArray offset:0 atIndex:0];
[render drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
[render endEncoding];

[commandBuffer presentDrawable:drawable];

[commandBuffer commit];
}

最佳答案

我明白了,伙计们。

问题出在这一行:

out.position = float4(pos[0], pos[1], pos[2], 0.0);

float4 的最后一个组成部分不应为零。 1.0 有所帮助。

问候

关于ios - Metal API 不会绘制简单的三角形 后面是 WWDC2014 讲座,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115666/

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