Skip to content

Commit eb51fea

Browse files
tzukpolinskychristian-rauch
authored andcommitted
working with multiple textures
1 parent b91d462 commit eb51fea

File tree

3 files changed

+167
-37
lines changed

3 files changed

+167
-37
lines changed

components/pango_geometry/src/geometry_obj.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ pangolin::Geometry LoadGeometryObj(const std::string& filename)
101101
PANGO_ASSERT(attrib.texcoords.size() % 2 == 0);
102102

103103
// Load textures - a bit of a hack for now.
104-
for(size_t i=0; i < materials.size(); ++i) {
105-
if(!materials[i].diffuse_texname.empty()) {
106-
const std::string tex_name = FormatString("texture_%",i);
104+
for(const auto& material : materials) {
105+
if(!material.diffuse_texname.empty()) {
107106
try {
108-
TypedImage& tex_image = geom.textures[tex_name];
109-
tex_image = LoadImage(PathParent(filename) + "/" + materials[i].diffuse_texname);
107+
TypedImage& tex_image = geom.textures[material.name];
108+
tex_image = LoadImage(PathParent(filename) + "/" + material.diffuse_texname);
110109
const int row_bytes = tex_image.w * tex_image.fmt.bpp / 8;
111110
std::vector<unsigned char> tmp_row(row_bytes);
112111
for (std::size_t y=0; y < (tex_image.h >> 1); ++y) {
@@ -115,8 +114,8 @@ pangolin::Geometry LoadGeometryObj(const std::string& filename)
115114
std::memcpy(tex_image.RowPtr(tex_image.h - 1 - y), tmp_row.data(), row_bytes);
116115
}
117116
} catch(const std::exception&) {
118-
pango_print_warn("Unable to read texture '%s'\n", tex_name.c_str());
119-
geom.textures.erase(tex_name);
117+
pango_print_warn("Unable to read texture '%s'\n", material.name.c_str());
118+
geom.textures.erase(material.name);
120119
}
121120
}
122121
}

components/pango_glgeometry/src/glgeometry.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,47 +96,50 @@ void GlDraw(GlSlProgram& prog, const GlGeometry& geom, const GlTexture* matcap)
9696
{
9797
// Bind textures
9898
int num_tex_bound = 0;
99-
for(auto& tex : geom.textures) {
100-
glActiveTexture(GL_TEXTURE0 + num_tex_bound);
101-
tex.second.Bind();
102-
prog.SetUniform(tex.first, (int)num_tex_bound);
103-
++num_tex_bound;
99+
for (const auto& buffer: geom.buffers) {
100+
BindGlElement(prog, buffer.second);
104101
}
105-
106102
if(matcap) {
107103
glActiveTexture(GL_TEXTURE0 + num_tex_bound);
108104
matcap->Bind();
109105
prog.SetUniform("matcap", (int)num_tex_bound);
110106
++num_tex_bound;
111-
}
112-
113-
// Bind all attribute buffers
114-
for(auto& buffer : geom.buffers) {
115-
BindGlElement(prog, buffer.second);
116-
}
117-
118-
// Draw all geometry
119-
for(auto& buffer : geom.objects) {
120-
auto it_indices = buffer.second.attributes.find("vertex_indices");
121-
if(it_indices != buffer.second.attributes.end()) {
122-
buffer.second.Bind();
123-
auto& attrib = it_indices->second;
124-
glDrawElements(
125-
GL_TRIANGLES, attrib.count_per_element * attrib.num_elements,
126-
attrib.gltype, reinterpret_cast<uint8_t*>(attrib.offset)
127-
);
128-
buffer.second.Unbind();
107+
}else{
108+
std::vector<std::string> texturesNames;
109+
for (auto& tex: geom.textures) {
110+
texturesNames.emplace_back(tex.first);
129111
}
130-
}
112+
for (auto& texName: texturesNames) {
113+
glActiveTexture(GL_TEXTURE0 + num_tex_bound);
114+
geom.textures.at(texName).Bind();
115+
prog.SetUniform("texture", (int) num_tex_bound);
116+
117+
auto& buffer = *geom.objects.equal_range(texName).first;
118+
auto it_indices = buffer.second.attributes.find("vertex_indices");
119+
if (it_indices != buffer.second.attributes.end()) {
120+
buffer.second.Bind();
121+
auto& attrib = it_indices->second;
122+
glDrawElements(
123+
GL_TRIANGLES, attrib.count_per_element * attrib.num_elements,
124+
attrib.gltype, reinterpret_cast<uint8_t *>(attrib.offset)
125+
);
126+
buffer.second.Unbind();
127+
128+
}
129+
130+
++num_tex_bound;
131131

132-
// Unbind attribute buffers
133-
for(auto& buffer : geom.buffers) {
134-
UnbindGlElements(prog, buffer.second);
132+
}
135133
}
136134

137135
// Unbind textures
138136
glBindTexture(GL_TEXTURE_2D, 0);
139137
glActiveTexture(GL_TEXTURE0);
138+
139+
// Unbind attribute buffers
140+
for (const auto& buffer: geom.buffers) {
141+
UnbindGlElements(prog, buffer.second);
142+
}
140143
}
141144

142145
}

tools/ModelViewer/shader.h

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ const std::string default_model_shader = R"Shader(
3030
#elif SHOW_TEXTURE
3131
attribute vec2 uv;
3232
varying vec2 vUV;
33+
attribute vec3 normal;
34+
varying vec3 vNormal;
3335
void main() {
3436
vUV = uv;
37+
vNormal = mat3(T_cam_norm) * normal;
38+
3539
#elif SHOW_MATCAP
3640
attribute vec3 normal;
3741
varying vec3 vNormalCam;
@@ -65,7 +69,9 @@ const std::string default_model_shader = R"Shader(
6569
varying vec3 vNormal;
6670
#elif SHOW_TEXTURE
6771
varying vec2 vUV;
68-
uniform sampler2D texture_0;
72+
uniform sampler2D texture;
73+
varying vec3 vNormal;
74+
6975
#elif SHOW_MATCAP
7076
varying vec3 vNormalCam;
7177
uniform sampler2D matcap;
@@ -81,7 +87,8 @@ void main() {
8187
#elif SHOW_NORMAL
8288
gl_FragColor = vec4((vNormal + vec3(1.0,1.0,1.0)) / 2.0, 1.0);
8389
#elif SHOW_TEXTURE
84-
gl_FragColor = texture2D(texture_0, vUV);
90+
vec4((vNormal + vec3(1.0,1.0,1.0)) / 2.0, 1.0);
91+
gl_FragColor = texture2D(texture, vUV);
8592
#elif SHOW_MATCAP
8693
vec2 uv = 0.5 * vNormalCam.xy + vec2(0.5, 0.5);
8794
gl_FragColor = texture2D(matcap, uv);
@@ -92,7 +99,128 @@ void main() {
9299
#endif
93100
}
94101
)Shader";
102+
const std::string uv_shader = R"Shader(
103+
/////////////////////////////////////////
104+
@start vertex
105+
#version 120
106+
107+
uniform mat4 T_cam_norm;
108+
uniform mat4 KT_cw;
109+
attribute vec3 vertex;
110+
attribute vec2 uv;
111+
varying vec2 vUV;
112+
void main() {
113+
vUV = uv;
114+
gl_Position = KT_cw * vec4(vertex, 1.0);
115+
}
116+
117+
/////////////////////////////////////////
118+
@start fragment
119+
#version 120
120+
varying vec2 vUV;
121+
122+
void main() {
123+
gl_FragColor = vec4(vUV,1.0-vUV.x,1.0);
124+
}
125+
)Shader";
95126

127+
const std::string color_shader = R"Shader(
128+
/////////////////////////////////////////
129+
@start vertex
130+
#version 120
131+
132+
uniform mat4 T_cam_norm;
133+
uniform mat4 KT_cw;
134+
attribute vec3 vertex;
135+
attribute vec4 color;
136+
varying vec4 vColor;
137+
void main() {
138+
vColor = color;
139+
gl_Position = KT_cw * vec4(vertex, 1.0);
140+
}
141+
142+
/////////////////////////////////////////
143+
@start fragment
144+
#version 120
145+
varying vec4 vColor;
146+
147+
void main() {
148+
gl_FragColor = vColor;
149+
}
150+
)Shader";
151+
const std::string matcap_shader = R"Shader(
152+
/////////////////////////////////////////
153+
@start vertex
154+
#version 120
155+
156+
uniform mat4 T_cam_norm;
157+
uniform mat4 KT_cw;
158+
attribute vec3 normal;
159+
varying vec3 vNormalCam;
160+
void main() {
161+
vNormalCam = mat3(T_cam_norm) * normal;
162+
gl_Position = KT_cw * vec4(vertex, 1.0);
163+
}
164+
165+
/////////////////////////////////////////
166+
@start fragment
167+
#version 120
168+
varying vec3 vNormalCam;
169+
uniform sampler2D matcap;
170+
void main() {
171+
vec2 uv = 0.5 * vNormalCam.xy + vec2(0.5, 0.5);
172+
gl_FragColor = texture2D(matcap, uv);
173+
}
174+
)Shader";
175+
176+
const std::string normal_shader = R"Shader(
177+
/////////////////////////////////////////
178+
@start vertex
179+
#version 120
180+
181+
uniform mat4 T_cam_norm;
182+
uniform mat4 KT_cw;
183+
attribute vec3 vertex;
184+
attribute vec3 normal;
185+
varying vec3 vNormal;
186+
void main() {
187+
vNormal = mat3(T_cam_norm) * normal;
188+
gl_Position = KT_cw * vec4(vertex, 1.0);
189+
}
190+
191+
/////////////////////////////////////////
192+
@start fragment
193+
#version 120
194+
varying vec3 vNormal;
195+
void main() {
196+
gl_FragColor = vec4((vNormal + vec3(1.0,1.0,1.0)) / 2.0, 1.0);
197+
}
198+
)Shader";
199+
const std::string texture_shader = R"Shader(
200+
/////////////////////////////////////////
201+
@start vertex
202+
#version 120
203+
204+
205+
uniform mat4 KT_cw;
206+
attribute vec3 vertex;
207+
attribute vec2 uv;
208+
varying vec2 vUV;
209+
void main() {
210+
vUV = uv;
211+
gl_Position = KT_cw * vec4(vertex, 1.0);
212+
}
213+
214+
/////////////////////////////////////////
215+
@start fragment
216+
#version 120
217+
varying vec2 vUV;
218+
uniform sampler2D texture;
219+
220+
void main() {
221+
gl_FragColor = texture2D(texture, vUV);
222+
}
223+
)Shader";
96224
const std::string equi_env_shader = R"Shader(
97225
/////////////////////////////////////////
98226
@start vertex

0 commit comments

Comments
 (0)