我是靠谱客的博主 忐忑柚子,最近开发中收集的这篇文章主要介绍opengl入门(五)使用indices复用vertex,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

上一节中,使用了6个顶点来控制显卡的绘制顺序,但是可以发现,vertex中存在多个顶点的定义,有没有一种办法能够顶点只负责定义顶点,另外一个参数能够指导显卡绘制。

vertex定义

修改原来的6个节点成4个节点。

 float positions[] = {
	-0.5f,-0.5f,//0
	0.5f,-0.5f,//1
	0.5f,0.5f,//2
	-0.5f,0.5f,//3
};

//存入显存的大小修改为:
glBufferData(GL_ARRAY_BUFFER,8*sizeof(float),positions,GL_STATIC_DRAW);

建立indices

  1. 使用一个数组来定义显卡绘制的顺序,这里的数字指的就是上面数组的下标。
unsigned int indices[] = {
    0,1,2,2,3,0
};
  1. 设置opengl,让显卡按照indices去绘制
unsigned int ibo; //index buffer object
glGenBuffers(1,&ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,6*sizeof(unsigned int),indices,GL_STATIC_DRAW);
  1. 修改绘制函数
//glDrawArrays(GL_TRIANGLES,0,6); //绘制之前
 glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);//修改之后

整体代码

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

static unsigned int CompileShader(const unsigned int type,const std::string& source)
{
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id,1,&src,nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id,GL_COMPILE_STATUS,&result);
    if(result == GL_FALSE){
        int length;
        glGetShaderiv(id,GL_INFO_LOG_LENGTH,&length);
        char* messge = (char * )alloca(length * sizeof(char));
        glGetShaderInfoLog(id,length,&length,messge);
        std::cout << "fild to compile" <<  (type == GL_VERTEX_SHADER?"vertex":"fragment") << std::endl;
        std::cout << messge << std::endl;
    }

    return id;
}

static int CreateShader(const std::string& vertexShader,const std::string& fragmentShader)
{
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER,vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER,fragmentShader);

    glAttachShader(program,vs);
    glAttachShader(program,fs);

    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);
    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(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

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

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        printf("Error: %sn", glewGetErrorString(err));
    }

    float positions[] = {
        -0.5f,-0.5f,//0
        0.5f,-0.5f,//1
        0.5f,0.5f,//2
        -0.5f,0.5f,//3
    };

    unsigned int indices[] = {
        0,1,2,2,3,0
    };

    unsigned int buffer;
    glGenBuffers(1,&buffer);
    glBindBuffer(GL_ARRAY_BUFFER,buffer);
    glBufferData(GL_ARRAY_BUFFER,8*sizeof(float),positions,GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(float),0);

    unsigned int ibo; //inde buffer object
    glGenBuffers(1,&ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,6*sizeof(unsigned int),indices,GL_STATIC_DRAW);

    std::string vertexShader = 
        "attribute vec4 position;n"
        "n"
        "void main()n"
        "n"
        "{n"
        " gl_Position = position;n"
        "}n";

    std::string fragmentShader = 
        "void main()n"
        "{n"
        " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);n"
        "}n";


    unsigned int shader = CreateShader(vertexShader,fragmentShader);
    glUseProgram(shader);

    printf("Version: %sn", glGetString(GL_VERSION));

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);
        
        //glDrawArrays(GL_TRIANGLES,0,6);
        glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);

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

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

    glfwTerminate();
    return 0;
}

最后

以上就是忐忑柚子为你收集整理的opengl入门(五)使用indices复用vertex的全部内容,希望文章能够帮你解决opengl入门(五)使用indices复用vertex所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部