QT_OPENGL-------- 3.ElementArraryBuffer

Posted fuhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT_OPENGL-------- 3.ElementArraryBuffer相关的知识,希望对你有一定的参考价值。

与上一节内容基本相同,只是用ElementArraryBuffer绘制三角形,也就是VBO与IBO。

1.VBO 一系列点,通过glDrawArrays指定绘制几个点,是连续的,不能跳跃。
2.IBO(ElementArrayBufferObject) 一系列点,通过指定其中的某一些点组成三角形,不需要是连续的。
gldrawElement(绘制什么,绘制数据的个数,类型,绘制的起始位置)

3.其他与上一节相同,代码部分如下:

#include<GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include<iostream>
#include<glm/glm.hpp>
#include<glm/ext.hpp>
using namespace std;
struct Vertex
{
    float pos[3];
    float color[4];
};

char *LoadFileContent(const char*path)//加载shader
{
    FILE*pFile = fopen(path, "rb");
    if (pFile)
    {
        fseek(pFile, 0, SEEK_END);
        int nLen = ftell(pFile);
        char*buffer = new char[nLen+1];
        rewind(pFile);
        fread(buffer, nLen , 1, pFile);
        buffer[nLen]=;
        fclose(pFile);
        return buffer;
    }
    fclose(pFile);
    return nullptr;
}

GLuint CreateGPUProgram(const char*vsShaderPath, const char*fsShaderPath)//编译shader
{
    GLuint vsShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER);
    const char* vsCode = LoadFileContent(vsShaderPath);
    const char* fsCode = LoadFileContent(fsShaderPath);
    glShaderSource(vsShader, 1, &vsCode, nullptr);
    glShaderSource(fsShader, 1, &fsCode, nullptr);//ram -> vram
    glCompileShader(vsShader);
    glCompileShader(fsShader);
    GLuint program = glCreateProgram();
    glAttachShader(program, vsShader);
    glAttachShader(program, fsShader);
    glLinkProgram(program);
    glDetachShader(program,vsShader);
    glDetachShader(program, fsShader);
    glDeleteShader(vsShader);
    glDeleteShader(fsShader);
    return program;
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window‘s context current */
    glfwMakeContextCurrent(window);

      //shader数据
    GLenum status = glewInit();

    if (status != GLEW_OK)
    {
        cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
        std::cout << "Error::GLEW glew version:" << glewGetString(GLEW_VERSION)
            << " error string:" << glewGetErrorString(status) << std::endl;
        glfwTerminate();
        std::system("pause");
       // return -1;
    }
        GLuint program = CreateGPUProgram("/home/jun/Qt_Preject/opengl/shader/materials/sample.vs", "/home/jun/Qt_Preject/opengl/shader/materials/sample.fs");
        //取得shader变量的位置
        GLint posLocation, colorLocation, MLocation, VLocation, PLocation;
        posLocation = glGetAttribLocation(program, "pos");
        colorLocation = glGetAttribLocation(program, "color");

        MLocation = glGetUniformLocation(program, "M");
        VLocation = glGetUniformLocation(program, "V");
        PLocation = glGetUniformLocation(program, "P");

        Vertex vertex[3];
        vertex[0].pos[0] = 0;
        vertex[0].pos[1] = 0;
        vertex[0].pos[2] = -100.0f;
        vertex[0].color[0] = 1.0f;
        vertex[0].color[1] = 1.0f;
        vertex[0].color[2] = 1.0f;
        vertex[0].color[3] = 1.0f;

        vertex[1].pos[0] = 10;
        vertex[1].pos[1] = 0;
        vertex[1].pos[2] = -100.0f;
        vertex[1].color[0] = 1.0f;
        vertex[1].color[1] = 1.0f;
        vertex[1].color[2] = 1.0f;
        vertex[1].color[3] = 1.0f;

        vertex[2].pos[0] = 0;
        vertex[2].pos[1] = 10;
        vertex[2].pos[2] = -100.0f;
        vertex[2].color[0] = 1.0f;
        vertex[2].color[1] = 1.0f;
        vertex[2].color[2] = 1.0f;
        vertex[2].color[3] = 1.0f;
     //vbo上传shader数据到GPU
        GLuint vbo;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 3, vertex, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
     //ibo上传shader数据到GPU
        unsigned int indexes[] = {0,1,2};
        GLuint ibo;
        glGenBuffers(1, &ibo);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3, indexes, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);



        float identity[] = {
            1,0,0,0,
            0,1,0,0,
            0,0,1,0,
            0,0,0,1
        };
        glm::mat4 projection = glm::perspective(45.0f, 800.0f / 600.0f, 0.1f, 1000.0f);


    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Draw a triangle */
        glUseProgram(program);
        glUniformMatrix4fv(MLocation, 1, GL_FALSE, identity);
        glUniformMatrix4fv(VLocation, 1, GL_FALSE, identity);
        glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glEnableVertexAttribArray(posLocation);
        glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
        glEnableVertexAttribArray(colorLocation);
        glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float)*3));

        //glDrawArrays(GL_TRIANGLES, 0, 3);
       //glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


        glUseProgram(0);
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

效果和上一节一样。



以上是关于QT_OPENGL-------- 3.ElementArraryBuffer的主要内容,如果未能解决你的问题,请参考以下文章

elementUI , echarts问题总结

java集合进阶ArrayList源码