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; }
>> 两个贴图时候的情况:
原文:https://www.cnblogs.com/gearslogy/p/12287447.html