与上一节内容基本相同,只是用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[];
float color[];
}; char *LoadFileContent(const char*path)//加载shader
{
FILE*pFile = fopen(path, "rb");
if (pFile)
{
fseek(pFile, , SEEK_END);
int nLen = ftell(pFile);
char*buffer = new char[nLen+];
rewind(pFile);
fread(buffer, nLen , , pFile);
buffer[nLen]='\0';
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, , &vsCode, nullptr);
glShaderSource(fsShader, , &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 -; /* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(, , "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -;
} /* 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[];
vertex[].pos[] = ;
vertex[].pos[] = ;
vertex[].pos[] = -100.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f; vertex[].pos[] = ;
vertex[].pos[] = ;
vertex[].pos[] = -100.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f; vertex[].pos[] = ;
vertex[].pos[] = ;
vertex[].pos[] = -100.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
vertex[].color[] = 1.0f;
//vbo上传shader数据到GPU
GLuint vbo;
glGenBuffers(, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * , vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, );
//ibo上传shader数据到GPU
unsigned int indexes[] = {,,};
GLuint ibo;
glGenBuffers(, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * , indexes, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ); float identity[] = {
,,,,
,,,,
,,,,
,,,
};
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, , GL_FALSE, identity);
glUniformMatrix4fv(VLocation, , GL_FALSE, identity);
glUniformMatrix4fv(PLocation, , GL_FALSE, glm::value_ptr(projection)); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(posLocation);
glVertexAttribPointer(posLocation, , GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*));
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, , GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float)*)); //glDrawArrays(GL_TRIANGLES, 0, 3);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, , GL_UNSIGNED_INT, );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ); glUseProgram();
/* Swap front and back buffers */
glfwSwapBuffers(window); /* Poll for and process events */
glfwPollEvents();
} glfwTerminate();
return ;
}
效果和上一节一样。