gpt4 book ai didi

c++ - 从特定类调用函数时出现段错误

转载 作者:行者123 更新时间:2023-11-28 01:02:39 26 4
gpt4 key购买 nike

当我尝试从我的 Shader 类访问 shader_id 时,它会出现段错误,但只有当我尝试从我的 OBJModel 类访问它时.将 shader_id 公开还是将其设为私有(private)并从函数访问它都没有关系。

这是我在运行 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;

无论我何时尝试在类中访问它,它都会出现段错误,即使是在构造函数中也是如此。

OBJModel 类相当大,所以我将只发布头文件和构造函数(因为它们是出现错误时唯一执行的。)

#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 构造函数:

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;
}

这是我的着色器构造函数:

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);
}

如果您需要查看更多代码,请告诉我,我会发布。

编辑:我这样创建 OBJModel:

island_model = new OBJModel(&cache, shader);
island_model->load("models/island/island_low.obj");

编辑 2:这是着色器类声明:

#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

最佳答案

看来我不小心在 Shader 之前创建了 OBJModel。更改顺序可以解决所有问题。

关于c++ - 从特定类调用函数时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7982542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com