Login Action Required

The NinevehGL Forum uses a new concept of "socialized forum" or as we like to say "Tweet Forum".


Here is the deal:
  1. No new registration is required. Just sign in with your Twitter account and authorize the NinevehGL Forum.
  2. Once you’re in, you'll be able to "Follow a Thread", that means every time that thread receive a new post or update you'll receive a mention on your twitter.
  3. Besides, you can enable "Auto Share", then every new post and/or thread you make will be tweeted on your timeline. (By default, auto-share is enabled only for your threads)

Forum Rules:

1. We understand human comunication can become "hot" sometimes. So some insults and bad words ARE allowed. Just don't push too much being an asshole all the time.

2. SPAMMERS are not allowed. There are penalties for this kind of user and they can be banned forever.

3. You can report other users, if you judge necessary. An user reported many times by many people can also be banned forever. However you can also receive penalties for report deliberately for no apparent reason.

If there is a similar thread title, make sure the other one doesn't already have the answer you're looking for.

This forum uses the BBCode (Bulletin Board Code), here are some instructions:

Bold: [b]text[/b]
Italic: [i]text[/i]
Underline: [u]text[/u]

Code: [code]text[/code]
Quote:
[quote=@username]text[/quote]
[quote]text[/quote]

List:
[ul] [*]item [/ul]
[ol] [*]item [/ol]

URL:
[url]http://url.com[/url]
[url=http://url.com]text[/url]

Image:
[img]http://imageurl.com[/img]

Embed (videos, code, 3D):
[embed]http://url.com[/embed]

Welcome to the NinevehGL's world!
NinevehGL is a 3D engine forged with pure Obj-C.
Welcome to the
Forum!
Hello, Guest.

Your current vote:

You can change your vote many times. But it's still one single vote.

Binding texture coord attribute t o shader
Vote this thread:


@PetrBodnar

Posts: 17

NGLNewbie

Mon, Aug 27 2012


I use following shaders in my app (for creating depth of field effect):
Vertex shader

[embed]
precision mediump float;

attribute vec2 a_nglTexcoord;
varying vec2 v_nglTexcoord;

varying float z;


void main(void)
{
vec4 pos = u_nglMVPMatrix * gl_Vertex;

z = pos.z;
v_nglTexcoord = a_nglTexcoord;
gl_Position = pos;
}
[/embed]

fragment shader:

[embed]
precision mediump float;

uniform sampler2D tex;
uniform float focalDistance, focalRange;

varying float z;
varying vec2 v_nglTexcoord;

void main (void)
{
float blur = clamp ( abs ( focalDistance z ) / focalRange, 0.0, 1.0 );
gl_FragColor = vec4 ( texture2D ( tex, v_nglTexcoord ).rgb, blur );
}
[/embed]

I create shader and attach it to my mesh at meshLoadingDidFinish:

[embed]
- (void) meshLoadingDidFinish:(NGLParsing)parsing {
NSLog(@"Mesh meshLoadingDidFinish");

float focalDistance = 1.0;
float focalRange = 4.0;
NGLShaders *blurShader = [[[NGLShaders alloc] initWithFilesVertex:@"dof1-p1.vsh" andFragment:@"dof1-p1.fsh"] autorelease];
[blurShader bindUniform:@"focalDistance" count:1 dataType:NGL_FLOAT data:&focalDistance];
[blurShader bindUniform:@"focalRange" count:1 dataType:NGL_FLOAT data:&focalRange];
[blurShader bindAttribute:@"a_nglTexcoord" stride:self.stride dataType:NGL_VEC2 data:(void *)(self.structures) 6]; // How to do that correctly ?
self.texture = [[[NGLTexture alloc] init2DWithFile:@"very_old_rusty_painted_metal_texture1 (1).jpg"] autorelease] ;

[blurShader bindTexture:@"tex" texture: self.texture ];
self.shaders = blurShader;

[self compileCoreMesh];
}
[/embed]

There is no errors in console and meshCompilingDidFinish is called,
But there is no meshes on screen.
I think that I'm binding a_nglTexcoord attribute incorrectly.
How to do this correctly?



0% like this - 0/0

@dineybomfim

Posts: 571

NGLAdmin

Mon, Aug 27 2012


In response to: @PetrBodnar

Hello buddy

Here is the deal:

- All the NinevehGL variables (described here) don't need to be declared in your custom shader. So using "attribute vec2 a_nglTexcoord;
varying vec2 v_nglTexcoord;"
Will not work at all.

Instead, the NinevehGL will create the shaders according to the mesh requirements. So if your mesh doesn't have neither one texture, the shaders will not have such variables.

- Custom shaders, materials or surfaces should be set right after the loading command. This will instruct NinevehGL to use your custom settings. You can do it in "meshLoadingDidFinish:" as well, however you have to recompile the core mesh, which is not good.

- When you're binding the variables to your custom shaders you're using dereferences, so you must make sure your original variables is not a "weak" one. I mean, they must be class variables. Just as you did with your custom texture.


That's all.
0% like this - 0/0

@PetrBodnar

Posts: 17

NGLNewbie

Tue, Aug 28 2012


In response to: @dineybomfim

Please explain one more thing to me.
My model is loading with texture ("very_old_rusty_painted_metal_texture1 (1).jpg").
Which NinevehGL variable should I use to access that texture in fragment shader?
Or should I create that texture manually and bind it to my shader (as in my first post):

self.texture = [[[NGLTexture alloc] init2DWithFile:@"very_old_rusty_painted_metal_texture1 (1).jpg"] autorelease] ;
[blurShader bindTexture:@"tex" texture: self.texture ];

In fsh:

uniform sampler2D tex;
...

?
0% like this - 0/0

@dineybomfim

Posts: 571

NGLAdmin

Tue, Aug 28 2012


In response to: @PetrBodnar

So if your mesh already has at least one texture, all can use the NinevehGL variables in shaders. You don't need to declare them nor bind them, because they are already in there.

Inside your VSH, just use a_nglTexcoord
Inside your FSH, just use v_nglTexcoord

However I'm sure you know that using a_nglTexcoord in VSH is not a good thing, because what we need for any texture element is the interpolated value in FSH.

Just it.
0% like this - 0/0

@PetrBodnar

Posts: 17

NGLNewbie

Tue, Aug 28 2012


In response to: @dineybomfim

I understood that if my mesh has texture, I can access variables in my shaders.
But I not understood, from where should I get sample2d variable in fragment shader.
My fsh looks now so:


precision mediump float;

uniform float focalDistance, focalRange;

varying float z;

void main (void)
{
float blur = clamp ( abs ( focalDistance z ) / focalRange, 0.0, 1.0 );
gl_FragColor = vec4 ( texture2D ( TEXTURE_VARIABLE, v_nglTexcoord ).rgb, blur );
}


How I must obtain TEXTURE_VARIABLE (representing my texture sampler2D)? (in case when my mesh has a texture).
Is there any NinevehGL variable or I must bind texture somehow to my shader?
I'm sorry for my English, I will try to improve it:)
0% like this - 0/0

@dineybomfim

Posts: 571

NGLAdmin

Tue, Aug 28 2012


In response to: @PetrBodnar

Well buddy,

You did it before!


self.texture = [[[NGLTexture alloc] init2DWithFile:@"very_old_rusty_painted_metal_texture1 (1).jpg"] autorelease] ;
[blurShader bindTexture:@"tex" texture: self.texture ];


precision mediump float;
uniform float focalDistance, focalRange;
uniform float focalDistance, focalRange;
varying float z;
uniform sampler2D tex;

void main (void)
{
float blur = clamp ( abs ( focalDistance * z ) / focalRange, 0.0, 1.0 );
gl_FragColor = vec4 ( texture2D ( tex, v_nglTexcoord ).rgb, blur );
}
0% like this - 0/0

@PetrBodnar

Posts: 17

NGLNewbie

Wed, Aug 29 2012


In response to: @dineybomfim

Thanks a lot, I understood.
0% like this - 0/0
Technology

NinevehGL is a 3D engine built right on top of OpenGL ES and it uses all the programmable pipeline power, making it easy for you to create great application with shaders.

Share on


Follow NinevehGL
Fastest way to contact us:

Copyright © 2011 db-in. All rights reserved.