GL not found...but it's there...there!!!

For discussions about game development that does not fit in any of the other topics.
Post Reply
Thunder_X
Posts: 11
Joined: Tue Apr 30, 2013 7:04 pm

GL not found...but it's there...there!!!

Post by Thunder_X »

Hey gang,

Okay, this snippet seems pretty straight4ward, still, it reports that gl is not defined...

Can someone shed some light here?

Thanks;;

--------------------------------------

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>step 3.5.1</title>
<link rel="stylesheet" href="css/styles2.css" type="text/css" charset="utf-8" /></link>
<script type="text/javascript" src="js/page-utils01.js"></script>

<script id="vshader" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;

uniform mat4 uMVMatrix; 
uniform mat4 uPMatrix; 
uniform mat4 uNMatrix; 

uniform vec3 uLightPosition;

varying vec3 vNormal;
varying vec3 vLightRay;
varying vec3 vEyeVec;

void main(void) {

 //Transformed vertex position
 vec4 vertex = uMVMatrix * vec4(aVertexPosition, 1.0);
 
 //Transformed normal position
 vNormal = vec3(uNMatrix * vec4(aVertexNormal, 1.0));

 //Transformed light position
 vec4 light = uMVMatrix * vec4(uLightPosition,1.0);
	
 // Light position
 vLightRay = vertex.xyz-light.xyz;
 
 //Vector Eye
 vEyeVec = -vec3(vertex.xyz);
 
 //Final vertex position
 gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
 
}
</script>

<script id="fshader" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif


uniform vec4 uLightAmbient;
uniform vec4 uLightDiffuse;
uniform vec4 uLightSpecular;

uniform vec4 uMaterialAmbient;
uniform vec4 uMaterialDiffuse;
uniform vec4 uMaterialSpecular;
uniform float uShininess;       

varying vec3 vNormal;
varying vec3 vLightRay;
varying vec3 vEyeVec;

void main(void)
{

    vec3 L = normalize(vLightRay);
    vec3 N = normalize(vNormal);

    //Lambert's cosine law
    float lambertTerm = dot(N,-L);
    
    //Ambient Term  
    vec4 Ia = uLightAmbient * uMaterialAmbient;

    //Diffuse Term
    vec4 Id = vec4(0.0,0.0,0.0,1.0);

    //Specular Term
    vec4 Is = vec4(0.0,0.0,0.0,1.0);

    if(lambertTerm > 0.0)
    {
        Id = uLightDiffuse * uMaterialDiffuse * lambertTerm; 
        vec3 E = normalize(vEyeVec);
        vec3 R = reflect(L, N);
        float specular = pow( max(dot(R, E), 0.0), uShininess);
        Is = uLightSpecular * uMaterialSpecular * specular;
    }

    //Final color
    vec4 finalColor = Ia + Id + Is;
    finalColor.a = 1.0;

    gl_FragColor = finalColor;

}

</script>

<script type="text/javascript">

var gl = null;

function setupWebGL()
{
	canvas = document.getElementById("my-canvas");
	gl = getGLContext(canvas);

	initViewPort(gl, canvas);	
	initShaders();
} 

function initShaders()
{
	// get the sources
	
}

function makeShader(src, type)
{
	{
	
	}
	
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) 
{
	alert("Error compiling shader: " + gl.getShaderInfoLog(shader));
}
return shader;
}

function initViewPort(gl, canvas)
{
	gl.viewport(0,0,canvas.width, canvas.height);
	
}

</script>
</head>
<body onload="setupWebGL();">
<canvas id="my-canvas" name="my-canvas" width="400" height="300">
Your browser does not support the HTML5 canvas element.
</canvas>
</body>
</html>
--------------------------------------

The lib linked in is only to declare some vars and for the code of the error handler...

Thanks in advance...
Thunder
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: GL not found...but it's there...there!!!

Post by Chris »

Fighting for peace is declaring war on war. If you want peace be peaceful.
Thunder_X
Posts: 11
Joined: Tue Apr 30, 2013 7:04 pm

Re: GL not found...but it's there...there!!!

Post by Thunder_X »

Thanks! Off to the link!!! :)

Edit : shoot me...this was the funtion

Code: Select all

		function getGLContext(canvas)
		{	
		
		var gl;
			if (canvas == null)
				{
					alert("there is no canvas on this page");
					return;
				}
		
		for (var i = 0; i < names.length; ++i) 
		{
			try 
			{
				gl = canvas.getContext(names[i]);
			}
			catch(e) 
			{
			
			}
			if (gl) 
				{
					alert(names[i]);
					break;
				}
		}

		if (gl == null)
		{
			alert("WebGL is not available");
		}
		else
		{
			gl.clearColor(0.0, 0.0, 0.0, 1.0);
			gl.clear(gl.COLOR_BUFFER_BIT);
         gl.viewportWidth = canvas.width;
         gl.viewportHeight = canvas.height;
		}
	[color=#FF0000][b]return gl;[/b][/color]
	}
The line in red, I just added...it's a function with this as call...

Code: Select all

gl = getGLContext(canvas);
...but all the time, the function returned nothing...

Sorry for the panic... :)

Thunder
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: GL not found...but it's there...there!!!

Post by Jackolantern »

I know next to nothing about WebGL, but man, I didn't realize it was basically in another language! If there is a strongly-typed, traditional OOP language hiding in the browser, why can't we use it for DOM manipulation! :o
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: GL not found...but it's there...there!!!

Post by Chris »

That's shader language.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: GL not found...but it's there...there!!!

Post by Jackolantern »

Chris wrote:That's shader language.
I know, but I guess it is just annoying to see something statically typed with main() methods, etc. that the browser can do something with. Don't get me wrong, I do like JS, but sometimes I just wish we had something a little bit more traditional OOP.
The indelible lord of tl;dr
Thunder_X
Posts: 11
Joined: Tue Apr 30, 2013 7:04 pm

Re: GL not found...but it's there...there!!!

Post by Thunder_X »

Using embedded Shader Language is in fact a great idea, yet be in somewhat confusing at first.
It is embeded in <script> tags, however, since the type is set to anything but "javascript" the browser "diplomatically" ignores it, and it's that same characteristic that makes it detect is the canvas tag is recognised, if it is, the browser will ignore the gfail message in between. innit cool?
But, the best part is that this shader language let's YOU do what YOU want done, because WebGL uses a programmable pipeline, that's what needs to be done: provide embedded source to heve it compile at run time.
Of course, once that shader is set up, chances are slim to none you'll ever need to tinker ith it...
And, yes, it(s a different language...and the [url:http://www.khronos.org/registry/gles/sp ... 1.0.17.pdf]specification[/url] at Khronos takes some 119 patges...
Of course, I just re discovered Three.js, not really my favorite as it hides too much, but you dont need to worry about shaders...

At this time I stuggle wilh loading/handling multiple models...

All is a learning path...

Thunder
Post Reply

Return to “General Development”