What is the fastest way to draw a 2D array of color triplets on screen?

前端 未结 6 1405
星月不相逢
星月不相逢 2021-02-14 18:24

The target language is C/C++ and the program has only to work on Linux, but platform independent solutions are preferred obviously. I run Xorg, XVideo and OpenGL are available.<

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 18:33

    I did this a while back using C and OpenGL, and got very good performance by creating a full screen sized quad, and then use texture mapping to transfer the bitmap onto the face of the quad.

    Here's some example code, hope you can make use of it.

    #include 
    #include 
    
    #define WIDTH 1024
    #define HEIGHT 768
    
    unsigned char texture[WIDTH][HEIGHT][3];             
    
    void renderScene() {    
    
        // render the texture here
    
        glEnable (GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
        glTexImage2D (
            GL_TEXTURE_2D,
            0,
            GL_RGB,
            WIDTH,
            HEIGHT,
            0,
            GL_RGB,
            GL_UNSIGNED_BYTE,
            &texture[0][0][0]
        );
    
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0, -1.0);
            glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0, -1.0);
            glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0,  1.0);
            glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0,  1.0);
        glEnd();
    
        glFlush();
        glutSwapBuffers();
    }
    
    int main(int argc, char **argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(WIDTH, HEIGHT);
        glutCreateWindow(" ");
    
        glutDisplayFunc(renderScene);
    
        glutMainLoop();
    
        return 0;
    }
    

提交回复
热议问题