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

时间:2018-09-06 17:55:49

标签: c++ opengl glew freeglut

我必须用三角形画出我的名字。我了解如何处理着色器。我只是对如何实际绘制对象并将其连接以形成字母感到困惑。

我已经获得了一些可以使用的代码:

#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;
}

1 个答案:

答案 0 :(得分:2)

您的问题似乎旨在使您熟悉设置和使用顶点缓冲区。如果是这样,那么如何得出三角形都没关系-关键是要了解设置的工作原理。

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

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

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

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

  • 具有内联数据
  • 使用一些代码从文件加载坐标
  • 具有更巧妙的功能,因此您无需手工生成它们