在 OpenGL 中使用三角形绘制字母?

Drawing letters using triangles in OpenGL?

我需要用三角形画出我的名字。我了解如何处理着色器。我只是对如何实际绘制对象并将它们连接起来组成字母感到困惑。

我得到了一些代码可以使用:

#include "Angel.h"

const int NumPoints = 50000;


/*This function initializes an array of 3d vectors 
   and sends it to the graphics card along with shaders
   properly connected to them.*/

void
init( void )
{
    vec3 points[NumPoints];

    // Specifiy the vertices for a triangle
    vec3 vertices[] = {
        vec3( -1.0, -1.0, 0.0 ),
        vec3(  0.0,  1.0, 0.0 ),
        vec3(  1.0, -1.0, 0.0 )
    };

    // Select an arbitrary initial point inside of the triangle
    points[0] = vec3( 0.0, 1.0, 0.0 );

    // compute and store NumPoints - 1 new points
    for ( int i = 1; i < NumPoints; ++i ) {
        int j = rand() % 3;   // pick a vertex from the triangle at random

        // Compute the point halfway between the selected vertex
        //   and the previous point
        points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
    }

    // Create a vertex array object
    GLuint vao;  //just an integer recognized by graphics card
    glGenVertexArrays( 1, &vao ); //generate 1 buffer
    glBindVertexArray( vao ); //become array buffer

    // Create and initialize a buffer object //sends it to graphics card
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW ); //size of array glstatic draw means this point isnt gonna change its static

   // Load shaders and use the resulting shader program

    GLuint program = InitShader("simpleShader - Copy.vert", "simpleShader - Copy.frag");
    // make these shaders the current shaders
    glUseProgram( program );

    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation( program, "vPosition" ); //find the location in the code
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

    glClearColor( 0.5, 0.5, 0.5, 1.0 ); // gray background
}

//----------------------------------------------------------------------------
/* This function handles the display and it is automatically called by GLUT
   once it is declared as the display function. The application should not
   call it directly.
*/

void
display( void )
{
    glClear( GL_COLOR_BUFFER_BIT );             // clear the window
    glDrawArrays( GL_POINTS, 0, NumPoints );    // draw the points
    glFlush();                                  // flush the buffer
}

//----------------------------------------------------------------------------
/* This function handles the keyboard and it is called by GLUT once it is 
   declared as the keyboard function. The application should not call it
   directly.
*/

void
keyboard( unsigned char key, int x, int y )
{
    switch ( key ) {
    case 033:                   // escape key
        exit( EXIT_SUCCESS );   // terminates the program
        break;
    }
}

//----------------------------------------------------------------------------
/* This is the main function that calls all the functions to initialize
   and setup the OpenGL environment through GLUT and GLEW.
*/

int
main( int argc, char **argv )
{
    // Initialize GLUT
    glutInit( &argc, argv );
    // Initialize the display mode to a buffer with Red, Green, Blue and Alpha channels
    glutInitDisplayMode( GLUT_RGBA );
    // Set the window size
    glutInitWindowSize( 512, 512 );
    // Here you set the OpenGL version
    glutInitContextVersion( 3, 2 );
    //Use only one of the next two lines
    //glutInitContextProfile( GLUT_CORE_PROFILE );
    glutInitContextProfile( GLUT_COMPATIBILITY_PROFILE );
    glutCreateWindow( "Simple GLSL example" );

    // Uncomment if you are using GLEW
    glewInit(); 

    // initialize the array and send it to the graphics card
    init();

    // provide the function that handles the display
    glutDisplayFunc( display );
    // provide the functions that handles the keyboard
    glutKeyboardFunc( keyboard );
    glutMainLoop();
    return 0;
}

您的问题似乎是为了让您熟悉设置和使用顶点缓冲区。如果是这样,那么您如何想出三角形并不重要 - 关键是要了解设置的工作原理。

因此,您需要做的第一件事就是阅读有关该主题的教科书。如果您没有文本,则需要在 OpenGL 参考中查找图形调用。

如果您按原样 运行 程序,它应该绘制一堆随机选择的、断开连接的点(以分形的形式,但仍然...)。这是因为实际的绘制调用 glDrawArrays() 是用枚举值 GL_POINTS 调用的,它告诉它绘制点,作为它的第一个参数。

如果您阅读 glDrawArrays() 的文档,它应该列出此参数的其他值,其中一些以各种方式绘制三角形。其中最直接的是 GL_TRIANGLES,但我建议您查看所有这些以了解您的选择。

生成三角形由您决定。如果您的名字很短,手动生成它们应该相当容易。请注意,您应该完全替换随机点生成代码;选项包括:

  • 使用内联数据
  • 使用一些代码从文件加载坐标
  • 有一些更聪明的东西,所以你不必手动生成它们