Question

I have already learned how can I add some objects in WebGL, rotate them.

But I have a problem with a texturing process.

The source code of my WebGL app:

<html>
<head>
<title>3D Plane Map</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
 
html, body, #canvas-main {
    width: 100%;
    height: 100%;
    min-height: 500px;
    min-width: 900px;
    margin: 0px;
}
 
</style>
<script type="text/javascript" src="scripts/glMatrix.js"></script>
<script type="text/javascript" src="scripts/webgl-utils.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
 
    precision mediump float;
        varying vec2 vTextureCoord;
        uniform sampler2D uSampler;
 
    void main(void) {
        gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
    }
       
</script>
<script id="shader-vs" type="x-shader/x-vertex">
 
    attribute vec3 aVertexPosition;
        attribute vec2 aTextureCoord;
        varying vec2 vTextureCoord;
 
    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
 
    void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
                vTextureCoord = aTextureCoord;
    }
       
</script>
<script type="text/javascript">
 
    var gl;
       
    function initGL(canvas) {
        try {
            gl = canvas.getContext("experimental-webgl");
            gl.viewportWidth = canvas.width;
            gl.viewportHeight = canvas.height;
        } catch (e) {
        }
        if (!gl) {
            alert("Could not initialise WebGL, sorry :-(");
        }
    }
 
    function getShader(gl, id) {
       
        var shaderScript = document.getElementById(id);
               
        if (!shaderScript) {
            return null;
        }
 
        var str = "";
        var k = shaderScript.firstChild;
               
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }
 
        var shader;
               
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }
 
        gl.shaderSource(shader, str);
        gl.compileShader(shader);
 
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }
 
        return shader;
    }
 
 
    var shaderProgram;
 
    function initShaders() {
       
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");
 
        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);
 
        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }
 
        gl.useProgram(shaderProgram);
 
        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
               
                shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
        gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
 
        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
                shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
    }
 
        function handleLoadedTexture(texture) {
       
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.bindTexture(gl.TEXTURE_2D, null);
    }
       
        var myTexture;
 
    function initTexture() {
       
        myTexture = gl.createTexture();
        myTexture.image = new Image();
        myTexture.image.onload = function () {
               
            handleLoadedTexture(myTexture)
        }
 
        myTexture.image.src = "moscow.jpg";
    }
       
    var mvMatrix = mat4.create();
    var pMatrix = mat4.create();
        var mvMatrixStack = [];
 
        function mvPushMatrix() {
       
        var copy = mat4.create();
        mat4.set(mvMatrix, copy);
        mvMatrixStack.push(copy);
    }
 
    function mvPopMatrix() {
       
        if (mvMatrixStack.length == 0) {
            throw "Invalid popMatrix!";
        }
               
        mvMatrix = mvMatrixStack.pop();
    }
       
    function setMatrixUniforms() {
       
        gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
        gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    }
 
        function degToRad(degrees) {
       
        return degrees * Math.PI / 180;
    }
 
    var squareVertexPositionBuffer;
        var squareVertexTextureCoordBuffer;
    var squareVertexIndexBuffer;
 
    function initBuffers() {
       
        squareVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        vertices = [
             1.0,  1.0,  0.0,
            -1.0,  1.0,  0.0,
             1.0, -1.0,  0.0,
            -1.0, -1.0,  0.0
        ];
               
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        squareVertexPositionBuffer.itemSize = 3;
        squareVertexPositionBuffer.numItems = 4;
               
                squareVertexTextureCoordBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureCoordBuffer);
       
                var textureCoords = [
                         0.0, 0.0,
                         1.0, 0.0,
                         1.0, 1.0,
                         0.0, 1.0,
                ];
               
                gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
        squareVertexTextureCoordBuffer.itemSize = 2;
        squareVertexTextureCoordBuffer.numItems = 4;
    }
 
       
        var rSquare = 0;
       
    function drawScene() {
       
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
 
        mat4.perspective(45, gl.viewportWidth / gl.viewportHeight / 3, 0.1, 100.0, pMatrix);
        mat4.identity(mvMatrix);
        mat4.translate(mvMatrix, [0.0, 0.0, -5.0]);
               
                mvPushMatrix();
        mat4.rotate(mvMatrix, degToRad(rSquare), [1, 0, 0]);
               
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
               
                gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureCoordBuffer);
        gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, squareVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
 
        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, myTexture);
        gl.uniform1i(shaderProgram.samplerUniform, 0);
               
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);
                mvPopMatrix();
    }
       
 
        var lastTime = 0;
 
    function animate() {
       
        var timeNow = new Date().getTime();
               
        if (lastTime != 0) {
            var elapsed = timeNow - lastTime;
            rSquare += (75 * elapsed) / 1000.0;
        }
               
        lastTime = timeNow;
    }
 
    function tick() {
       
        requestAnimFrame(tick);
        drawScene();
        animate();
    }
       
    function webGLStart() {
       
        var canvas = document.getElementById("canvas-main");
 
        initGL(canvas);
        initShaders();
        initBuffers();
                initTexture();
 
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);
        drawScene();
                tick();
    }
 
</script>
</head>
<body onload="webGLStart();">
    <canvas id="canvas-main" style="border: none; width: 100%; height: 100%;"></canvas>
</body>
</html>

When I launch Chrome debugger, it gives me such an erros:

PS I've tried to search in web some info, but there isn't much information for my issue, just few links.

[.WebGLRenderingContext]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete' 192.168.1.179/plane/:1
50
WebGL: drawArrays: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete'. Or the texture is Float or Half Float type with linear filtering while OES_float_linear or OES_half_float_linear extension is not enabled. 192.168.1.179/plane/:1
202
WebGL: drawArrays: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete'. Or the texture is Float or Half Float type with linear filtering while OES_float_linear or OES_half_float_linear extension is not enabled. 192.168.1.179/plane/:244
WebGL: too many errors, no more errors will be reported to the console for this context. 192.168.1.179/plane/:244
Was it helpful?

Solution

The answer was clear as a crystal. Sorry for my inattention.

The problem was, because for mipmapping the texture must be the integer type of power-of-2.

My texture was 400x300, that's why such an error occurs, when I changed to the 512x512 the error has solved.

If you want to check, that your integer is the power-of-2, then use such simple formula:

A & (A-1) == 0

OTHER TIPS

Actually that check in itself is not enough, it is not valid for zero (0).

Instead, consider bool isPowerOfTwo = A && !( A & ( A - 1 ));

Source: https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top