OpenGL shader program parsing --keypress

This program demonstrates how to write keyboard response functions.

//////////////////////////////////////////////////////////////////////////////
//
//  Triangles.cpp
//
//////////////////////////////////////////////////////////////////////////////
#include <vermilion.h>
#include "vgl.h"
#include "vapp.h"//都是调用库函数减少工作量
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
GLuint  VAOs[NumVAOs];
GLuint  Buffers[NumBuffers];
const GLuint  NumVertices = 6;
BEGIN_APP_DECLARATION(KeyPressExample)//宏定义隐藏了繁琐的KeyPressExample类的实现过程
    virtual void Initialize(const char * title);//需要重定义的一些虚函数
    virtual void Display(bool auto_redraw);
    virtual void Finalize(void);
    virtual void Resize(int width, int height);
    void OnKey(int key, int scancode, int action, int mods);
END_APP_DECLARATION()//宏定义结束
DEFINE_APP(KeyPressExample, "Key Press Example")//声明一个KeyPressExample类对象
//----------------------------------------------------------------------------
//
// init
//
void KeyPressExample::Initialize(const char * title)//初始化加载顶点数组和着色器
{
    base::Initialize(title);
    glGenVertexArrays( NumVAOs, VAOs );
    glBindVertexArray( VAOs[Triangles] );
    GLfloat  vertices[NumVertices][2] = {
        { -0.90f, -0.90f }, {  0.85f, -0.90f }, { -0.90f,  0.85f },  // Triangle 1
        {  0.90f, -0.85f }, {  0.90f,  0.90f }, { -0.85f,  0.90f }   // Triangle 2
    };
    glGenBuffers( NumBuffers, Buffers );
    glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer] );
    glBufferData( GL_ARRAY_BUFFER, sizeof(vertices),
                  vertices, GL_STATIC_DRAW );
    ShaderInfo  shaders[] = {
        { GL_VERTEX_SHADER, "media/shaders/keypress/keypress.vert" },
        { GL_FRAGMENT_SHADER, "media/shaders/keypress/keypress.frag" },
        { GL_NONE, NULL }
    };
    GLuint program = LoadShaders( shaders );
    glUseProgram( program );
    glVertexAttribPointer( vPosition, 2, GL_FLOAT,
                           GL_FALSE, 0, BUFFER_OFFSET(0) );
    glEnableVertexAttribArray( vPosition );
}
void KeyPressExample::OnKey(int key, int scancode, int action, int mods)
{//重载键盘响应函数
    if (action == GLFW_PRESS)
    {
        switch (key)
        {
            case GLFW_KEY_M:
                {
                    static GLenum  mode = GL_FILL;
                    mode = ( mode == GL_FILL ? GL_LINE : GL_FILL );
                    glPolygonMode( GL_FRONT_AND_BACK, mode );//按键m改变绘制模式
                }
                return;
        }
    }
    base::OnKey(key, scancode, action, mods);//调用基类函数
}
//----------------------------------------------------------------------------
//
// display
//
void KeyPressExample::Display(bool auto_redraw)
{
    glClear( GL_COLOR_BUFFER_BIT );
    glBindVertexArray( VAOs[Triangles] );
    glDrawArrays( GL_TRIANGLES, 0, NumVertices );
    base::Display(auto_redraw);
}
void KeyPressExample::Resize(int width, int height)
{
    glViewport(0, 0, width, height);
}
void KeyPressExample::Finalize(void)
{
}

Our code is similar to the previous example, drawing the same graph. This example accepts keyboard messages and changes the drawing mode to wireframe mode when the m key is pressed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525809&siteId=291194637