3

好的,我一遍又一遍地检查,到目前为止,我“认为”我了解 opengl 是如何绘制的以及如何指定我想要绘制的内容,但是我似乎无法让它绘制超过 1 条线。

使用 Xcode 4.2 中的模板,我可以获得以下代码。它确实使用 GLKIT 绘制了我的方形骨架,因为我只是忽略了 opengl es 2.0 方法不使用着色器 atm,但是当我触摸屏幕时它应该绘制我自己的数组,它没有......

所以首先是方形格式(只是为了显示他们在模板中使用的变量类型)

GLfloat gCubeVertexData[216] = 
{
    // Data layout for each line below is:
    // positionX, positionY, positionZ,     normalX, normalY, normalZ,
    0.5f, -0.5f, -0.5f,        1.0f, 0.0f, 0.0f,
    0.5f, 0.5f, -0.5f,         1.0f, 0.0f, 0.0f,
    0.5f, -0.5f, 0.5f,         1.0f, 0.0f, 0.0f,
    0.5f, -0.5f, 0.5f,         1.0f, 0.0f, 0.0f,
    0.5f, 0.5f, 0.5f,          1.0f, 0.0f, 0.0f,
    0.5f, 0.5f, -0.5f,         1.0f, 0.0f, 0.0f,

    0.5f, 0.5f, -0.5f,         0.0f, 1.0f, 0.0f,
    -0.5f, 0.5f, -0.5f,        0.0f, 1.0f, 0.0f,
    0.5f, 0.5f, 0.5f,          0.0f, 1.0f, 0.0f,
    0.5f, 0.5f, 0.5f,          0.0f, 1.0f, 0.0f,
    -0.5f, 0.5f, -0.5f,        0.0f, 1.0f, 0.0f,
    -0.5f, 0.5f, 0.5f,         0.0f, 1.0f, 0.0f,

    -0.5f, 0.5f, -0.5f,        -1.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, -0.5f,       -1.0f, 0.0f, 0.0f,
    -0.5f, 0.5f, 0.5f,         -1.0f, 0.0f, 0.0f,
    -0.5f, 0.5f, 0.5f,         -1.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, -0.5f,       -1.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, 0.5f,        -1.0f, 0.0f, 0.0f,

    -0.5f, -0.5f, -0.5f,       0.0f, -1.0f, 0.0f,
    0.5f, -0.5f, -0.5f,        0.0f, -1.0f, 0.0f,
    -0.5f, -0.5f, 0.5f,        0.0f, -1.0f, 0.0f,
    -0.5f, -0.5f, 0.5f,        0.0f, -1.0f, 0.0f,
    0.5f, -0.5f, -0.5f,        0.0f, -1.0f, 0.0f,
    0.5f, -0.5f, 0.5f,         0.0f, -1.0f, 0.0f,

    0.5f, 0.5f, 0.5f,          0.0f, 0.0f, 1.0f,
    -0.5f, 0.5f, 0.5f,         0.0f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.5f,         0.0f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.5f,         0.0f, 0.0f, 1.0f,
    -0.5f, 0.5f, 0.5f,         0.0f, 0.0f, 1.0f,
    -0.5f, -0.5f, 0.5f,        0.0f, 0.0f, 1.0f,

    0.5f, -0.5f, -0.5f,        0.0f, 0.0f, -1.0f,
    -0.5f, -0.5f, -0.5f,       0.0f, 0.0f, -.0f,
    0.5f, 0.5f, -0.5f,         0.0f, 0.0f, -1.0f,
    0.5f, 0.5f, -0.5f,         0.0f, 0.0f, -1.0f,
    -0.5f, -0.5f, -0.5f,       0.0f, 0.0f, -1.0f,
    -0.5f, 0.5f, -0.5f,        0.0f, 0.0f, -1.0f
};

这是我在“View did Load”函数中调用一次的 SetupGL。

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];

    [self loadShaders];

    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.light0.enabled = GL_TRUE; //No Idea why it wont work without this


    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

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

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));

    glBindVertexArrayOES(0);
}

这是更新方法(只是让东西旋转)

- (void)update
{
    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 10000.0f);

    self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -10.0f);
    baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);

    // Compute the model view matrix for the object rendered with GLKit
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
    modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);

    self.effect.transform.modelviewMatrix = modelViewMatrix;

    _rotation += self.timeSinceLastUpdate * 0.5f;
}

Draw In Rect 方法(我认为的渲染)

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

    glBindVertexArrayOES(_vertexArray);

    // Render the object with GLKit
    [self.effect prepareToDraw];

    glDrawArrays(GL_LINE_STRIP, 0, 36); //Probably have to change the 36

}

这是触摸部分:

在开始接触时,我只是在创建 NSMutableData

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self.view];

    vertexData3 = [NSMutableData dataWithLength: sizeof(GLKVector2)]; //HERE

    if (!referenceAttitude)
        referenceAttitude = motionManager.deviceMotion.attitude;

    writtingON = YES;   

}

在 Touches Ended 时,我正在尝试使用我得到的新顶点进行绘制。顶点是使用触发运动管理器读数的计时器获得的,在用户触摸屏幕的位置进行一些数学运算,因此值应该在屏幕坐标的范围内。

但是为了测试绘图,我只是创建了一个临时 NSMUtableData 变量,其中包含一些我手动添加的值(添加太可怕了)。我正在使用 NSMutableData,因为这就是他们在另一个教程中的建议。我 90% 确定它正确保存了数据,因为当我检索它时它就在那里。(http://games.ianterrell.com/how-to-draw-2d-shapes-with-glkit/

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{


    ////////Test array


    tempVert = GLKVector2Make(10, -8);
    NSRange r = {0*sizeof(GLKVector2), sizeof(GLKVector2)};
    [vertexData3 replaceBytesInRange: r withBytes:&tempVert];
    [vertexData3 increaseLengthBy:sizeof(GLKVector2)];

    tempVert = GLKVector2Make(2, -8);
    NSRange q = {1*sizeof(GLKVector2), sizeof(GLKVector2)};
    [vertexData3 replaceBytesInRange: q withBytes:&tempVert];
    [vertexData3 increaseLengthBy:sizeof(GLKVector2)];

    tempVert = GLKVector2Make(4, 9);
    NSRange w = {2*sizeof(GLKVector2), sizeof(GLKVector2)};
    [vertexData3 replaceBytesInRange: w withBytes:&tempVert];
    [vertexData3 increaseLengthBy:sizeof(GLKVector2)];

    tempVert = GLKVector2Make(1, -9);
    NSRange e = {3*sizeof(GLKVector2), sizeof(GLKVector2)};
    [vertexData3 replaceBytesInRange: e withBytes:&tempVert];
    [vertexData3 increaseLengthBy:sizeof(GLKVector2)];

    tempVert = GLKVector2Make(2, -2);
    NSRange t = {4*sizeof(GLKVector2), sizeof(GLKVector2)};
    [vertexData3 replaceBytesInRange: t withBytes:&tempVert];
    [vertexData3 increaseLengthBy:sizeof(GLKVector2)];


    //////////////////////

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData3), [vertexData3 mutableBytes], GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

    glBindVertexArrayOES(0);

}

最后一部分是让我发疯的部分!我认为它应该做的是,用我自己的数组修改缓冲区,以便自动更新绘制它。

任何建议将不胜感激。

谢谢

哦,如果有人知道如何设置正交矩阵,因为我真的只对二维组件 atm 感兴趣,所以我不能用透视矩阵替换它。

编辑:

好的,我只是尝试发送一个随机的 GLfloat 数组,它确实改变了形状,但为什么不使用我的 NSMutableData 数组呢?我以为我正在做与教程中完全相同的程序....

4

1 回答 1

0

最后一部分应该是:

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData3), [vertexData3 mutableBytes], GL_DYNAMIC_DRAW);
// bind buffer and specify it's new data (no need to glGenBuffers(), that was done in setupGL())
// also, it would be handy to save number of vertices so it could be used later for drawing (nVertices = sizeof(vertexData3) / sizeof(GLKVector2))

glBindVertexArrayOES(_vertexArray);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);
// bind vertex array and setup the vertex attribute (although this could be done only once, in setupGL())

这样,您就不会生成这么多的 OpenGL 对象,并且渲染应该更快(而且,不会耗尽内存)。

否则,渲染似乎没问题。但是你真的应该使用实际的顶点数来绘制:

glDrawArrays(GL_LINE_STRIP, 0, nVertices);

几乎可以肯定规模存在问题(GLfloats 和 GLKVector2 之间应该没有区别)。您通过您的方法生成了一些点,但只绘制了前 36 个点。可能有一条曲线生成了正确的,但您只看到了它的开头的一小部分,这自然看起来是平的。此外,您似乎不确定您的位置计算是否会在窗口内生成坐标,也许 a) 在此处发布您的源 b) 将您的坐标乘以一些小的(但高于零)值,以便它们适合屏幕。

至于正交,您可以使用:

GLKMatrix4MakeOrtho(-1, 1, -1, 1, .1, 1000); // this sets normalized coordinates
GLKMatrix4MakeOrtho(0, widthPx, 0, heightPx, .1, 1000); // this sets pixel coordinates

GLKMatrix4MakeOrtho 的参数只是屏幕角落(左、右、下、上)的所需坐标和正在绘制的深度范围(近、远)。

我希望这至少能给你一些见解......

于 2012-01-20T09:47:26.457 回答