OpenGL Mesh Class - c++ - Code Review Stack Exchange

文章推薦指數: 80 %
投票人數:10人

OpenGL Mesh Class ... I've written a simple mesh class. The purpose of it is to build a mesh, draw it to the screen, and provide some means by ... 2022DeveloperSurveyisopen!Takesurvey. CodeReviewStackExchangeisaquestionandanswersiteforpeerprogrammercodereviews.Itonlytakesaminutetosignup. Signuptojointhiscommunity Anybodycanaskaquestion Anybodycananswer Thebestanswersarevotedupandrisetothetop Home Public Questions Tags Users Companies Unanswered Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore OpenGLMeshClass AskQuestion Asked 1year,11monthsago Modified 1year,11monthsago Viewed 1ktimes 3 \$\begingroup\$ I'vewrittenasimplemeshclass.Thepurposeofitistobuildamesh,drawittothescreen,andprovidesomemeansbywhichthemeshcanbetransformed/scaled,etc.ThiswasdonewithGLAD,GLFW,GLM,andOpenGL. /* Themeshclasstakesvertexdata,bindsVAOs,VBOs,drawingorders,etc,anddrawsit. Otherclassescaninheritfromthisclass. */ classMesh{ private: //------------------------------------------------------------------------------------------- //GLenum:drawingmode(ieGL_STATIC_DRAW)andprimitivetype(ieGL_TRIANGLES) GLenumDRAW_MODE,PRIMITIVE_TYPE; //------------------------------------------------------------------------------------------- //Vertexbufferobject,vertexarrayobject,elementbufferobject unsignedintVBO,VAO,EBO; protected: //------------------------------------------------------------------------------------------- //Vectorsholdingvertexandindexdata std::vectorvertices; std::vectorindices; //------------------------------------------------------------------------------------------- voidinit(){ //Generatevertexarrays glGenVertexArrays(1,&VAO); //GenerateVBO glGenBuffers(1,&VBO); //GenerateEBO glGenBuffers(1,&EBO); //BindtheVAO glBindVertexArray(VAO); //Bindthebuffer glBindBuffer(GL_ARRAY_BUFFER,VBO); //DetailtheVBObufferdata-attachthevertices glBufferData(GL_ARRAY_BUFFER,vertices.size()*sizeof(Vertex),&vertices[0],DRAW_MODE); //Bindtheindices glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO); //DetailtheEBOdata glBufferData(GL_ELEMENT_ARRAY_BUFFER,indices.size()*sizeof(unsignedint), &indices[0],DRAW_MODE); glEnableVertexAttribArray(0); //TellOpenGLhowthevertexdataisstructured glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)0); glBindVertexArray(0); } //------------------------------------------------------------------------------------------- voidset_vertices(std::vector_vertices){ vertices=_vertices; } //------------------------------------------------------------------------------------------- voidset_indices(std::vector_indices){ indices=_indices; } //------------------------------------------------------------------------------------------- voidset_primitive_type(GLenum_PRIMITIVE_TYPE){ PRIMITIVE_TYPE=_PRIMITIVE_TYPE; } //------------------------------------------------------------------------------------------- voidset_draw_mode(GLenum_DRAW_MODE){ DRAW_MODE=_DRAW_MODE; } public: //------------------------------------------------------------------------------------------- Mesh(std::vector_vertices,std::vector_indices, GLenum_DRAW_MODE=GL_STATIC_DRAW,GLenum_PRIMITIVE_TYPE=GL_TRIANGLES){ this->vertices=_vertices; this->indices=_indices; this->DRAW_MODE=_DRAW_MODE; this->PRIMITIVE_TYPE=_PRIMITIVE_TYPE; //std::cout<DRAW_MODE=_DRAW_MODE; this->PRIMITIVE_TYPE=_PRIMITIVE_TYPE; } //------------------------------------------------------------------------------------------- virtual~Mesh(){ glDeleteVertexArrays(1,&VAO); glDeleteBuffers(1,&VBO); glDeleteBuffers(1,&EBO); } //------------------------------------------------------------------------------------------- virtualvoidupdate(){} //------------------------------------------------------------------------------------------- voiddraw(){ //BindtheEBO glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO); //Bindthevertexarrayobject glBindVertexArray(VAO); //Readytodraw glDrawElements(PRIMITIVE_TYPE,indices.size(),GL_UNSIGNED_INT,0); //Unbindthevertexarray(althoughthisisn'tentirelynecessary) glBindVertexArray(0); } //------------------------------------------------------------------------------------------- /*NowIwillintroducesomesimplemeshtransformationfunctions*/ voidmove(glm::vec3_position){ //Transformeachvertexinthegivenvectortoachievethedesiredeffect for(std::size_ti=0;ivertices; std::vectorindices; //------------------------------------------------------------------------------------------- //Setuptheplane voidcreate_mesh_plane(){ constintw=SIZE_X+1; for(std::size_ti=0;i



請為這篇文章評分?