Changeset 0b1b52d in opengl-game


Ignore:
Timestamp:
Oct 4, 2019, 8:27:07 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
771b33a
Parents:
83b5b4b
Message:

In openglgame, port over the functionality to specify and initialize varying attributes

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_opengl.cpp

    r83b5b4b r0b1b52d  
    66using namespace std;
    77
    8 GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL() {
     8GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL(Viewport viewport) {
     9   this->viewport = viewport;
    910}
    1011
     
    1213}
    1314
     15void 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
    1434void GraphicsPipeline_OpenGL::createPipeline(string vertShaderFile, string fragShaderFile) {
    1535   shaderProgram = loadShaderProgram(vertShaderFile, fragShaderFile);
    1636
    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   }
    1959}
    2060
  • graphics-pipeline_opengl.hpp

    r83b5b4b r0b1b52d  
    22#define _GRAPHICS_PIPELINE_OPENGL_H
    33
     4#include "graphics-pipeline.hpp"
     5
     6#include <vector>
     7
    48#include <GL/glew.h>
    59
    6 #include "graphics-pipeline.hpp"
     10enum VaryingAttribType {
     11   ATTRIB_OBJECT_VARYING,
     12   ATTRIB_POINT_VARYING,
     13};
     14
     15// TODO: Might be better to create a different struct for uniform attributes
     16struct 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};
    726
    827class GraphicsPipeline_OpenGL : public GraphicsPipeline {
    928   public:
    10       GraphicsPipeline_OpenGL();
     29      GraphicsPipeline_OpenGL(Viewport viewport);
    1130      ~GraphicsPipeline_OpenGL();
    1231
     32      void addVaryingAttribute(VaryingAttribType attribType, GLint size, GLenum type, size_t fieldOffset);
    1333      void createPipeline(string vertShaderFile, string fragShaderFile);
    1434
     
    1838      unsigned int numPoints;
    1939
     40      vector<VaryingAttribInfo> varyingAttribs;
     41
    2042      GLuint loadShaderProgram(string vertShaderFile, string fragShaderFile);
    2143      GLuint loadShader(GLenum type, string file);
  • opengl-game.cpp

    r83b5b4b r0b1b52d  
    108108   ((GameGui_GLFW*)gui)->bindEventHandlers();
    109109
    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
    111121   graphicsPipelines.back().createPipeline("gl-shaders/ship.vert", "gl-shaders/ship.frag");
    112122
    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
    114134   graphicsPipelines.back().createPipeline("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag");
    115135
    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
    117145   graphicsPipelines.back().createPipeline("gl-shaders/laser.vert", "gl-shaders/laser.frag");
    118146
    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
    120156   graphicsPipelines.back().createPipeline("gl-shaders/explosion.vert", "gl-shaders/explosion.frag");
    121157
  • opengl-game.hpp

    r83b5b4b r0b1b52d  
    99#include "game-gui-glfw.hpp"
    1010#include "graphics-pipeline_opengl.hpp"
     11
     12// TODO: Figure out if these structs should be defined in the OpenGLGame class
     13
     14enum ObjectType {
     15   TYPE_SHIP,
     16   TYPE_ASTEROID,
     17   TYPE_LASER,
     18   TYPE_EXPLOSION,
     19};
     20
     21struct 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
     44struct ParticleEffect : SceneObject {
     45   vector<GLfloat> particleVelocities;
     46   vector<GLfloat> particleTimes;
     47   GLfloat startTime;
     48   GLfloat duration;
     49};
    1150
    1251class OpenGLGame {
Note: See TracChangeset for help on using the changeset viewer.