gpt4 book ai didi

iphone - 我们如何为 GLFloat 赋值以在 iPhone 应用程序中画一条线?

转载 作者:行者123 更新时间:2023-11-28 17:35:50 27 4
gpt4 key购买 nike

最后Mr.Chris 帮我用GLKview 和drawInRect: 方法画了一条线。它工作正常。但是,我需要一些说明,

  1. 我们如何为 GLFloat 赋值?

    const GLfloat line[] = 
    {
    -1.0f, -1.5f, //point A : What is -1.0f and -1.5f ? These are x and y or something.
    1.5f, -1.0f, //point B : What is 1.5f and -1.0f ? These are x and y or something.
    };

因为,我很难在这里设置一个值。它是如何取 x、y 或长度的?如果这是一个愚蠢的问题,请接受我的道歉。请澄清我对此的疑问。

  1. 如何为 GLKViewController 设置背景图片?我有一张图片可以设置背景,但我不知道我需要在哪里设置它?

示例 viewDidLoad 代码

    - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GameDesign03.png"]];

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

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

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

[self setupGL];
}

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

[self.effect prepareToDraw]; // Prepare the effect for rendering

const GLfloat line[]=
{
-1.0f, -1.5f,
1.5f, -1.0f
};


GLuint bufferObjectNameArray; //Create an handle for a buffer object array

glGenBuffers(1, &bufferObjectNameArray); //Have OpenGL generate a buffer name and store it in the buffer object array

glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray); //Bind the buffer object array to the GL_ARRAY_BUFFER target buffer

//Send the line data over to the target buffer in GPU RAM
glBufferData(GL_ARRAY_BUFFER, sizeof(line), line, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition); //Enable vertex data to be fed down the graphics pipeline to be drawn

glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 2, NULL); //Specify how the GPU looks up the data

glDrawArrays(GL_LINES, 0, 2); // render

}

请帮忙解决这个问题。提前致谢。

最佳答案

所以要回答你的第一个问题,在这一行:

glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 2, NULL); //Specify how the GPU looks up the data 

您将 size 参数设置为 2,这告诉 OpenGL 每个顶点属性有 2 个分量。它们是直线的端点。

要回答您的第二个问题,您需要在绘制线条之前绘制背景图像,如我在上面的评论中所述。

关于iphone - 我们如何为 GLFloat 赋值以在 iPhone 应用程序中画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849430/

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