c++ - Quake 3 BSP Renderer Drawing Random Vertices -


recently i've been writing game engine in opengl , have question. when generate bsp file (quake 3), generates odd vertices!! i've checked vertex vector, heres screenshot of contains (it repeats itself)

what vertex vector contains (vertexes)

i've checked glgeterror, , returns nothing (zero)! i'm stumped here, , have no idea do.

here code:

#include <gl/glew.h> #include <glfw/glfw3.h> #include <iostream> #include <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> #include <glm/vec3.hpp> #include <glm/gtc/matrix_transform.hpp> #include <vector>    #include "map.h" #include <fstream> #include <memory> #include "game_manager.h"  std::vector<bspverts> vertexes;  bool kikobsp::load_map(std::string file_name) {     this->file.open(file_name.c_str(), std::ios::in | std::ios::binary);      if (this->file.is_open())     {         this->file.read(reinterpret_cast<char*>(&this->header), sizeof(this->header));          bspentities* ents = new bspentities;         ents->ents_array = new char[this->header.lumps[bsplumps::entities].length];          this->num_textures = this->header.lumps[bsplumps::textures].length / sizeof(bsptexture);         this->num_planes = this->header.lumps[bsplumps::planes].length / sizeof(bspplane);         this->num_textures = this->header.lumps[bsplumps::textures].length / sizeof(bsptexture);         this->num_nodes = this->header.lumps[bsplumps::nodes].length / sizeof(bspnode);         this->num_leafs = this->header.lumps[bsplumps::leafs].length / sizeof(bspleaf);         this->num_leaf_faces = this->header.lumps[bsplumps::leaf_faces].length / sizeof(bspleafface);         this->num_leaf_brushes = this->header.lumps[bsplumps::leaf_brushes].length / sizeof(bspleafbrush);         this->num_models = this->header.lumps[bsplumps::models].length / sizeof(bspmodel);         this->num_brushes = this->header.lumps[bsplumps::brushes].length / sizeof(bspbrush);         this->num_brush_sides = this->header.lumps[bsplumps::brushsides].length / sizeof(bspbrushsides);         this->num_vertexes = this->header.lumps[bsplumps::vertexes].length / sizeof(bspverts);         this->num_meshverts = this->header.lumps[bsplumps::meshverts].length / sizeof(bspmeshverts);         this->num_effects = this->header.lumps[bsplumps::effects].length / sizeof(bspeffects);         this->num_faces = this->header.lumps[bsplumps::faces].length / sizeof(bspfaces);          (int32_t x = 0; x < this->num_vertexes; x++)         {             vertexes.push_back(*reinterpret_cast<bspverts*>(&x));         }          (int32_t x = 0; x < this->num_textures; x++)         {             this->textures.push_back(*reinterpret_cast<bsptexture*>(&x));         }          (int32_t x = 0; x < this->num_meshverts; x++)         {             this->mesh_verts.push_back(*reinterpret_cast<bspmeshverts*>(&x));         }          (int32_t x = 0; x < this->num_planes; x++)         {             this->planes.push_back(*reinterpret_cast<bspplane*>(&x));         }          (int32_t x = 0; x < this->num_nodes; x++)         {             this->nodes.push_back(*reinterpret_cast<bspnode*>(&x));         }          (int32_t x = 0; x < this->num_leafs; x++)         {             this->leaf.push_back(*reinterpret_cast<bspleaf*>(&x));         }          (int32_t x = 0; x < this->num_faces; x++)         {             this->faces.push_back(*reinterpret_cast<bspfaces*>(&x));         }          (int32_t x = 0; x < this->num_leaf_faces; x++)         {             this->leaf_faces.push_back(*reinterpret_cast<bspleafface*>(&x));         }          (int32_t x = 0; x < this->num_leaf_brushes; x++)         {             this->leaf_brush.push_back(*reinterpret_cast<bspleafbrush*>(&x));         }          (int32_t x = 0; x < this->num_models; x++)         {             this->model.push_back(*reinterpret_cast<bspmodel*>(&x));         }          (int32_t x = 0; x < this->num_brushes; x++)         {             this->brushes.push_back(*reinterpret_cast<bspbrush*>(&x));         }          (int32_t x = 0; x < this->num_brush_sides; x++)         {             this->brush_sides.push_back(*reinterpret_cast<bspbrushsides*>(&x));         }          (int32_t x = 0; x < this->num_effects; x++)         {             this->effect.push_back(*reinterpret_cast<bspeffects*>(&x));         }          this->file.seekg(this->header.lumps[bsplumps::entities].offset);         this->file.read(reinterpret_cast<char*>(ents->ents_array), this->header.lumps[bsplumps::entities].length);           this->file.seekg(this->header.lumps[bsplumps::textures].offset);         this->file.read(reinterpret_cast<char*>(textures.data()), this->header.lumps[bsplumps::textures].length);          this->file.seekg(this->header.lumps[bsplumps::planes].offset);         this->file.read(reinterpret_cast<char*>(this->planes.data()), this->header.lumps[bsplumps::planes].length);          this->file.seekg(this->header.lumps[bsplumps::nodes].offset);         this->file.read(reinterpret_cast<char*>(this->nodes.data()), this->header.lumps[bsplumps::nodes].length);          this->file.seekg(this->header.lumps[bsplumps::leafs].offset);         this->file.read(reinterpret_cast<char*>(this->leaf.data()), this->header.lumps[bsplumps::leafs].length);          this->file.seekg(this->header.lumps[bsplumps::leaf_faces].offset);         this->file.read(reinterpret_cast<char*>(this->leaf_faces.data()), this->header.lumps[bsplumps::leaf_faces].length);          this->file.seekg(this->header.lumps[bsplumps::leaf_brushes].offset);         this->file.read(reinterpret_cast<char*>(this->leaf_brush.data()), this->header.lumps[bsplumps::leaf_brushes].length);          this->file.seekg(this->header.lumps[bsplumps::models].offset);         this->file.read(reinterpret_cast<char*>(this->model.data()), this->header.lumps[bsplumps::models].length);          this->file.seekg(this->header.lumps[bsplumps::brushes].offset);         this->file.read(reinterpret_cast<char*>(this->brushes.data()), this->header.lumps[bsplumps::brushes].length);          this->file.seekg(this->header.lumps[bsplumps::brushsides].offset);         this->file.read(reinterpret_cast<char*>(this->brush_sides.data()), this->header.lumps[bsplumps::brushsides].length);          this->file.seekg(this->header.lumps[bsplumps::vertexes].offset);          (int32_t x = 0; x < this->num_vertexes; x++)         {             this->file.read(reinterpret_cast<char*>(&vertexes.data()[x]), this->header.lumps[bsplumps::vertexes].length);         }          this->file.seekg(this->header.lumps[bsplumps::meshverts].offset);         this->file.read(reinterpret_cast<char*>(this->mesh_verts.data()), this->header.lumps[bsplumps::meshverts].length);          this->file.seekg(this->header.lumps[bsplumps::effects].offset);         this->file.read(reinterpret_cast<char*>(this->effect.data()), this->header.lumps[bsplumps::effects].length);          this->file.seekg(this->header.lumps[bsplumps::faces].offset);         this->file.read(reinterpret_cast<char*>(this->faces.data()), this->header.lumps[bsplumps::faces].length);          std::printf("bsp version: '%s'\n", this->header.magic);          if (std::strncmp(this->header.magic, "ibsp", 4) == 0)         {             std::printf("success: valid bsp format!\n");         }         else         {             std::printf("error: invalid bsp format!\n");              return false;         }          this->shader.load_shader("shaders/bsp.vs", "shaders/bsp.fs");          /* heres try store data faces vertices */         (int32_t x = 0; x < this->num_faces; x++)         {             bspfaces& face = this->faces[x];              (int32_t vertices = 0; vertices < this->num_vertexes; vertices++)              {                 bspverts* vert = reinterpret_cast<bspverts*>(&vertexes[x]);                  this->vertices_vector.push_back(vert[x].position.x);                 this->vertices_vector.push_back(vert[x].position.y);                 this->vertices_vector.push_back(vert[x].position.z);                  this->colors.push_back(vert[x].position.x / 0xff);                 this->colors.push_back(vert[x].position.y / 0xff);                 this->colors.push_back(vert[x].position.z / 0xff);                  this->indices.push_back(vert[x].position.x);                 this->indices.push_back(vert[x].position.y);                 this->indices.push_back(vert[x].position.z);             }         }          glgenvertexarrays(1, &this->vao);          glbindvertexarray(this->vao);          glgenbuffers(1, &this->vbo);         glbindbuffer(gl_array_buffer, vbo);         glbufferdata(gl_array_buffer, this->vertices_vector.size() * sizeof(float), &this->vertices_vector.front(), gl_static_draw);          glgenbuffers(1, &this->color_vbo);         glbindbuffer(gl_array_buffer, this->color_vbo);         glbufferdata(gl_array_buffer, this->colors.size() * sizeof(float), &this->colors.front(), gl_static_draw);          this->coord3d = glgetattriblocation(this->shader.program, "coord3d");         this->mvp = glgetuniformlocation(this->shader.program, "mvp");         this->attrib_color = glgetattriblocation(this->shader.program, "v_color");          glbindbuffer(gl_array_buffer, this->vbo);         glvertexattribpointer(this->coord3d, // attribute             3,                 // number of elements per vertex, here (r,g,b)             gl_float,          // currentblock of each element             gl_false,          // take our values as-is             0,                 // no data between each position             nullptr               // offset of first element         );          glbindbuffer(gl_array_buffer, this->color_vbo);         glvertexattribpointer(this->attrib_color,             3,             gl_float,             gl_false,             0,             nullptr         );          glbindvertexarray(0);          glvertexattrib3fv(this->attrib_color, reinterpret_cast<float*>(this->colors.data()));          return true;     }     else     {         std::printf("error: couldn't open file!\n");          return false;     }      return false; }  void kikobsp::render(glm::vec3 position) {     glbindvertexarray(this->vao);     glenablevertexattribarray(this->coord3d);     glenablevertexattribarray(this->attrib_color);      glm::mat4 model = glm::translate(glm::mat4(1.0), glm::vec3(position.x, position.y, position.z));     glm::mat4 mvp = game_manager->projection * game_manager->view * model;     gluniformmatrix4fv(this->mvp, 1, gl_false, glm::value_ptr(mvp));      gllinewidth(3.0);     gldrawarrays(gl_lines, 0, this->vertices_vector.size());      gldisablevertexattribarray(this->coord3d);     gldisablevertexattribarray(this->attrib_color);     glbindvertexarray(0); }  void kikobsp::cleanup_map() {     /* delete[] textures;     delete[] planes;     delete[] this->leafs;     delete[] this->nodes;     delete[] this->leaf_this->faces;     delete[] this->models;     delete[] this->brushes;     delete[] this->brush_sides;     delete[] this->vertexes;     delete[] this->mesh_verts;     delete[] this->effects;     delete[] this->this->faces;     (use maybe later?) */     this->file.close(); } 

there no errors in code, draws this:

what draws

which not map!

also, heres header file if need to:

#pragma once #pragma pack(2)  #include <iostream> #include <cstdint> #include <string> #include <vector> #include <glm/vec3.hpp> #include <fstream> #include "shader.h" #include <memory> #include <array>  #define face_polygon 1  enum bsplumps {     entities,     textures,     planes,     nodes,     leafs,     leaf_faces,     leaf_brushes,     models,     brushes,     brushsides,     vertexes,     meshverts,     effects,     faces,     lightmaps,     lightvols,     visdata,     max_lumps };  struct bsplump {     int32_t offset; /* offset start of lump */     int32_t length; /* length of lump, multiple of 4 */ };  struct bspheader {     char magic[4]; /* ibsp */     int32_t version; /* 0x2e quake 3 */     bsplump lumps[bsplumps::max_lumps]; /* direntries */ };  struct bspentities {     char* ents_array; };  struct bsptexture {     char name[64];     int32_t flags;     int32_t contents; };  struct bspplane {     glm::vec3 normal;     float distance; };  struct bspnode {     int32_t plane;     glm::ivec2 children;     glm::ivec3 mins;     glm::ivec3 maxs; };  struct bspleaf {     int32_t cluster;     int32_t area;     glm::ivec3 mins;     glm::ivec3 maxs;     int32_t leafface;     int32_t num_leaffaces;     int32_t leaf_brush;     int32_t num_leaf_brushes; };  struct bspleafface {     int32_t face; };  struct bspleafbrush {     int32_t brush; };  struct bspmodel {     glm::fvec3 mins;     glm::fvec3 maxs;     int32_t face;     int32_t num_faces;     int32_t brush;     int32_t num_brushes; };  struct bspbrush {     int32_t brush_side;     int32_t num_brush_sides;     int32_t texture; };  struct bspbrushsides {     int32_t plane;     int32_t texture; };  struct bspverts {     glm::vec3 position;     glm::vec2 tex_coord; /* same float tex_coord[2][2] */     glm::vec2 lm_coord; /* same float tex_coord[2][2] */     glm::vec3 normal;     char color[4]; };  struct bspmeshverts {     int32_t offset; };  struct bspeffects {     char name[64];     int32_t brush;     int32_t unk; /* unknown */ };  struct bspfaces {     int32_t texture;     int32_t effect;     int32_t type;     int32_t vertex;     int32_t num_vertexes;     int32_t meshvert; /* start */     int32_t num_of_meshverts;     int32_t lm_index;     glm::ivec2 lm_start;     glm::ivec2 lm_size;     glm::vec3 lm_origin;     float lm_vecs[2][3];     glm::fvec3 normal;     glm::ivec2 size; };  class kikobsp { public:     bool load_map(std::string);     void render(glm::vec3);     void draw_level();     int32_t get_max();     int32_t get_min();     void get_vert();      void cleanup_map();      shader shader;     bspheader header;      int32_t num_vertexes;  private:     std::ifstream file;     uint32_t vbo;     uint32_t vao;     uint32_t color_vbo;     uint32_t ebo;      int32_t coord3d;     int32_t mvp;     int32_t attrib_color;     bspfaces* cur_face;      std::vector<float> vertices_vector;     std::vector<float> colors;     std::vector<float> indices;     std::vector<bspfaces> faces;     std::vector<bsptexture> textures;     std::vector<bspplane> planes;     std::vector<bspnode> nodes;     std::vector<bspmeshverts> mesh_verts;     std::vector<bspleaf> leaf;     std::vector<bspleafface> leaf_faces;     std::vector<bspleafbrush> leaf_brush;     std::vector<bspmodel> model;     std::vector<bspbrush> brushes;     std::vector<bspbrushsides> brush_sides;     std::vector<bspeffects> effect;      int32_t num_textures;     int32_t num_planes;     int32_t num_nodes;     int32_t num_leafs;     int32_t num_leaf_faces;     int32_t num_leaf_brushes;     int32_t num_models;     int32_t num_brushes;     int32_t num_brush_sides;     int32_t num_meshverts;     int32_t num_effects;     int32_t num_faces;     int32_t num_lightmaps;     int32_t num_lightvols; }; 

i think problem indices or vertices. believe problem lies how store data vertices. buggy.

thanks! helps appreciated! :)

whenever says "there no errors in code", know has gone terribly wrong!

for example, supposed doing?

for (int32_t x = 0; x < this->num_vertexes; x++) {     vertexes.push_back(*reinterpret_cast<bspverts*>(&x)); } 

it looks it's invoking undefined behavior here. if want fill array up, this:

vertexes.resize(num_vertices); 

secondly, code reads in vertexes file looks has logic error:

for (int32_t x = 0; x < this->num_vertexes; x++) {     this->file.read(reinterpret_cast<char*>(&vertexes.data()[x]), this->header.lumps[bsplumps::vertexes].length); } 

this looks it's going read in entirety of vertex lump each vertex, resulting in bizarre behavior. want this:

file.read(reinterpret_cast<char*>(vertexes.data()), header.lumps[bsplumps::vertexes].length); 

also, note on code that's unrelated problem: using this-> everywhere hurts readability , adds nothing code, remove it.

i did same bsp renderer while back, can't remember state left in, can have @ code here.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -