Changeset 0b1b52d in opengl-game
- Timestamp:
- Oct 4, 2019, 8:27:07 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 771b33a
- Parents:
- 83b5b4b
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_opengl.cpp
r83b5b4b r0b1b52d 6 6 using namespace std; 7 7 8 GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL() { 8 GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL(Viewport viewport) { 9 this->viewport = viewport; 9 10 } 10 11 … … 12 13 } 13 14 15 void GraphicsPipeline_OpenGL::addVaryingAttribute(VaryingAttribType attribType, GLint size, GLenum type, 16 size_t fieldOffset) { 17 // TODO: Throw an exception instead 18 if (type != GL_FLOAT && type != GL_UNSIGNED_INT) { 19 cout << "Unknown shader program attribute type: " << type << endl; 20 return; 21 } 22 23 VaryingAttribInfo attributeDesc; 24 25 attributeDesc.attribType = attribType; 26 attributeDesc.index = this->varyingAttribs.size(); 27 attributeDesc.size = size; 28 attributeDesc.type = type; 29 attributeDesc.fieldOffset = fieldOffset; 30 31 this->varyingAttribs.push_back(attributeDesc); 32 } 33 14 34 void GraphicsPipeline_OpenGL::createPipeline(string vertShaderFile, string fragShaderFile) { 15 35 shaderProgram = loadShaderProgram(vertShaderFile, fragShaderFile); 16 36 17 glGenVertexArrays(1, &vao); 18 numPoints = 0; 37 this->numPoints = 0; 38 glGenVertexArrays(1, &this->vao); 39 glBindVertexArray(this->vao); 40 41 vector<VaryingAttribInfo>::iterator it; 42 for (it = this->varyingAttribs.begin(); it != this->varyingAttribs.end(); it++) { 43 glEnableVertexAttribArray(it->index); 44 45 glGenBuffers(1, &it->buffer); 46 glBindBuffer(GL_ARRAY_BUFFER, it->buffer); 47 48 switch (it->type) { 49 case GL_FLOAT: { 50 glVertexAttribPointer(it->index, it->size, it->type, GL_FALSE, 0, NULL); 51 break; 52 } 53 case GL_UNSIGNED_INT: { 54 glVertexAttribIPointer(it->index, it->size, it->type, 0, NULL); 55 break; 56 } 57 } 58 } 19 59 } 20 60 -
graphics-pipeline_opengl.hpp
r83b5b4b r0b1b52d 2 2 #define _GRAPHICS_PIPELINE_OPENGL_H 3 3 4 #include "graphics-pipeline.hpp" 5 6 #include <vector> 7 4 8 #include <GL/glew.h> 5 9 6 #include "graphics-pipeline.hpp" 10 enum VaryingAttribType { 11 ATTRIB_OBJECT_VARYING, 12 ATTRIB_POINT_VARYING, 13 }; 14 15 // TODO: Might be better to create a different struct for uniform attributes 16 struct VaryingAttribInfo { 17 VaryingAttribType attribType; 18 GLuint index; 19 GLint size; 20 GLenum type; 21 // UniformType uniType; 22 GLuint buffer; // For uniforms, this is the uniform location 23 size_t fieldOffset; 24 // GLfloat* data; // pointer to data source for uniform attributes 25 }; 7 26 8 27 class GraphicsPipeline_OpenGL : public GraphicsPipeline { 9 28 public: 10 GraphicsPipeline_OpenGL( );29 GraphicsPipeline_OpenGL(Viewport viewport); 11 30 ~GraphicsPipeline_OpenGL(); 12 31 32 void addVaryingAttribute(VaryingAttribType attribType, GLint size, GLenum type, size_t fieldOffset); 13 33 void createPipeline(string vertShaderFile, string fragShaderFile); 14 34 … … 18 38 unsigned int numPoints; 19 39 40 vector<VaryingAttribInfo> varyingAttribs; 41 20 42 GLuint loadShaderProgram(string vertShaderFile, string fragShaderFile); 21 43 GLuint loadShader(GLenum type, string file); -
opengl-game.cpp
r83b5b4b r0b1b52d 108 108 ((GameGui_GLFW*)gui)->bindEventHandlers(); 109 109 110 graphicsPipelines.push_back(GraphicsPipeline_OpenGL()); 110 graphicsPipelines.push_back(GraphicsPipeline_OpenGL(viewport)); 111 112 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 113 offset_of(&SceneObject::points)); 114 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 115 offset_of(&SceneObject::colors)); 116 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 117 offset_of(&SceneObject::normals)); 118 graphicsPipelines.back().addVaryingAttribute(ATTRIB_OBJECT_VARYING, 1, GL_UNSIGNED_INT, 119 offset_of(&SceneObject::ubo_offset)); 120 111 121 graphicsPipelines.back().createPipeline("gl-shaders/ship.vert", "gl-shaders/ship.frag"); 112 122 113 graphicsPipelines.push_back(GraphicsPipeline_OpenGL()); 123 graphicsPipelines.push_back(GraphicsPipeline_OpenGL(viewport)); 124 125 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 126 offset_of(&SceneObject::points)); 127 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 128 offset_of(&SceneObject::colors)); 129 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 130 offset_of(&SceneObject::normals)); 131 graphicsPipelines.back().addVaryingAttribute(ATTRIB_OBJECT_VARYING, 1, GL_UNSIGNED_INT, 132 offset_of(&SceneObject::ubo_offset)); 133 114 134 graphicsPipelines.back().createPipeline("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag"); 115 135 116 graphicsPipelines.push_back(GraphicsPipeline_OpenGL()); 136 graphicsPipelines.push_back(GraphicsPipeline_OpenGL(viewport)); 137 138 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 139 offset_of(&SceneObject::points)); 140 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 2, GL_FLOAT, 141 offset_of(&SceneObject::texcoords)); 142 graphicsPipelines.back().addVaryingAttribute(ATTRIB_OBJECT_VARYING, 1, GL_UNSIGNED_INT, 143 offset_of(&SceneObject::ubo_offset)); 144 117 145 graphicsPipelines.back().createPipeline("gl-shaders/laser.vert", "gl-shaders/laser.frag"); 118 146 119 graphicsPipelines.push_back(GraphicsPipeline_OpenGL()); 147 graphicsPipelines.push_back(GraphicsPipeline_OpenGL(viewport)); 148 149 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 3, GL_FLOAT, 150 offset_of(&ParticleEffect::particleVelocities)); 151 graphicsPipelines.back().addVaryingAttribute(ATTRIB_POINT_VARYING, 1, GL_FLOAT, 152 offset_of(&ParticleEffect::particleTimes)); 153 graphicsPipelines.back().addVaryingAttribute(ATTRIB_OBJECT_VARYING, 1, GL_UNSIGNED_INT, 154 offset_of(&ParticleEffect::ubo_offset)); 155 120 156 graphicsPipelines.back().createPipeline("gl-shaders/explosion.vert", "gl-shaders/explosion.frag"); 121 157 -
opengl-game.hpp
r83b5b4b r0b1b52d 9 9 #include "game-gui-glfw.hpp" 10 10 #include "graphics-pipeline_opengl.hpp" 11 12 // TODO: Figure out if these structs should be defined in the OpenGLGame class 13 14 enum ObjectType { 15 TYPE_SHIP, 16 TYPE_ASTEROID, 17 TYPE_LASER, 18 TYPE_EXPLOSION, 19 }; 20 21 struct SceneObject { 22 unsigned int id; 23 ObjectType type; 24 bool deleted; 25 26 // Currently, model_transform should only have translate, and rotation and scale need to be done in model_base since 27 // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate 28 // matrices for each object that can be updated independently and then applied to the object in that order. 29 // TODO: Actually, to make this as generic as possible, each object should have a matrix stack to support, 30 // for instance, applying a rotate, then a translate, then another rotate. Think about and implement the best approach. 31 glm::mat4 model_mat, model_base, model_transform; 32 glm::mat4 translate_mat; // beginning of doing what's mentioned above 33 unsigned int num_points; 34 GLuint vertex_vbo_offset; 35 GLuint ubo_offset; 36 vector<GLfloat> points; 37 vector<GLfloat> colors; 38 vector<GLfloat> texcoords; 39 vector<GLfloat> normals; 40 glm::vec3 bounding_center; 41 GLfloat bounding_radius; 42 }; 43 44 struct ParticleEffect : SceneObject { 45 vector<GLfloat> particleVelocities; 46 vector<GLfloat> particleTimes; 47 GLfloat startTime; 48 GLfloat duration; 49 }; 11 50 12 51 class OpenGLGame {
Note:
See TracChangeset
for help on using the changeset viewer.