在qt中实现opengl obj模型导入:
main.cpp
#include<GL/glew.h> #include <GLFW/glfw3.h> #include<stdio.h> #include<glm/glm.hpp> #include<glm/ext.hpp> #include"misc.h" #include"model.h" GLfloat deltaTime = 0.0f; GLfloat lastFrame = 0.0f; GLint CreateGPUProgram(const char*vsShaderPath,const char*fsShaderPath) { 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); 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; } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } int main(void) { GLFWwindow* window; if (!glfwInit()) return -1; window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; // 还需要注册这个函数,告诉GLFW我们希望每当窗口调整大小的时候调用这个函数。 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glewInit(); GLuint program = CreateGPUProgram("/home/jun/OpenGL/model/sample.vs", "/home/jun/OpenGL/model/sample.fs"); GLint posLocation, texcoordLocation,normalLocation, MLocation, VLocation, PLocation; posLocation = glGetAttribLocation(program, "pos"); texcoordLocation = glGetAttribLocation(program, "texcoord"); normalLocation = glGetAttribLocation(program, "normal"); MLocation = glGetUniformLocation(program, "M"); VLocation = glGetUniformLocation(program, "V"); PLocation = glGetUniformLocation(program, "P"); unsigned int *indexes = nullptr; int vertexCount = 0, indexCount = 0; VertexData*vertexes = LoadObjModel("/home/jun/OpenGL/model/MODEL/niutou.obj", &indexes, vertexCount, indexCount); if (vertexes==nullptr) { printf("load obj model fail "); } //obj model -> vbo & ibo GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes); printf("vertex count %d index count %d ",vertexCount,indexCount); glClearColor(41.0f / 255.0f, 71.0f / 255.0f, 121.0f / 255.0f, 1.0f); //ShowWindow(hwnd, SW_SHOW); //UpdateWindow(hwnd); float identity[] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; //创建一个投影矩阵 glm::mat4 model; model = glm::translate(model, glm::vec3(0.0f, 0.0f, -54.0f)); model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f)); glm::mat4 projection=glm::perspective(45.0f,800.0f/600.0f,0.1f,1000.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); while (!glfwWindowShouldClose(window)) { GLfloat currentFrame = (GLfloat)glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; glfwPollEvents(); // glClearColor(1.0f, 0.04f, 0.14f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glUseProgram(program); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); 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(VertexData), (void*)0); glEnableVertexAttribArray(texcoordLocation); glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); glEnableVertexAttribArray(normalLocation); glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glUseProgram(0); glfwSwapBuffers(window); } glfwTerminate(); return 0; }
mish.h
#include<GL/glew.h> GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void*data = nullptr); char *LoadFileContent(const char*path);
mish.cpp
#include "misc.h" #include <stdio.h> GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void*data /* = nullptr */) { GLuint object; glGenBuffers(1, &object); glBindBuffer(bufferType, object); glBufferData(bufferType, size, data, usage); glBindBuffer(bufferType, 0); return object; } char *LoadFileContent(const char*path) { FILE*pFile = fopen(path, "rb"); if (pFile) { fseek(pFile, 0, SEEK_END); int nLen = ftell(pFile); char*buffer = nullptr; if (nLen!=0) { buffer=new char[nLen + 1]; rewind(pFile); fread(buffer, nLen, 1, pFile); buffer[nLen] = '