Questions about ES2/GL3 shaders

All topics about ZGameEditor goes here.

Moderator: Moderators

User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

Image
Matrixes... :roll:
Yeah, I saw you complain here and there regarding matrix and shaders already.

Thanks for your example. So I played a bit with it in order to comprehend how it's working. I guess I need to do the same on all three axes, right? But I'm struggling with the Z axis rotation. Then I searched through the forum for other Matrix examples, then I discovered that one could "add from library" by right-clicking the Project Tree in ZGE. That's a neat but really hidden feature... :lol:

So now I'm playing with the Matrix Library, trying to get the correct rotation on all three axes from that.
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

I managed to get a correct rotation transformation on all three axes with the Matrix Library. I had to multiply the matrices in a certain order (I'm discovering matrices here).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0.5 0.5 0.5 1" CameraPosition="-7.2006 2 3.4859" CameraRotation="0 1.1782 0" RenderOrder="1" FileVersion="2">
  <OnLoaded>
    <ZLibrary Comment="Matrix Library">
      <Source>
<![CDATA[// Returns a identity matrix.

mat4 matrixIdentity()
{
  mat4 m;

  m[0,0] = 1;
  m[1,1] = 1;
  m[2,2] = 1;
  m[3,3] = 1;

  return m;
}

// Returns a translation matrix.

mat4 matrixFromTranslation(vec3 translation)
{
  mat4 m;

  m[0,0] = 1;
  m[1,1] = 1;
  m[2,2] = 1;
  m[3,3] = 1;

  m[3,0] = translation.x;
  m[3,1] = translation.y;
  m[3,2] = translation.z;

  return m;
}

// Returns a scale matrix.

mat4 matrixFromScale(vec3 scale)
{
  mat4 m;

  m[0,0] = scale.x;
  m[1,1] = scale.y;
  m[2,2] = scale.z;
  m[3,3] = 1;

  return m;
}

// Returns a x-axis rotation matrix.

mat4 matrixFromRotationX(float angle)
{
  angle *= PI*2;

  float s = sin(angle);
  float c = cos(angle);

  mat4 m;

  m[1,1] = c; m[2,1] = 0-s;
  m[1,2] = s; m[2,2] = c;

  m[0,0] = 1;
  m[3,3] = 1;

  return m;
}

// Returns a y-axis rotation matrix.

mat4 matrixFromRotationY(float angle)
{
  angle *= PI*2;

  float s = sin(angle);
  float c = cos(angle);

  mat4 m;

  m[0,0] = c;   m[2,0] = s;
  m[0,2] = 0-s; m[2,2] = c;

  m[1,1] = 1;
  m[3,3] = 1;

  return m;
}

// Returns a z-axis rotation matrix.

mat4 matrixFromRotationZ(float angle)
{
  angle *= PI*2;

  float s = sin(angle);
  float c = cos(angle);

  mat4 m;

  m[0,0] = c; m[1,0] = 0-s;
  m[0,1] = s; m[1,1] = c;

  m[2,2] = 1;
  m[3,3] = 1;

  return m;
}

// Returns a euler-angles matrix.

mat4 matrixFromEuler(vec3 rotation)
{
  float sx, cx, sy, cy, sz, cz;

  // Would have been nice if you could do "rotation *= PI*2" :-(
  rotation.x *= PI*2;
  rotation.y *= PI*2;
  rotation.z *= PI*2;

  sx = sin(rotation.x);
  cx = cos(rotation.x);

  sy = sin(rotation.y);
  cy = cos(rotation.y);

  sz = sin(rotation.z);
  cz = cos(rotation.z);

  mat4 m;

  m[0,0] = cy*cz; m[1,0] = sx*cz*sy-cx*sz; m[2,0] = cx*cz*sy+sx*sz;
  m[0,1] = cy*sz; m[1,1] = sx*sz*sy+cx*cz; m[2,1] = cx*sz*sy-sx*cz;
  m[0,2] = 0-sy;  m[1,2] = sx*cy;          m[2,2] = cx*cy;

  m[3,3] = 1;

  return m;
}

// Returns a axis-angle matrix.

mat4 matrixFromAxisAngle(vec3 axis, float angle)
{
  float s, c, x, y, z, t;

  angle *= PI*2;

  s = sin(angle);
  c = cos(angle);

  x = axis.x;
  y = axis.y;
  z = axis.z;

  t = 1-c;

  mat4 m;

  m[0,0] = t*x*x+c;   m[1,0] = t*x*y-s*z; m[2,0] = t*x*z+s*y;
  m[0,1] = t*x*y+s*z; m[1,1] = t*y*y+c;   m[2,1] = t*y*z-s*x;
  m[0,2] = t*x*z-s*y; m[1,2] = t*y*z+s*x; m[2,2] = t*z*z+c;

  m[3,3] = 1;

  return m;
}

// Returns a quaternion matrix.

mat4 matrixFromQuaternion(vec4 quaternion)
{
  float x, y, z, w, x2, y2, z2;

  x = quaternion.x;
  y = quaternion.y;
  z = quaternion.z;
  w = quaternion.w;

  x2 = x+x;
  y2 = y+y;
  z2 = z+z;

  mat4 m;

  m[0,0] = 1-(y*y2+z*z2); m[1,0] =    x*y2-w*z2;  m[2,0] =    x*z2+w*y2;
  m[0,1] =    x*y2-w*z2;  m[1,1] = 1-(x*x2+z*z2); m[2,1] =    y*z2-w*x2;
  m[0,2] =    x*z2-w*y2;  m[1,2] =    y*z2+w*x2;  m[2,2] = 1-(x*x2+y*y2);

  m[3,3] = 1;

  return m;
}

// Returns a model matrix.

mat4 matrixFromModel(model q)
{
  // Would have been nice if you could do "mat4 m = matrixFromEuler(q.rotation)" :-(
  vec3 rotation = vector3(q.rotation.x, q.rotation.y, q.rotation.z);
  mat4 m = matrixFromEuler(rotation);

  m[3,0] = q.position.x;
  m[3,1] = q.position.y;
  m[3,2] = q.position.z;

  for(int i=0; i<3; i++)
  {
    m[0,i] *= q.scale.x;
    m[1,i] *= q.scale.y;
    m[2,i] *= q.scale.z;
  }

  return m;
}]]>
      </Source>
    </ZLibrary>
    <SpawnModel Model="Box"/>
  </OnLoaded>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[// Get the current modelViewMatrix ( which contains the viewMatrix at this point )

getMatrix(0, ViewMatrix);]]>
      </Expression>
    </ZExpression>
  </OnRender>
  <Content>
    <Variable Name="ViewMatrix" Type="5"/>
    <Model Name="Box">
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[// Animate the box

Box.Position.Y = abs(sin(App.Time))*2.5+1;
Box.Position.X = sin(App.Time)*2;
Box.Position.Z = cos(App.Time)*2;
Box.Rotation.X = App.Time/PI;
Box.Rotation.Y = App.Time/PI;
Box.Rotation.Z = App.Time/PI;]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="BoxMaterial"/>
        <RenderMesh Mesh="BoxMesh"/>
        <ZExpression>
          <Expression>
<![CDATA[
mat4 mt = matrixFromTranslation(vector3(CurrentModel.Position.X, 0, CurrentModel.Position.Z));
mat4 ms = matrixFromScale(vector3(1,0,1));
mat4 mx = matrixFromRotationX(CurrentModel.Rotation.X);
mat4 my = matrixFromRotationY(CurrentModel.Rotation.Y);
mat4 mz = matrixFromRotationZ(CurrentModel.Rotation.Z);
setMatrix(0, ViewMatrix * mt * ms * mz * my * mx);]]>
          </Expression>
        </ZExpression>
        <RenderMesh Mesh="BoxMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox/>
        <MeshBox Scale="2 0.1 0.1"/>
        <MeshCombine/>
        <MeshBox Scale="0.4 1.5 0.4"/>
        <MeshCombine/>
      </Producers>
    </Mesh>
    <Material Name="BoxMaterial" Shading="2" Light="0"/>
  </Content>
</ZApplication>
Now I'm going to try with what Kjell sent me :wink:
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

I’m totally into shaders now, so I have a few more questions :lol:

1. How can I obtain a unique number per model in a shader?
I tried vec3 modelSeed = position.xyz; but it’s not enough. Can I pass a random number from the currently rendered model to its shader?

2. I’ve noticed sometimes shaders take time to display. Maybe they take time to compile or compute, I’m not sure. So I was wondering: do they stay in memory once they’ve been used? If that’s the case, can I precompute them at the start of the game and reuse them later without delay?
User avatar
VilleK
Site Admin
Posts: 2389
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Questions about ES2/GL3 shaders

Post by VilleK »

Ats wrote: Wed Jul 02, 2025 3:53 pm Can I pass a random number from the currently rendered model to its shader?
You could pass a value using the ShaderVariable. The Shader needs to have the UpdateVarsOnEachUse property set to make sure the variables update for each model.
Ats wrote: Wed Jul 02, 2025 3:53 pm 2. I’ve noticed sometimes shaders take time to display.
This sounds like shader compilation. It is compiled the first time it is used. I guess the only way around that is to render something small using each shader during application initialisation.
User avatar
Kjell
Posts: 1939
Joined: Sat Feb 23, 2008 11:15 pm

Re: Questions about ES2/GL3 shaders

Post by Kjell »

Hi Ats,
Ats wrote: Wed Jul 02, 2025 3:53 pmCan I pass a random number from the currently rendered model to its shader?
That's what uniform variables are for :wink: Unfortunately, there's a issue with ShaderVariable. Since UseMaterial is ignored when the selected material is already active, any changes to ShaderVariable will be skipped in that case too ( regardless of UpdateVarsOnEachUse ). You can work around this by temporarily switching to a dummy material, like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[// Spawn 4 models

for(int i=0; i<4; i++)
{
  Sprite.Position.X = random(0, 4);
  Sprite.Position.Y = random(0, 2);

  createModel(Sprite);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Material Name="DummyMaterial"/>
    <Model Name="Sprite">
      <OnRender>
        <ZExpression>
          <Expression>
<![CDATA[//

SpriteRandom.Value = Sprite.Personality;]]>
          </Expression>
        </ZExpression>
        <UseMaterial Material="DummyMaterial"/>
        <UseMaterial Material="SpriteMaterial"/>
        <RenderSprite/>
      </OnRender>
    </Model>
    <Material Name="SpriteMaterial" Shader="SpriteShader"/>
    <Shader Name="SpriteShader" UpdateVarsOnEachUse="255">
      <VertexShaderSource>
<![CDATA[//

void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

uniform float random;

void main()
{
  gl_FragColor = vec4(random, 1-random, 1, 1);
}]]>
      </FragmentShaderSource>
      <UniformVariables>
        <ShaderVariable Name="SpriteRandom" VariableName="random" Value="0.554"/>
      </UniformVariables>
    </Shader>
  </Content>
</ZApplication>
But for your specific situation you might want to use a glUniform call instead, for example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

void glUniform1f(int location, float v0){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[// Spawn 4 models

for(int i=0; i<4; i++)
{
  Sprite.Position.X = random(0, 4);
  Sprite.Position.Y = random(0, 2);

  createModel(Sprite);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Sprite">
      <OnRender>
        <UseMaterial Material="SpriteMaterial"/>
        <ZExpression>
          <Expression>
<![CDATA[// Modify uniform value of active shader

glUniform1f(0, Sprite.Personality);]]>
          </Expression>
        </ZExpression>
        <RenderSprite/>
      </OnRender>
    </Model>
    <Material Name="SpriteMaterial" Shader="SpriteShader"/>
    <Shader Name="SpriteShader">
      <VertexShaderSource>
<![CDATA[//

void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

uniform float random;

void main()
{
  gl_FragColor = vec4(random, 1-random, 1, 1);
}]]>
      </FragmentShaderSource>
    </Shader>
  </Content>
</ZApplication>
You can get the uniform location using glGetUniformLocation ( use Shader.Handle as program argument ).

K
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

VilleK wrote:I guess the only way around that is to render something small using each shader during application initialisation.
All right, I'll add that to my arcade style warming up screen :wink:
Kjell wrote:But for your specific situation you might want to use a glUniform call instead
Thanks, that’s perfect. I managed to use it in my model-independent per-vertex position modifier. What I don’t understand is how glUniform1f sets a random value inside the shader. The documentation says “glUniform - Specify the value of a uniform variable for the current program object.” So does it set the seed for the current iteration of the shader, and any uniform variables are affected?
User avatar
Kjell
Posts: 1939
Joined: Sat Feb 23, 2008 11:15 pm

Re: Questions about ES2/GL3 shaders

Post by Kjell »

Hi Ats,
Ats wrote: Wed Jul 02, 2025 8:58 pmWhat I don’t understand is how glUniform1f sets a random value inside the shader.
The glUniform function(s) set the value of a specific uniform variable of the shader that is currently active. It's the "location" argument that determines which uniform variable you're writing to. In the example i posted my shader only has one ( user-defined ) uniform variable, so the location should be 0 ( just like the first element of an array ).

In other words; glUniform1f(0, Sprite.Personality) basically does "SpriteShader.random = Sprite.Personality".

Generally i'd recommended calling glGetUniformLocation(MyShader.Handle, "myUniform") once a shader is compiled, and then storing that location value for later use .. but sometimes a shortcut doesn't hurt :wink:

K
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

So I wrote a shader that jitter the vertices, so several copies of a single model looks different.
  • On PC, the models are jittered, and they all look different.
  • But on android, the models are jittered, but they all look the same...
In order to see that better, I applied a color based on the model personality:

PC:
personality_pc.png
personality_pc.png (118.03 KiB) Viewed 92 times
Android:
personality_android.png
personality_android.png (96.42 KiB) Viewed 92 times
I thought personality wasn't working on Android, so I wrote a simpler test without shaders:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="Debug personality" FileVersion="2" AndroidPackageName="com.txori.debug_personality">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[for (int i=0; i<10; i++) {
  for (int j=0; j<10; j++) {
    ModelCube.Position.X = i*2-5;
    ModelCube.Position.Y = j*2-5;
    @SpawnModel(Model:ModelCube);
  }
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Mesh Name="MeshCube">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
    <Model Name="ModelCube" Position="13 13 0">
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[CubeColor.Color.R = CurrentModel.Personality;
CubeColor.Color.B = 1.0 - CurrentModel.Personality;]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnRender>
        <RenderSetColor Name="CubeColor"/>
        <RenderMesh Mesh="MeshCube"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
And it's working perfectly!
So I suppose the problem is coming from that openGL instruction:
glUniform1f(0, CurrentModel.Personality);
Is it possible that the number 0 doesn't correspond to "random" in the shader on Android? Maybe the order of variables is different?

Edit:
glUniform1f(1, CurrentModel.Personality); is working on Android!
But I don't think this is going to be consistent between all the Android devices...
How should I use glGetUniformLocation instead?
Last edited by Ats on Fri Jul 11, 2025 8:46 am, edited 1 time in total.
User avatar
Kjell
Posts: 1939
Joined: Sat Feb 23, 2008 11:15 pm

Re: Questions about ES2/GL3 shaders

Post by Kjell »

Hi Ats,

Did you use ES2/GL3 for both the PC and Android version?

Anyway, here's an example using glGetUniformLocation that should move a sprite around like a clock.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

 int glGetUniformLocation(int program, string name){}
void glUniform1f(int location, float v0){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[// Enable the shader & get the uniform locations

@UseMaterial(Material: DemoMaterial);

DemoXLocation = glGetUniformLocation(DemoShader.Handle, "x");
DemoYLocation = glGetUniformLocation(DemoShader.Handle, "y");]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <UseMaterial Material="DemoMaterial"/>
    <ZExpression>
      <Expression>
<![CDATA[//

glUniform1f(DemoXLocation, sin(App.Time));
glUniform1f(DemoYLocation, cos(App.Time));]]>
      </Expression>
    </ZExpression>
    <RenderNet/>
  </OnRender>
  <Content>
    <Material Name="DemoMaterial" Shader="DemoShader"/>
    <Shader Name="DemoShader">
      <VertexShaderSource>
<![CDATA[//

uniform float x;
uniform float y;

void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + vec4(x, y, 0.0, 0.0));
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

void main()
{
  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}]]>
      </FragmentShaderSource>
    </Shader>
    <Variable Name="DemoXLocation" Type="1"/>
    <Variable Name="DemoYLocation" Type="1"/>
  </Content>
</ZApplication>
K
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

Thanks Kjell.
Kjell wrote:Did you use ES2/GL3 for both the PC and Android version?
My game is ES2/GL3 only, and it's one project for all the targets, so yes :wink:
Kjell wrote:here's an example using glGetUniformLocation
I was indeed using it wrong. But the result is the same with your example: it crashes hard on Android.

The mere fact of calling glGetUniformLocation is crashing the app on the phone and on Android Studio Emulator.

I tried to do that in a state, in OnUpdate, in compatible and ES2/GL3 mode (I adapted the shader to use uniform mat4 modelViewProjectionMatrix)...
I also tried to use a dummy material before using your material.
I also added this on opengl32 BeforeInitExp:

Code: Select all

if (ANDROID)
{
  if(App.GLBase==0) this.ModuleName="libGLESv1_CM.so";
  else this.ModuleName="libGLESv2.so";
}
I'll start digging the logcat when I'll get back home.
User avatar
Kjell
Posts: 1939
Joined: Sat Feb 23, 2008 11:15 pm

Re: Questions about ES2/GL3 shaders

Post by Kjell »

Hi Ats,

The example i posted uses "Compatible" mode, which doesn't support shaders on Android. And in case you'd switch the example to ES2/GL3, the shaders need to be re-written ( a bit ). So, here's a ES2/GL3 version instead ( untested .. i don't have any Android devices lying around at the moment ).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" GLBase="1" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//

if(ANDROID)
{
  ModuleName = "libGLESv2.so";
}]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//

 int glGetUniformLocation(int program, string name){}
void glUniform1f(int location, float v0){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

@UseMaterial(Material: DemoMaterial);

DemoLocations[0] = glGetUniformLocation(DemoShader.Handle, "x");
DemoLocations[1] = glGetUniformLocation(DemoShader.Handle, "y");]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <UseMaterial Material="DemoMaterial"/>
    <ZExpression>
      <Expression>
<![CDATA[//

glUniform1f(DemoLocations[0], sin(App.Time));
glUniform1f(DemoLocations[1], cos(App.Time));]]>
      </Expression>
    </ZExpression>
    <RenderMesh Mesh="DemoMesh"/>
  </OnRender>
  <Content>
    <Array Name="DemoLocations" Type="1" SizeDim1="2"/>
    <Material Name="DemoMaterial" Shader="DemoShader"/>
    <Mesh Name="DemoMesh">
      <Producers>
        <MeshBox Scale="0.5 0.5 1" Grid2DOnly="255"/>
      </Producers>
    </Mesh>
    <Shader Name="DemoShader">
      <VertexShaderSource>
<![CDATA[#version 100

precision mediump float;

uniform mat4 modelViewProjectionMatrix;
uniform float x;
uniform float y;

attribute vec4 position;

void main()
{
  gl_Position = modelViewProjectionMatrix * (position + vec4(x, y, 0.0f, 0.0f));
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[#version 100

precision mediump float;

void main()
{
  gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}]]>
      </FragmentShaderSource>
    </Shader>
  </Content>
</ZApplication>
K
User avatar
Ats
Posts: 842
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Questions about ES2/GL3 shaders

Post by Ats »

switch the example to ES2/GL3, the shaders need to be re-written
That is exactly what I did :lol:

I added text to your example to see what's going on, and another shader that isn't using values x and y.

On PC, the glGetUniformLocation values are 1 and 2.
Screenshot 2025-07-11 172308.png
Screenshot 2025-07-11 172308.png (1.83 KiB) Viewed 21 times
On Android, it's 0 and 0... And the square isn't showing up. I believe it's because 0 corresponds to modelViewProjectionMatrix ?
Screenshot 2025-07-11 172257.png
Screenshot 2025-07-11 172257.png (1.87 KiB) Viewed 21 times

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="Debug Personality" GLBase="1" FileVersion="2" AndroidPackageName="com.txori.debug_personality">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//

if (ANDROID)
{
  ModuleName="libGLESv2.so";
}]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//

 int glGetUniformLocation(int program, string name){}
void glUniform1f(int location, float v0){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

@UseMaterial(Material: DemoMaterial);

DemoLocations[0] = glGetUniformLocation(DemoShader.Handle, "x");
DemoLocations[1] = glGetUniformLocation(DemoShader.Handle, "y");]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <UseMaterial Material="BasicMaterial"/>
    <RenderMesh Mesh="DemoMesh"/>
    <UseMaterial Material="DemoMaterial"/>
    <ZExpression>
      <Expression>
<![CDATA[//

glUniform1f(DemoLocations[0], sin(App.Time));
glUniform1f(DemoLocations[1], cos(App.Time));]]>
      </Expression>
    </ZExpression>
    <RenderMesh Mesh="DemoMesh"/>
    <UseMaterial Material="FontArcadeMaterial"/>
    <RenderText Scale="0.3" TextExpression="intToStr(DemoLocations[0])+&quot; &quot;+intToStr(sin(App.Time)*100.0)+&quot; - &quot;+intToStr(DemoLocations[1])+&quot; &quot;+intToStr(cos(App.Time)*100.0);"/>
  </OnRender>
  <Content>
    <Array Name="DemoLocations" Type="1" SizeDim1="2"/>
    <Material Name="DemoMaterial" Shader="DemoShader"/>
    <Mesh Name="DemoMesh">
      <Producers>
        <MeshBox Scale="0.5 0.5 1" Grid2DOnly="255"/>
        <MeshExpression VertexColors="255">
          <Expression>
<![CDATA[C.R = V.X;
C.G = V.Y;
C.B = V.Z;]]>
          </Expression>
        </MeshExpression>
      </Producers>
    </Mesh>
    <Shader Name="DemoShader">
      <VertexShaderSource>
<![CDATA[#ifdef GL_ES
  precision mediump float;
#endif

uniform mat4 modelViewProjectionMatrix;
uniform float x;
uniform float y;

attribute vec4 position;

void main()
{
//  gl_Position = modelViewProjectionMatrix * position;
  gl_Position = modelViewProjectionMatrix * (position + vec4(x, y, 0.0f, 0.0f));
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[#ifdef GL_ES
  precision mediump float;
#endif

void main()
{
  gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}]]>
      </FragmentShaderSource>
    </Shader>
    <Group Comment="Arcade">
      <Children>
        <Bitmap Name="FontArcadeBitmap" Width="144" Height="44" Filter="1">
          <Producers>
            <BitmapFromFile Comment="Imported from Arcade.png" Transparency="2" HasAlphaLayer="1" DataWidth="144" DataHeight="44">
              <BitmapFile>
<![CDATA[789CED58419283300CEBFF3F9D1E3A7442B065390152527946871261CB8E08CB96525E451004411004E1BFB005CBABF9C558B7F8280FAA8DD6B379D8FC5E1F595D8CDE48F713C0F4D072AC7BEA6BDE6CD86BEC5E64F72D5AB3B80C6754C793E105E38D6270B273425C6F2DD2C86A4291C993ED07DDFF34A0798D70BC595991DD8B8C9EE2FCCEF887DDFB28C78A1EB27A69AF5DC9C96ABB4A4F7138ACE799FA2B7A08CDEB6A8E75CF2CFF449CE2FC1EED0D719E002F18DE19797A34A1FA33389967C58AD91E10849FC3167772106F869E3B34AF8A3B7BFFD719AF8C3BF754FE590FD59E6E51AFB7D7BCB0CE319303F258FADC7A40B3876C1E740D6967D72DFE68EF3D9CCC7E9CE51F56CFEE37A8E5698E7A617B8DEAB17DF5E845B58AB13ED23BDB27E264E61A9D093DB5CCDACDF9C46A8EFA60FAECD58C723375993D1CA951AF17E79E9663D5B4EAD0FD9DF5FE626A0FF827AA95D163F1505CED9FDE6783D19CE1A4FB6BBE4D993CBD1C66363D7DF4D6EAD573A67F466BA0405E457A7AFA3B9C0BAD9F188F05EFA796E3F640789A5E0FCEBB545FCEB7419BC7DDE70B7BDF38874406E712FF38F75AF77B61CDEFC009F2649EB1CC33C8D4441A337A500FBDB5D8BC67D4627B120441F8498077793ACF59B90441783CEAF85E37BE53DA88D677F988754F17D23BC24135466ACDDECF19DEA97F7FFD637D9F82EFD6DDEC886FE568DEEC3D560F6C2DA421CB67D657029C73F2FF1B6DEC38CEDF3BBB42468EA3985C1E97D372DBEBD59AD91FABC7585B09E19C09BEB58E78070ED8F7A89EC7F3223387919EBE8D11DA9F8CBA55F83CD67CF0DC59F30CF7CB39C7A0BF8DC8F89CF590959FA9F5B9B0BE7F5E5B9FEDF3EF9DF3ED223343E7ACB7EEF13C82CEA88C1F58BF441CF9C7DFAF57B1FD1379C20ACF33071ED2E4F93893C3E0A06073647A5F16D6FB089C1B708DC93DA3B7191A56F78FD75FE48FCC7ECCDCBFD958BD67E66C893867CC6FC539FFC333C37845103CC83F42F9C46C0D82200882200882200802C01B50635CC2]]>
              </BitmapFile>
            </BitmapFromFile>
          </Producers>
        </Bitmap>
        <Font Name="FontArcade" Bitmap="FontArcadeBitmap" FirstChar="32" CharPixelWidth="8" CharPixelHeight="10" BorderPixels="1"/>
        <Material Name="FontArcadeMaterial" Light="0" Blend="1" ZBuffer="0" Font="FontArcade" Shader="FontShader"/>
        <Shader Name="FontShader">
          <VertexShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

varying vec2 tc;

uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;

attribute vec4 position;  //vertex position
attribute vec2 texCoord;  //vertex texture coordinate

void main()
{
  tc = (textureMatrix * vec4(texCoord,0,1)).xy;
  gl_Position = modelViewProjectionMatrix * position;
}

//]]>
          </VertexShaderSource>
          <FragmentShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

varying vec2 tc; // texCoordVarying

uniform sampler2D tex1;

void main()
{
  gl_FragColor = texture2D(tex1, tc);
}

//]]>
          </FragmentShaderSource>
        </Shader>
      </Children>
    </Group>
    <Shader Name="BasicShader">
      <VertexShaderSource>
<![CDATA[//

#ifdef GL_ES
  #ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
  #else
    precision mediump float;
  #endif
#endif

varying vec4 v_color;

//The variables below are predefined in ZGE

uniform mat4 modelViewProjectionMatrix;

attribute vec4 position;    //vertex position
attribute vec4 color;       //vertex color

void main()
{
  v_color = color;
  gl_Position = modelViewProjectionMatrix * position;
}

//]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

varying vec4 v_color;

void main() {
  gl_FragColor = v_color;
}

//]]>
      </FragmentShaderSource>
    </Shader>
    <Material Name="BasicMaterial" Shader="BasicShader"/>
  </Content>
</ZApplication>
Edit:
Setting this manually doesn't make the rotating cube appear on Android either.

Code: Select all

DemoLocations[0] = 1;
DemoLocations[1] = 2;
But the main problem is glGetUniformLocation that returns 0 :|


I moved that to OnBeginRenderPass:

Code: Select all

@UseMaterial(Material: DemoMaterial);
DemoLocations[0] = glGetUniformLocation(DemoShader.Handle, "x");
DemoLocations[1] = glGetUniformLocation(DemoShader.Handle, "y");
Now the numbers are 1 and 1 on Android. Still no rotating square.

Edit 2:
All right, the square isn't showing up on Android because of the "f" in "0.0f". It works fine without them. I used text-compare to spot the difference :lol:
Here's the last test with a visible cube on Android, but glGetUniformLocation isn't working.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="Debug Personality" GLBase="1" FileVersion="2" AndroidPackageName="com.txori.debug_personality">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//

if (ANDROID)
{
  ModuleName="libGLESv2.so";
}]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//

 int glGetUniformLocation(int program, string name){}
void glUniform1f(int location, float v0){}]]>
      </Source>
    </ZExternalLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

@UseMaterial(Material: DemoMaterial);

// Returns 0 and 0 on Android
DemoLocations[0] = glGetUniformLocation(DemoShader.Handle, "x");
DemoLocations[1] = glGetUniformLocation(DemoShader.Handle, "y");

//DemoLocations[0] = 1;
//DemoLocations[1] = 2;]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <UseMaterial Material="DemoMaterial"/>
    <ZExpression>
      <Expression>
<![CDATA[//

glUniform1f(DemoLocations[0], sin(App.Time));
glUniform1f(DemoLocations[1], cos(App.Time));]]>
      </Expression>
    </ZExpression>
    <RenderMesh Mesh="DemoMesh"/>
    <UseMaterial Material="FontArcadeMaterial"/>
    <RenderText Scale="0.3" TextExpression="intToStr(DemoLocations[0])+&quot; &quot;+intToStr(sin(App.Time)*100.0)+&quot; - &quot;+intToStr(DemoLocations[1])+&quot; &quot;+intToStr(cos(App.Time)*100.0);"/>
  </OnRender>
  <Content>
    <Array Name="DemoLocations" Type="1" SizeDim1="2"/>
    <Material Name="DemoMaterial" Shader="DemoShader"/>
    <Mesh Name="DemoMesh">
      <Producers>
        <MeshBox Scale="0.5 0.5 1" Grid2DOnly="255"/>
        <MeshExpression VertexColors="255">
          <Expression>
<![CDATA[C.R = V.X;
C.G = V.Y;
C.B = V.Z;]]>
          </Expression>
        </MeshExpression>
      </Producers>
    </Mesh>
    <Shader Name="DemoShader">
      <VertexShaderSource>
<![CDATA[//

#ifdef GL_ES
  #ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
  #else
    precision mediump float;
  #endif
#endif

uniform mat4 modelViewProjectionMatrix;

uniform float x;
uniform float y;

attribute vec4 position;    //vertex position

void main()
{
  gl_Position = modelViewProjectionMatrix * (position + vec4(x, y, 0.0, 0.0));
}

//]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

void main() {
  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

//]]>
      </FragmentShaderSource>
    </Shader>
    <Group Comment="Arcade">
      <Children>
        <Bitmap Name="FontArcadeBitmap" Width="144" Height="44" Filter="1">
          <Producers>
            <BitmapFromFile Comment="Imported from Arcade.png" Transparency="2" HasAlphaLayer="1" DataWidth="144" DataHeight="44">
              <BitmapFile>
<![CDATA[789CED58419283300CEBFF3F9D1E3A7442B065390152527946871261CB8E08CB96525E451004411004E1BFB005CBABF9C558B7F8280FAA8DD6B379D8FC5E1F595D8CDE48F713C0F4D072AC7BEA6BDE6CD86BEC5E64F72D5AB3B80C6754C793E105E38D6270B273425C6F2DD2C86A4291C993ED07DDFF34A0798D70BC595991DD8B8C9EE2FCCEF887DDFB28C78A1EB27A69AF5DC9C96ABB4A4F7138ACE799FA2B7A08CDEB6A8E75CF2CFF449CE2FC1EED0D719E002F18DE19797A34A1FA33389967C58AD91E10849FC3167772106F869E3B34AF8A3B7BFFD719AF8C3BF754FE590FD59E6E51AFB7D7BCB0CE319303F258FADC7A40B3876C1E740D6967D72DFE68EF3D9CCC7E9CE51F56CFEE37A8E5698E7A617B8DEAB17DF5E845B58AB13ED23BDB27E264E61A9D093DB5CCDACDF9C46A8EFA60FAECD58C723375993D1CA951AF17E79E9663D5B4EAD0FD9DF5FE626A0FF827AA95D163F1505CED9FDE6783D19CE1A4FB6BBE4D993CBD1C66363D7DF4D6EAD573A67F466BA0405E457A7AFA3B9C0BAD9F188F05EFA796E3F640789A5E0FCEBB545FCEB7419BC7DDE70B7BDF38874406E712FF38F75AF77B61CDEFC009F2649EB1CC33C8D4441A337A500FBDB5D8BC67D4627B120441F8498077793ACF59B90441783CEAF85E37BE53DA88D677F988754F17D23BC24135466ACDDECF19DEA97F7FFD637D9F82EFD6DDEC886FE568DEEC3D560F6C2DA421CB67D657029C73F2FF1B6DEC38CEDF3BBB42468EA3985C1E97D372DBEBD59AD91FABC7585B09E19C09BEB58E78070ED8F7A89EC7F3223387919EBE8D11DA9F8CBA55F83CD67CF0DC59F30CF7CB39C7A0BF8DC8F89CF590959FA9F5B9B0BE7F5E5B9FEDF3EF9DF3ED223343E7ACB7EEF13C82CEA88C1F58BF441CF9C7DFAF57B1FD1379C20ACF33071ED2E4F93893C3E0A06073647A5F16D6FB089C1B708DC93DA3B7191A56F78FD75FE48FCC7ECCDCBFD958BD67E66C893867CC6FC539FFC333C37845103CC83F42F9C46C0D82200882200882200802C01B50635CC2]]>
              </BitmapFile>
            </BitmapFromFile>
          </Producers>
        </Bitmap>
        <Font Name="FontArcade" Bitmap="FontArcadeBitmap" FirstChar="32" CharPixelWidth="8" CharPixelHeight="10" BorderPixels="1"/>
        <Material Name="FontArcadeMaterial" Light="0" Blend="1" ZBuffer="0" Font="FontArcade" Shader="FontShader"/>
        <Shader Name="FontShader">
          <VertexShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

varying vec2 tc;

uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;

attribute vec4 position;  //vertex position
attribute vec2 texCoord;  //vertex texture coordinate

void main()
{
  tc = (textureMatrix * vec4(texCoord,0,1)).xy;
  gl_Position = modelViewProjectionMatrix * position;
}

//]]>
          </VertexShaderSource>
          <FragmentShaderSource>
<![CDATA[//

#ifdef GL_ES
  precision mediump float;
#endif

varying vec2 tc; // texCoordVarying

uniform sampler2D tex1;

void main()
{
  gl_FragColor = texture2D(tex1, tc);
}

//]]>
          </FragmentShaderSource>
        </Shader>
      </Children>
    </Group>
  </Content>
</ZApplication>
Post Reply