首页 > 其他 > 详细

- OPENGL3 -

时间:2020-02-09 17:59:59      阅读:84      评论:0      收藏:0      [点我收藏+]

CP07:贴图

技术分享图片

 

 

 

>>理解Element Buffer Object(EBO), 理解glDrawElements()

技术分享图片

 

 

 

例如上图要绘制一个长方形,上一篇- OPENGL2 - 最后一章用的glDrawArrays(GL_QUADS,0 ,NPTS) 绘制。

此章绘制用glDrawElements().如上图,绘制可以为2个三角形,那就是绘制6个顶点。

// triangle indices
static GLuint indices[6] = {
    0, 1, 3,  // FIRST TRIANGLE
    1, 2, 3   // SECOND TRIANGLE
};

 

第一种方法:直接绘制方法就是在main loop中:

glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_INT , indices);  // draw six verticles

 

第二种方法:

如果建立了EBO对象就不用这么绘制可以先创建 EBO:

glCreateBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glNamedBufferStorage(EBO, sizeof(indices), indices , 0);

然后在main loop中写:

 glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_INT , NULL);  // draw six verticles

显然第一种简单。

技术分享图片
#define GLEW_STATIC
// GLEW

#include <GL/glew.h>
#include <cstdlib>
#undef GLFW_DLL
// GLFW
#include <GLFW/glfw3.h>
#include <iostream>
#include "LoadShader.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>


using namespace std;

const int NPTS = 4;
static GLuint VBO,VAO,EBO;

// load shader
static LoadShader shader;

// Texture
static GLuint  texture;



void init(){
    static const GLfloat verticles[NPTS][8] = {
        // positions          // colors           // texture coords
         {0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f},   // top right
         {0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f},   // bottom right
        {-0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f},   // bottom left
        {-0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f}    // top left
    };



    GLuint indices[6] = {
        0, 1, 3,  // FIRST TRIANGLE
        1, 2, 3   // SECOND TRIANGLE
    };



    shader.load("shader.vert","shader.frag");
    shader.use();

    // Vertex Array Object
    glCreateVertexArrays(1,&VAO);
    glBindVertexArray(VAO);

   // Vertex Buffer Object
    glCreateBuffers(1,&VBO);
    glNamedBufferStorage(VBO,sizeof(verticles),verticles, 0 );
    glBindBuffer(GL_ARRAY_BUFFER,VBO);

    // Create Element buffer object
    glCreateBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glNamedBufferStorage(EBO, sizeof(indices), indices , 0);



    // position Attrib
    glVertexAttribPointer(0,                        // LOCATION
                          3,                        // vector is 3 length
                          GL_FLOAT,                 // float type
                          GL_FALSE,                 // not normalized
                          8 * sizeof(float),        // stride
                          (void*)0 );               // offset
    glEnableVertexAttribArray(0);

    // color Attrib
    glVertexAttribPointer(1,3, GL_FLOAT,GL_FALSE,8 * sizeof(float), (void*)(3* sizeof(float)) );
    glEnableVertexAttribArray(1);


    // st Attrib
    glVertexAttribPointer(2,2, GL_FLOAT,GL_FALSE,8 * sizeof(float), (void*)(6* sizeof(float)) );
    glEnableVertexAttribArray(2);

    //glGenTextures(1,&texture);
    glCreateTextures(GL_TEXTURE_2D, 1, &texture); // same as above method
    glBindTexture(GL_TEXTURE_2D, texture);

    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // same as above methods
    glTextureParameteri(texture, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(texture, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTextureParameteri(texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    // load imag
    int width,height,nchans;
    unsigned char *data = stbi_load("texture_02.jpg",&width, &height, &nchans, 0);
    cout << "WIDTH:" << width << "HEIGHT:" <<height <<endl;
    if(data){
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        glGenerateTextureMipmap(texture);

    }
    else{
        cout << "Load texture error" <<endl;
    }
    stbi_image_free(data);


}

void display(){

    static const float black[] = {0.2f, 0.3f, 0.3f, 1.0f};
    glClearBufferfv(GL_COLOR,0,black);
    //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    //glClear(GL_COLOR_BUFFER_BIT);


    //glBindTexture(GL_TEXTURE_2D, texture);
    glBindVertexArray(VAO);
    //glDrawArrays(GL_QUADS,0 ,NPTS);
    glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_INT , NULL);  // draw six verticles
}

int main()
{
    cout << sizeof(float) <<endl;
    glfwInit();
    GLFWwindow *window =glfwCreateWindow(1280,720,"Triangles",NULL,NULL);
    glfwMakeContextCurrent(window);
    glewInit();

    init();


    while(!glfwWindowShouldClose(window)){
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}
main.cpp

 

>> 两个贴图时候的情况:

 

- OPENGL3 -

原文:https://www.cnblogs.com/gearslogy/p/12287447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!