aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/RenderBackend/OpenGL/Texture2D.cpp13
-rw-r--r--src/main.cpp26
-rw-r--r--src/model_loader/ObjModelLoader.cpp12
3 files changed, 44 insertions, 7 deletions
diff --git a/src/RenderBackend/OpenGL/Texture2D.cpp b/src/RenderBackend/OpenGL/Texture2D.cpp
index f4326a8..7d1b83c 100644
--- a/src/RenderBackend/OpenGL/Texture2D.cpp
+++ b/src/RenderBackend/OpenGL/Texture2D.cpp
@@ -43,6 +43,13 @@ namespace amalgine {
}
+ static void output_gl_error(const char *description) {
+ GLenum error = glGetError();
+ if(error != GL_NO_ERROR) {
+ fprintf(stderr, "Error: failed to %s, reason: %s (%u)\n", description, glewGetErrorString(error), error);
+ }
+ }
+
Texture2D::Texture2D(Image *image)
{
assert(image);
@@ -52,10 +59,16 @@ namespace amalgine {
texture_id = texture_id_allocator->get_free_texture_id();
printf("texture id: %d\n", texture_id);
texture_ref = -1;
+ //output_gl_error("start");
glGenTextures(1, &texture_ref);
+ //output_gl_error("gen textures");
glActiveTexture(GL_TEXTURE0 + texture_id);
+ //output_gl_error("set active texture");
glBindTexture(GL_TEXTURE_2D, texture_ref);
+ //output_gl_error("bind texture");
+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->getData());
+ //output_gl_error("set texture");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
diff --git a/src/main.cpp b/src/main.cpp
index a2ea0b8..04c1001 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -99,6 +99,7 @@ int main()
gpuModel->set({triangles.data(), triangles.size()}, DeviceMemory::StorageType::STATIC);
Texture2D model_texture(image);
+ delete image;
DeviceMemory *model_gpu_texcoords = model_frame.get_input_by_name("texcoord_vert");
model_gpu_texcoords->set({texture_coords.data(), texture_coords.size()}, DeviceMemory::StorageType::STATIC);
@@ -110,6 +111,9 @@ int main()
proj_uniform->set(proj);
tex_uniform->set(model_texture);
+ triangles.resize(0);
+ texture_coords.resize(0);
+
Userdata userdata;
userdata.cam_dist = 3.0f;
glm::vec3 character_pos(0.0f, 0.0f, userdata.cam_dist);
@@ -190,7 +194,6 @@ int main()
glfwSwapBuffers(window);
}
- delete image;
//glfwTerminate();
return 0;
}
@@ -213,6 +216,7 @@ void create_sand(DeviceMemory *triangles, DeviceMemory *texcoords, Texture2D *te
}
Texture2D height_map_texture(height_map_image.unwrap());
+ delete height_map_image.unwrap();
*texture = std::move(height_map_texture);
texcoords->set({height_map_texcoord.data(), height_map_texcoord.size()}, DeviceMemory::StorageType::STATIC);
}
@@ -226,6 +230,12 @@ void glfwErrorHandler(int errorCode, const char *errorDescription)
printf("GLFW error code: %d, description: %s\n", errorCode, errorDescription);
}
+static void GLAPIENTRY gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user_param) {
+ fprintf(stderr, "##### OPENGL ERROR #####\n");
+ fprintf(stderr, "source: %d, type: %d, id: %d, severity: %d, message: %s\n", source, type, id, severity, message);
+ fprintf(stderr, "########################\n");
+}
+
void initGlfw()
{
if(!glfwInit())
@@ -240,6 +250,19 @@ void initGlfw()
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
+
+ // TODO: Only enable in debugging mode
+ glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
+ GLint flags;
+ glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
+ if(flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
+ glEnable(GL_DEBUG_OUTPUT);
+ glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
+ glDebugMessageCallback(gl_debug_callback, nullptr);
+ glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
+ } else {
+ fprintf(stderr, "Warning: failed to enable gl debugging\n");
+ }
}
void initGlew() {
@@ -249,6 +272,7 @@ void initGlew() {
glfwTerminate();
exit(-1);
}
+ glGetError(); // Reset error to none
}
GLFWwindow* createWindow() {
diff --git a/src/model_loader/ObjModelLoader.cpp b/src/model_loader/ObjModelLoader.cpp
index 51d8a74..a42f8d7 100644
--- a/src/model_loader/ObjModelLoader.cpp
+++ b/src/model_loader/ObjModelLoader.cpp
@@ -160,16 +160,16 @@ namespace amalgine {
texture_coords.push_back(temp_texture_coords[texture_coord_indices[1]]);
texture_coords.push_back(temp_texture_coords[texture_coord_indices[2]]);
} else if(num_columns == 4) { // Quad, convert to triangle. TODO: Support rendering quads instead of converting to triangles
- triangles.push_back({ vertices[vertex_indices[0]], vertices[vertex_indices[1]], vertices[vertex_indices[3]] });
- triangles.push_back({ vertices[vertex_indices[3]], vertices[vertex_indices[2]], vertices[vertex_indices[1]] });
+ triangles.push_back({ vertices[vertex_indices[0]], vertices[vertex_indices[1]], vertices[vertex_indices[2]] });
+ triangles.push_back({ vertices[vertex_indices[2]], vertices[vertex_indices[3]], vertices[vertex_indices[0]] });
texture_coords.push_back(temp_texture_coords[texture_coord_indices[0]]);
texture_coords.push_back(temp_texture_coords[texture_coord_indices[1]]);
- texture_coords.push_back(temp_texture_coords[texture_coord_indices[3]]);
+ texture_coords.push_back(temp_texture_coords[texture_coord_indices[2]]);
- texture_coords.push_back(temp_texture_coords[texture_coord_indices[3]]);
texture_coords.push_back(temp_texture_coords[texture_coord_indices[2]]);
- texture_coords.push_back(temp_texture_coords[texture_coord_indices[1]]);
+ texture_coords.push_back(temp_texture_coords[texture_coord_indices[3]]);
+ texture_coords.push_back(temp_texture_coords[texture_coord_indices[0]]);
}
}
}
@@ -245,7 +245,7 @@ namespace amalgine {
Result<Image*> image_result = Image::loadFromFile(texture_path.c_str());
if(!image_result)
- return Result<Material>::Err("Failed to load texture: " + texture_path);
+ return Result<Material>::Err(image_result.getErrorMsg(), image_result.getErrorCode());
image = image_result.unwrap();
break;