Skip to content

Commit f233627

Browse files
committed
fix bugs with texture mapping; add textures demo
1 parent bbb6670 commit f233627

File tree

6 files changed

+74
-13
lines changed

6 files changed

+74
-13
lines changed

461223191.jpg

90 KB
Loading

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ a rotating, pulsating box.
3838
* [surfaces.html](https://peteroupc.github.io/html3dutil/surfaces.html) - Demonstrates
3939
using evaluators to generate parametric surfaces.
4040
* [stl.html](https://peteroupc.github.io/html3dutil/stl.html) - Demonstrates loading 3D models.
41+
* [textured.html](https://peteroupc.github.io/html3dutil/textured.html) - Demonstrates loading textures
42+
and applying them to 3D shapes.
4143

4244
Examples
4345
---------

glutil-binders.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ function LoadedTexture(textureImage, context){
6262
if(GLUtil._isPowerOfTwo(textureImage.image.width) &&
6363
GLUtil._isPowerOfTwo(textureImage.image.height)){
6464
context.generateMipmap(context.TEXTURE_2D);
65+
} else {
66+
context.texParameteri(context.TEXTURE_2D,
67+
context.TEXTURE_MIN_FILTER, context.LINEAR);
68+
context.texParameteri(context.TEXTURE_2D,
69+
context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
70+
context.texParameteri(context.TEXTURE_2D,
71+
context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
6572
}
6673
}
6774

@@ -145,7 +152,8 @@ TextureBinder.prototype.bind=function(program){
145152
// set magnification
146153
context.texParameteri(context.TEXTURE_2D,
147154
context.TEXTURE_MAG_FILTER, context.LINEAR);
148-
if(texture.powerOfTwo){
155+
if(GLUtil._isPowerOfTwo(texture.width) &&
156+
GLUtil._isPowerOfTwo(texture.height)){
149157
// Enable mipmaps if texture's dimensions are powers of two
150158
context.texParameteri(context.TEXTURE_2D,
151159
context.TEXTURE_MIN_FILTER, context.LINEAR_MIPMAP_LINEAR);

glutil.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,8 +1593,12 @@ Scene3D.prototype.loadTexture=function(name){
15931593
* object), and is rejected when an error occurs.
15941594
*/
15951595
Scene3D.prototype.loadAndMapTexture=function(name){
1596-
return Texture.loadAndMapTexture(
1597-
name, this.context, this.textureCache);
1596+
var context=this.context;
1597+
return Texture.loadTexture(
1598+
name, this.context, this.textureCache).then(function(texture){
1599+
texture.loadedTexture=new LoadedTexture(texture,context);
1600+
return texture;
1601+
});
15981602
}
15991603
/**
16001604
* Loads one or more textures from an image URL and uploads each of them
@@ -1615,10 +1619,7 @@ Scene3D.prototype.loadAndMapTextures=function(textureFiles, resolve, reject){
16151619
var context=this.context;
16161620
for(var i=0;i<textureFiles.length;i++){
16171621
var objf=textureFiles[i];
1618-
var p=this.loadAndMapTexture(objf).then(function(texture){
1619-
texture.loadedTexture=new LoadedTexture(texture,context);
1620-
});
1621-
promises.push(p);
1622+
promises.push(this.loadAndMapTexture(objf));
16221623
}
16231624
return GLUtil.getPromiseResults(promises, resolve, reject);
16241625
}

glutil_min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

textured.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<meta charset=utf-8>
2+
<head>
3+
<script type="text/javascript" src="glutil_min.js"></script>
4+
<script type="text/javascript" src="frame.js"></script>
5+
</head>
6+
<body>
7+
<canvas width="600" height="450" id=canvas></canvas>
8+
<p>Texture courtesy of pdtextures.blogspot.com</p>
9+
<script id="demo">
10+
//<!--
11+
/*
12+
Written by Peter O. in 2015.
13+
14+
Any copyright is dedicated to the Public Domain.
15+
http://creativecommons.org/publicdomain/zero/1.0/
16+
If you like this, you should donate to Peter O.
17+
at: http://upokecenter.dreamhosters.com/articles/donate-now-2/
18+
*/
19+
// Create the 3D scene; find the HTML canvas and pass it
20+
// to Scene3D.
21+
var scene=new Scene3D(document.getElementById("canvas"));
22+
var fc=new FrameCounterDiv(scene)
23+
scene.setPerspective(45,scene.getAspect(),0.1,100);
24+
scene.setLookAt([0,0,40]);
25+
scene.loadAndMapTextures([
26+
"461223191.jpg"
27+
]).then(function(results){
28+
var mesh=Meshes.createBox(10,20,20);
29+
var shape=scene.makeShape(mesh).setTexture(results.successes[0])
30+
scene.addShape(shape);
31+
var rotation=[0,0,0];
32+
GLUtil.renderLoop(function(){
33+
fc.update();
34+
rotation[0]+=.5; // Adjust x-rotation by .5 degree
35+
rotation[1]+=1.0; // Adjust y-rotation by 1 degree
36+
var q=GLMath.quatFromTaitBryan(rotation);
37+
shape.setQuaternion(q);
38+
scene.render();
39+
});
40+
})
41+
//-->
42+
</script>
43+
<script>
44+
window.addEventListener("load",function(){
45+
var e=document.createElement("pre")
46+
e.innerHTML=document.getElementById("demo").textContent.replace(/</g,"&lt;")
47+
document.body.appendChild(e)
48+
})
49+
</script>
50+
</body>

0 commit comments

Comments
 (0)