使用OpenGL在矩形上方绘制倒三角形

时间:2018-09-19 05:36:20

标签: c++ opengl glew freeglut

这是目标:

goal

我可以通过以下方式获得倒三角形:

GLfloat vertices[][4] = {
    {-0.75, 0.5, 0.0, 1.0}, 
    {0.75, 0.5, 0.0, 1.0},
    {0.0, -0.75, 0.0, 1.0}};

和一些带有以下内容的矩形:

GLfloat vertices[][4] = {
    {-0.9, 0.0, 0.0, 1.0},
    {0.9, 0.0, 0.0, 1.0},
    {-0.9, -0.9, 0.0, 1.0}};

当我尝试添加第四个顶点({0.0, -0.9, 0.0, 1.0})时,它失去了形式,并在左上角创建了一个瘦矩形,如下所示:

this is what I get

当我尝试同时绘制它们时,出现黑屏。

如何绘制目标图像?

完整代码:

#include <stdlib.h>
#include <GL/glew.h>   
#include <GL/freeglut.h>

#define BUFFER_OFFSET(offset) ((GLvoid *) offset)

GLuint buffer = 0;
GLuint vPos;
GLuint program;

void init()
{
    // Three vertexes that define a triangle. 
    GLfloat vertices[][ 4 ] =
    {
        {-0.75, -0.5, 0.0, 1.0},
        {0.75, -0.5, 0.0, 1.0},
        {0.0, 0.75, 0.0, 1.0}
    };

    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );

    // OpenGL vertex shader source code
    const char* vSource =
    {
        "#version 330\n"
        "in vec4 vPos;"
        "void main() {"
        "   gl_Position = vPos * vec4(1.0f, 1.0f, 1.0f, 1.0f);"
        "}"
    };

    // OpenGL fragment shader source code
    const char* fSource =
    {
        "#version 330\n"
        "out vec4 fragColor;"
        "void main() {"
        "   fragColor = vec4(0.8, 0.8, 0, 1);"
        "}"
    };

    // Declare shader IDs
    GLuint vShader, fShader;

    // Create empty shader objects
    vShader = glCreateShader( GL_VERTEX_SHADER );
    fShader = glCreateShader( GL_FRAGMENT_SHADER );

    glShaderSource( vShader, 1, &vSource, NULL );
    glShaderSource( fShader, 1, &fSource, NULL );

    glCompileShader( vShader );
    glCompileShader( fShader );

    // Create an empty shader program object
    program = glCreateProgram();

    // Attach vertex and fragment shaders to the shader program
    glAttachShader( program, vShader );
    glAttachShader( program, fShader );

    // Link the shader program
    glLinkProgram( program );

    vPos = glGetAttribLocation( program, "vPos" );

    // Specify the background color
    glClearColor( 0, 0, 0, 1 );
}

void reshape( int width, int height )
{
    // Specify the width and height of the picture within the window
    glViewport( 0, 0, width, height );
}

void display()
{
    // Clear the window with the background color
    glClear( GL_COLOR_BUFFER_BIT );

    // Activate the shader program
    glUseProgram( program );

    glBindBuffer( GL_ARRAY_BUFFER, buffer );

    glVertexAttribPointer( vPos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET( 0 ) );

    glEnableVertexAttribArray( vPos );

    // Start the shader program
    glDrawArrays( GL_TRIANGLES, 0, 3 );

    // Refresh the window
    glutSwapBuffers();
}

void main( int argc, char *argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutCreateWindow( argv[ 0 ] );
    glewInit();
    init();
    glutDisplayFunc( display );
    glutReshapeFunc( reshape );
    glutMainLoop();
}

0 个答案:

没有答案