@MisterX: It would be a nice feature. If someone develops it, I would add it to cocos2d.
[devel] OpenGL ES 2.0 support
(171 posts) (45 voices)-
Posted 1 year ago #
-
Hi , i did a bumpmap shader with some exemple code found arround the web and the great tutorial ,it's working great so far , here is the fragment shader for the one who are interested :
#ifdef GL_ES precision lowp float; #endif varying vec4 v_fragmentColor; varying vec2 v_texCoord; uniform vec4 u_lightcolor; uniform float u_spec; uniform vec3 u_lightdir; uniform sampler2D u_texcolor; uniform sampler2D u_texnormal; void main() { vec4 material = texture2D (u_texcolor, v_texCoord) * v_fragmentColor; vec3 normal = texture2D (u_texnormal, v_texCoord).rgb * 2.0 - 1.0; vec3 halfdir = normalize (normalize(u_lightdir) ); float diff = max (0.0, dot (normal, u_lightdir)); float nh = max (0.0, dot (normal, halfdir)); float spec = pow (nh, u_spec); vec4 c = material * u_lightcolor * diff * spec ; gl_FragColor = vec4(c.r,c.g,c.b,material.a); }I d'like to know if that's possible to use texture from a CCSpriteBatchNode to use in shader , so far i tried out to init that way:
#import "Shad.h" @implementation Shad - (id)initWithSpriteFrameName:(NSString *)spriteFrameName { self = [super initWithSpriteFrameName:spriteFrameName]; if (self) { CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"SphereNormal.png"]; _NormalTexture = frame.texture; //then other stuff...The texture sprite is loaded but the Normal texture doesn't seams to be loaded , maybe i don't do it the right way?
Posted 1 year ago # -
What does frame.texture return ?
Posted 1 year ago # -
yep actually it seams that the shader is not use at all when using initWithSpriteFrameName
I didn't assign a texture to the shader and it keep showing the texture i'm using for init.here is the full code of the working version:
Shad.h
#import "cocos2d.h" @interface Shad : CCSprite { CCTexture2D * _NormalTexture; GLuint _textureLocation; GLuint _NormTextLocation; GLuint _lightcolor; GLuint _matcolor; GLuint _spec; GLuint _fragmentColor; GLuint _lightdir; GLuint _viewdir; float inc; } @endShad.m
#import "Shad.h" @implementation Shad - (id)initWithFile:(NSString *)file { self = [super initWithFile:file]; if (self) { _NormalTexture = [[[CCTextureCache sharedTextureCache] addImage:@"SphereNormal2.png"] retain]; self.shaderProgram = [[[GLProgram alloc] initWithVertexShaderFilename:@"Shaders/PositionTextureColor.vert" fragmentShaderFilename:@"BumpMapping.frag"] autorelease]; CHECK_GL_ERROR_DEBUG(); [shaderProgram_ addAttribute:kCCAttributeNamePosition index:kCCAttribPosition]; [shaderProgram_ addAttribute:kCCAttributeNameColor index:kCCAttribColor]; [shaderProgram_ addAttribute:kCCAttributeNameTexCoord index:kCCAttribTexCoords]; CHECK_GL_ERROR_DEBUG(); [shaderProgram_ link]; CHECK_GL_ERROR_DEBUG(); [shaderProgram_ updateUniforms]; CHECK_GL_ERROR_DEBUG(); _textureLocation = glGetUniformLocation( shaderProgram_->program_, "u_texcolor"); _NormTextLocation = glGetUniformLocation( shaderProgram_->program_, "u_texnormal"); _lightcolor = glGetUniformLocation( shaderProgram_->program_, "u_lightcolor"); _matcolor = glGetUniformLocation( shaderProgram_->program_, "u_matcolor"); _spec = glGetUniformLocation( shaderProgram_->program_, "u_spec"); _lightdir = glGetUniformLocation( shaderProgram_->program_, "u_lightdir"); _viewdir = glGetUniformLocation( shaderProgram_->program_, "u_viewdir"); CHECK_GL_ERROR_DEBUG(); } return self; } -(void) draw { // 1 ; inc+=0.02; ccGLBlendFunc( blendFunc_.src, blendFunc_.dst ); ccGLUseProgram( shaderProgram_->program_ ); ccGLUniformProjectionMatrix( shaderProgram_ ); ccGLUniformModelViewMatrix( shaderProgram_ ); // 2 glActiveTexture(GL_TEXTURE0); glBindTexture( GL_TEXTURE_2D, [texture_ name] ); glUniform1i(_textureLocation, 0); glActiveTexture(GL_TEXTURE1); glBindTexture( GL_TEXTURE_2D, [_NormalTexture name] ); glUniform1i(_NormTextLocation, 1); glUniform4f( _matcolor, 1.0, 1.0, 1.0, 1.0); glUniform4f( _lightcolor, 1.0, 0.5, 1.0, 1.0); glUniform3f( _lightdir,sinf(inc),cosf(inc),1.0 ); glUniform3f( _viewdir,0.0,0.0,1.0 ); glUniform1f(_spec, 0.3); // #define kQuadSize sizeof(quad_.bl) long offset = (long)&quad_; // vertex NSInteger diff = offsetof( ccV3F_C4B_T2F, vertices); glVertexAttribPointer(kCCAttribPosition, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)); // texCoods diff = offsetof( ccV3F_C4B_T2F, texCoords); glVertexAttribPointer(kCCAttribTexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); // color diff = offsetof( ccV3F_C4B_T2F, colors); glVertexAttribPointer(kCCAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)); // 4 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glActiveTexture(GL_TEXTURE0); } @endand BumpMapping.frag
#ifdef GL_ES precision lowp float; #endif varying vec4 v_fragmentColor; varying vec2 v_texCoord; uniform vec4 u_lightcolor; uniform vec4 u_matcolor; uniform float u_spec; uniform vec3 u_lightdir; uniform vec3 u_viewdir; uniform sampler2D u_texcolor; uniform sampler2D u_texnormal; void main() { vec4 material = texture2D (u_texcolor, v_texCoord)*u_matcolor; vec3 normal = texture2D (u_texnormal, v_texCoord).rgb * 2.0 - 1.0; vec3 halfdir = normalize (normalize(u_lightdir) + normalize(u_viewdir)); float diff = max (0.0, dot (normal, u_lightdir)); float nh = max (0.0, dot (normal, halfdir)); float spec = pow (nh, u_spec); vec4 c = material * u_lightcolor * diff * spec ; gl_FragColor = vec4(c.r,c.g,c.b,material.a); }but using initWithSpriteFrameName like i wrote it before doesn't seams to load the shader
I had to enable VBO in ccConfig in order to compile on device , i don't know if that's related.Posted 1 year ago # -
Ok after looking into the cocos code i found out that since i'm using a batchnode i need to load my shader in the batchnode ,so i'll need instead to subclasse CCspriteBatchNode.
Thx anyway all this looks very promising and already works great.Posted 1 year ago # -
Ok i start to understand , i need to load my shader in the Batchnode , no need to change things here , but then i can't call draw for batchnode in my CCsprite subclass so were can i update the uniforms param and load additionnal texture? is it possible to have different uniform value for each sprite with batchnode?
Posted 1 year ago # -
Thx riq , it totally make sense , the sprite are acting like a single mesh so you can't control them with uniform , i'll try the vertex attributre thingy i think best is to set extra texturecoords and get them as param in the shader , i'll try also render as texture , with a 32*32 texture i can store 1024 4d vector but i need 16bit at least , then i need to set correct texturecoord for each sprite ...
i'll try as well with vertexattribute to see what the best, i need to have a look to the batchnode code... to see how i can do that , but i think it's a lot of work :)Posted 1 year ago # -
status update: templates are working Ok in the gles20 branch.
cocos2d v2.0-alpha coming soon.
Posted 1 year ago # -
Excellent!
Posted 1 year ago # -
Status:
- Chipmunk 6.0.1 integrated into gles20
- Box2d 2.2.0 integrated into gles20
- All templates are working OK
- NEON matrix multiplication integrated into Kazmath (should be faster)
- Uniforms: send only 1 matrix (Model View Projection) instead of 2 (ModelView and Projection) (should be faster)
- Performance tests: updated (gles20 a bit slower than v1.0 in some tests... don't know why yet)
- Includes latests changes from develop branch
- Improved GL state API
- Integrated @manucorporat Streak MotionWhat's missing for v2.0-alpha
- Try .fs and .vs extension for shaders
- Write migration guidePosted 1 year ago # -
I started to use 2.0. I noticed that it already uses VAO's glGenVertexArraysOES which is only available starting from iOS 4.0. And yes, when I tested it on iPad 3.2, it crashed because of it. Will this means cocos2d 2.0 will only support devices with iOS 4.0 upward ?
Posted 1 year ago # -
@riq: I saw that grid effects are still using the old method. IMO, with shaders support, maybe we should try to do those effects using them?
Posted 1 year ago # -
@riq:
I have tried implementing a simple ripple effect by subclassing CCSprite, and set its own ripple frag shader. It works ok. But I would like to know the best design pattern to make it more flexible, for example the effect usage will be as simple as the way we use it in the old way (action style). What do you think ?Posted 1 year ago # -
Is it possible to install both cocos2d 1.0 and 2.0 templates?
Thanks!Posted 1 year ago # -
Cocos2d v2 is not compatible with iPhone 3G even if they are in iOS4.x, is it?
I think it's not a big problem as according to this post, it should represents less than 5% of the phones now. http://www.iphonedevsdk.com/forum/business-legal-app-store/79677-iphone-4-vs-3g-market-share-need-input-please.htmlPosted 1 year ago # -
@jptsetung: iPhone 3G is not supported
http://www.cocos2d-iphone.org/wiki/doku.php/release_notes:2_0_0#compatibilityPosted 1 year ago # -
One of the most exiting things I find about the new shader integration is the possibility to do fullscreen effects. So I've been playing with the new shader pipeline to see how it works. I discovered that the shader is basically applied to a Texture2d instance so I'm immediately wondering how can I extract the Texture2d from the screen buffer before the layer is actually rendered?
I found this thread talking about screen captures, but I really don't know if this would be the way to go shader wise.Posted 1 year ago #
Reply
You must log in to post.