Rendering multiple textures at different points on screen without using point sprites

后端 未结 2 1344
后悔当初
后悔当初 2020-12-20 07:55

I\'m building an ios drawing app and am having a hard time how to draw paint textures on the screen at different points. Most tutorials online refer to rendering a single te

2条回答
  •  感情败类
    2020-12-20 08:41

    I finally got this to work. I drew each point where the user touches as a TRIANGLE_STRIP primitive, two of which together result in a square.

        GLfloat *tBuffer = malloc(4 * 2 * sizeof(GLfloat));
        tBuffer[0] = 0.0;
        tBuffer[1] = 1.0;
        tBuffer[2] = 1.0;
        tBuffer[3] = 1.0;
        tBuffer[4] = 0.0;
        tBuffer[5] = 0.0;
        tBuffer[6] = 1.0;
        tBuffer[7] = 0.0;
        glTexCoordPointer(2, GL_FLOAT, 0, tBuffer);
    
        CGFloat padding = 16;
        vBuffer[0] = xCenter - padding;
        vBuffer[1] = yCenter - padding;
    
        vBuffer[2] = xCenter + padding;
        vBuffer[3] = yCenter - padding;
    
        vBuffer[4] = xCenter - padding;
        vBuffer[5] = yCenter + padding;
    
        vBuffer[6] = xCenter + padding;
        vBuffer[7] = yCenter + padding;
    
        CGFloat degrees = atan2(end.y - start.y, end.x - start.x) * 180 / M_PI;
    
        // rotate the texture in the direction of the stroke.
        glMatrixMode(GL_TEXTURE);
        glPushMatrix();
        glTranslatef(0.5, 0.5, 0);
        glRotatef(degrees, 0, 0, 1);
        glTranslatef(-0.5, -0.5, 0);
        glVertexPointer(2, GL_FLOAT, 0, vBuffer);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
    

    xCenter, yCenter is where the user touches. Padding determines the size of the primitive.

    I also rotate the texture, but translate it to the center before rotating since rotation happens with the pivot at the origin otherwise.

    Hope this helps!

    UPDATE:

    I was able to reduce the number of openGL calls by 1 by running the following set of commands for rotation instead:

    Note that I was able to reduce the number of OpenGL calls by 1, by running the following set of commands for the rotation (removing push/pop matrix)

        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
        glTranslatef(0.5, 0.5, 0);
        [GLManager rotateBrush:degrees];
        glTranslatef(-0.5, -0.5, 0);
        [GLManager drawVertexBuffer:vBuffer withVertexNumber:4];
        glMatrixMode(GL_MODELVIEW);`
    

提交回复
热议问题