用可编程管线绘制一个三角形
1.以上一节window为基准,进行绘制。
2.下载编译glew,并在.pro添加动态链接,并在头文件中引用。
LIBS +=-L/usr/lib64 -lGLEW
可能根据安装路径不同,修改动态链接库的路径,-L是路径的索引 -l是名称索引,去掉lib或so.
3.下载编译安装glm,并在头文件中引用。这个不需要添加动态链接库。
4.按照如下编写代码。
#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]='\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, 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; //上传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); 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); glUseProgram(0); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; }
最后的效果: