Reputation: 856
When I try to access shader_id
from my Shader
class, it segfaults, but only when I try to access it from my OBJModel
class. It doesn't matter if I make shader_id public or if I make it private and access it from a function.
This is the error I get when running GDB:
Program received signal SIGSEGV, Segmentation fault.
0x00404831 in _fu9___ZSt4cout () at .\src/objmodel.cpp:153
153 std::cerr << "Shader ID: " << shader->shader_id << std::endl;
It segfaults no matter when I try to access it in the class, even in the constructor.
The OBJModel class is rather big so I will only be posting the header file and constructor (as those are the only ones that have been executed when the error appears.)
#ifndef __OBJMODEL_H_INCLUDED
#define __OBJMODEL_H_INCLUDED
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <gl/glew.h>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include "cache.h"
#include "shader.h"
typedef struct {
float r;
float g;
float b;
} Color;
typedef struct {
float x;
float y;
float z;
} Vertex;
typedef struct {
float u;
float v;
} TexCoord;
typedef struct {
float x;
float y;
float z;
} Normal;
typedef struct {
int vertices[4];
int texcoords[4];
int normals[4];
int face_type;
} Face;
typedef struct {
std::string filename;
Color ambient;
Color diffuse;
Color specular;
float specular_power;
float transparency;
} Material;
typedef struct {
std::vector<Face> faces;
unsigned int num_vertices;
GLfloat* vertices;
GLfloat* normals;
GLfloat* texcoords;
Material material;
bool has_material;
} Object;
class OBJModel {
private:
std::vector<Vertex> vertices;
std::vector<TexCoord> texcoords;
std::vector<Normal> normals;
std::vector<Object> objects;
std::map<std::string, Material> materials;
Cache* cache;
Shader* shader;
public:
float xRot;
float yRot;
float zRot;
OBJModel(Cache* cache_, Shader* shader_);
~OBJModel();
int load_mtl(std::string filename);
int load(std::string filename);
int expand_indices(unsigned int index);
int draw_object(Object o);
int draw_object_old(Object o);
int draw(float x, float y, float z);
int draw_immediate(float x, float y, float z);
};
#endif
OBJModel constructor:
OBJModel::OBJModel(Cache* cache_, Shader* shader_) {
cache = cache_;
shader = shader_;
xRot = 0;
yRot = 0;
zRot = 0;
std::cerr << "Init OBJ ID: " << shader->shader_id << std::endl;
}
This is my shader constructor:
Shader::Shader(std::string vert_filename, std::string frag_filename) {
vert_shader = glCreateShader(GL_VERTEX_SHADER);
frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
const char* vert_source_c = textFileRead(vert_filename.c_str());
const char* frag_source_c = textFileRead(frag_filename.c_str());
glShaderSource(vert_shader, 1, &vert_source_c, 0);
glShaderSource(frag_shader, 1, &frag_source_c, 0);
glCompileShader(vert_shader);
const unsigned int BUFFER_SIZE = 512;
char buffer[BUFFER_SIZE];
GLsizei length = 0;
glGetShaderInfoLog(vert_shader, BUFFER_SIZE, &length, buffer);
if (length > 0) {
std::cerr << "Shader " << vert_shader << " (" << vert_filename << ") compile error: " << buffer << std::endl;
}
glCompileShader(frag_shader);
const unsigned int BUFFER_SIZE2 = 512;
char buffer2[BUFFER_SIZE];
GLsizei length2 = 0;
glGetShaderInfoLog(frag_shader, BUFFER_SIZE2, &length2, buffer2);
if (length2 > 0) {
std::cerr << "Shader " << frag_shader << " (" << frag_filename << ") compile error: " << buffer2 << std::endl;
}
shader_id = glCreateProgram();
glAttachShader(shader_id, vert_shader);
glAttachShader(shader_id, frag_shader);
glBindFragDataLocation(shader_id, 0, "fragColor");
glBindAttribLocation(shader_id, 0, "vertex");
glBindAttribLocation(shader_id, 1, "normal");
glBindAttribLocation(shader_id, 2, "texcoord");
glLinkProgram(shader_id);
}
If you need to see more code, please tell me and I'll post it.
Edit: I create the OBJModel like this:
island_model = new OBJModel(&cache, shader);
island_model->load("models/island/island_low.obj");
Edit 2: This is the Shader class declaration:
#ifndef __SHADER_H_INCLUDED
#define __SHADER_H_INCLUDED
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <gl/glew.h>
class Shader {
private:
GLuint vert_shader;
GLuint frag_shader;
std::set<int> attribsSet;
public:
GLuint shader_id;
Shader(std::string vert_filename, std::string frag_filename);
~Shader();
std::string readShaderSource(std::string filename);
void bind();
void unbind();
void setAttrib(std::string attribName, GLint len, void* data);
void setUniformMatrix4fv(std::string uniformName, GLfloat* data);
void reset();
GLuint id();
};
#endif
Upvotes: 0
Views: 1183
Reputation: 856
It seems I accidentally created the OBJModel
before the Shader
. Changing the order fixed everything.
Upvotes: 3
Reputation: 57169
Make sure that shader_
is not null or a bad reference (adress of some object created on the stack). Since it is a pointer be sure that is created with new
and that ownership of the Shader
object is clear.
OBJModel model = OBJModel(NULL,
new Shader(std::string("filename"), std::string("fragname")));
//OBJModel is responsible for deleting Shader in its destructor in this scenario.
Upvotes: 0