Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix texture lod #1060

Merged
merged 7 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 1 addition & 76 deletions apps/yshapeview/yshapeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,13 @@ quads_shape make_cylinders(const vector<vec2i>& lines,
return shape;
}

const char* draw_instanced_vertex_code();

void init_glscene(app_state* app, shade_scene* glscene, generic_shape* ioshape,
progress_callback progress_cb) {
// handle progress
auto progress = vec2i{0, 4};

// init scene
init_scene(glscene);
init_scene(glscene, true);

// compute bounding box
auto bbox = invalidb3f;
Expand Down Expand Up @@ -243,10 +241,6 @@ void init_glscene(app_state* app, shade_scene* glscene, generic_shape* ioshape,
add_instance(glscene, identity3x4f, edges_shape, glmateriale, true);
add_instance(glscene, identity3x4f, vertices_shape, glmaterialv, true);

// override eyelight vertex shader
set_program(glscene->camlight_program, draw_instanced_vertex_code(),
shade_camlight_fragment());

// done
if (progress_cb) progress_cb("convert done", progress.x++, progress.y);
}
Expand Down Expand Up @@ -438,72 +432,3 @@ int main(int argc, const char* argv[]) {
// done
return 0;
}

const char* draw_instanced_vertex_code() {
static const char* code = R"(
#version 330

layout(location = 0) in vec3 positions;
layout(location = 1) in vec3 normals;
layout(location = 2) in vec2 texcoords;
layout(location = 3) in vec4 colors;
layout(location = 4) in vec4 tangents;
layout(location = 5) in vec3 instance_from;
layout(location = 6) in vec3 instance_to;

uniform mat4 frame;
uniform mat4 frameit;
uniform float offset = 0;

uniform mat4 view;
uniform mat4 projection;

out vec3 position;
out vec3 normal;
out vec2 texcoord;
out vec4 color;
out vec4 tangsp;

// main function
void main() {
// copy values
position = positions;
normal = normals;
tangsp = tangents;
texcoord = texcoords;
color = colors;

// normal offset
if (offset != 0) {
position += offset * normal;
}

// world projection
position = (frame * vec4(position, 1)).xyz;
normal = (frameit * vec4(normal, 0)).xyz;
tangsp.xyz = (frame * vec4(tangsp.xyz, 0)).xyz;

if (instance_from != instance_to) {
vec3 dir = instance_to - instance_from;

vec3 up = abs(dir.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, dir));
vec3 bitangent = normalize(cross(dir, tangent));

mat3 mat;
mat[2] = dir;
mat[0] = tangent;
mat[1] = bitangent;
position = mat * position;
normal = mat * normal;
tangent = mat * tangent;
bitangent = mat * bitangent;
}
position += instance_from;

// clip
gl_Position = projection * view * vec4(position, 1);
}
)";
return code;
}
18 changes: 12 additions & 6 deletions libs/yocto_gui/yocto_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ int get_uniform_location(const ogl_program* program, const char* name) {
void set_uniform(const ogl_program* program, int location,
const ogl_texture* texture, int unit) {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture->texture_id);
glBindTexture(GL_TEXTURE_2D, texture ? texture->texture_id : 0);
glUniform1i(location, unit);
assert_ogl_error();
}
Expand All @@ -814,6 +814,9 @@ void set_uniform(const ogl_program* program, int location, int location_on,
glUniform1i(location, unit);
glUniform1i(location_on, 1);
} else {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, 0);
glUniform1i(location, unit);
glUniform1i(location_on, 0);
}
assert_ogl_error();
Expand All @@ -829,7 +832,7 @@ void set_uniform(const ogl_program* program, int location,
const ogl_cubemap* cubemap, int unit) {
assert_ogl_error();
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap->cubemap_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap ? cubemap->cubemap_id : 0);
glUniform1i(location, unit);
assert_ogl_error();
}
Expand All @@ -847,6 +850,9 @@ void set_uniform(const ogl_program* program, int location, int location_on,
glUniform1i(location, unit);
glUniform1i(location_on, 1);
} else {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glUniform1i(location, unit);
glUniform1i(location_on, 0);
}
assert_ogl_error();
Expand Down Expand Up @@ -1119,7 +1125,7 @@ void set_quad_shape(ogl_shape* shape) {
// -----------------------------------------------------------------------------
namespace yocto {

auto ogl_image_vertex =
static auto ogl_image_vertex =
R"(
#version 330
in vec2 positions;
Expand All @@ -1134,7 +1140,7 @@ void main() {
}
)";
#if 0
auto ogl_image_vertex = R"(
static auto ogl_image_vertex = R"(
#version 330
in vec2 positions;
out vec2 frag_texcoord;
Expand All @@ -1148,7 +1154,7 @@ void main() {
}
)";
#endif
auto ogl_image_fragment =
static auto ogl_image_fragment =
R"(
#version 330
in vec2 frag_texcoord;
Expand All @@ -1159,7 +1165,7 @@ void main() {
}
)";
#if 0
auto ogl_image_fragment = R"(
static auto ogl_image_fragment = R"(
#version 330
in vec2 frag_texcoord;
out vec4 frag_color;
Expand Down
Loading