I have made spritesheet in Zwoptex , I know TexturePacker is better than this but i have just started with cocos2d iphone, so haven't purchase that.
I have made CCTexture2D using following code.
texture = [[CCTextureCache sharedTextureCache] addImage:#"1.png"];
self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture];
CC_NODE_DRAW_SETUP();
And i use this CCtexture2D object to draw texture around soft body.Using Following code.
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords);
ccGLBindTexture2D([texture name]);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_TRUE, 0, triangleFanPos);
glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_SEGMENT+2);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Color);
Now I want to animate texture of soft body. I know how to animate sprite using spritesheet. But now i am confuse how to make CCTexture2D using spritesheet and how can i animate texture using different images like we do in sprite animation?
Can anyone give me any direction in solving this issue?
First of all i want to say sorry for this question. I want to share my code here. I want to animate the texture which i am giving to the soft body.
I was confused , how can i give new image to the cctexture2d object and rotate that at the angle of previous textures angle. But I came to know that cocos2d iphone automatically handles that.
I animate my texture2d object by just giving it the image sequences with delay. Code for that is shown below.
Called Function
-(void)changeTexture1:(NSNumber*)i{
texture = [[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:#"blink%#.png",i]];
}
Calling function:
-(void)makeAnimation{
for (int i = 2; i<7; i++) {
[self performSelector:#selector(changeTexture1:) withObject:[NSNumber numberWithInt:i] afterDelay:0.1*i];
}
int j = 1;
for(int i=6;i>0;i--){
[self performSelector:#selector(changeTexture1:) withObject:[NSNumber numberWithInt:i] afterDelay:0.1*6 + 0.1*j++];
}
}
Related
I have a weird problem that I have not found a solution for despite lots and lots of googling and reading.
I have a scene that uses a dynamically generated background. The code for the background is based on this tutorial and other code I have found related to that such as Haqus Tiny-Wings github.
Anyway, my code has simplified the hill generation and it is all contained in one CCNode class called StripedTerrain. It all works fine (now!) but when going to another scene that uses the same layout with the same background sprite, it doesnt render completely. See the screenshot. Image A is the first run with my code as is. Image B is after a replaceScene call to a new scene of the same scene class. Then, I made this small change to my draw code just before popping the matrix:
ccDrawColor4B(255, 255, 255, 255);
ccDrawLine(ccp(0.0,0.0),ccp(0.0,0.0));
and then it works fine (images C and D)
This is the strangest thing and I cannot figure out what's going wrong.
I'll post the draw call code, but spare you the rest of the details :
/**
* Previus to the draw method we have already done the following:
* Randomly selected, or have been given two colors to paint the stripes onto our texture
* Generated a texture to overlay on to our hill vertice geometry
* Generated the top of the hill peaks and valleys
* Generated the hill verticies that will fill in the surface of the hills
* with the texture applied
*/
- (void) draw {
CC_NODE_DRAW_SETUP();
// this statement fixed the screwed up jagged line rendering
// since we are only using position and texcoord vertexes, we have to use this shader
kmGLPushMatrix();
CHECK_GL_ERROR_DEBUG();
ccGLBlendFunc( CC_BLEND_SRC, CC_BLEND_DST ); //TB 25-08-12: Allows change of blend function
ccGLBindTexture2D(self.stripes.texture.name);
//TB 25-08-12: Assign the vertices array to the 'position' attribute
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, _hillVertices);
//TB 25-08-12: Assign the texCoords array to the 'TexCoords' attribute
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, _hillTexCoords);
glEnableVertexAttribArray(kCCVertexAttrib_Position);
glEnableVertexAttribArray(kCCVertexAttrib_TexCoords);
//TB 25-08-12: Draw the above arrays
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nHillVertices);
CHECK_GL_ERROR_DEBUG();
//Debug Drawing (remove comments to enable)
if(0) {
for(int i = MAX(_fromKeyPointI, 1); i <= _toKeyPointI; ++i) {
ccDrawColor4B(255, 0, 0, 255);
ccDrawLine(_hillKeyPoints[i-1], _hillKeyPoints[i]);
}
for(int i =0;i<_nHillVertices;i+=3) {
ccDrawColor4B(255, 0, 0, 255);
ccDrawLine(_hillVertices[i+1], _hillVertices[i+2]);
}
}
// have to do this to force it to work the next scene load
ccDrawColor4B(255, 255, 255, 255);
ccDrawLine(ccp(0.0,0.0),ccp(0.0,0.0));
kmGLPopMatrix();
CC_INCREMENT_GL_DRAWS(1);
}
Any obvious mistakes above?
I've set the shader in another method.
Check if the previous scene and its children run their dealloc method. If not, and one or more are leaking the weirdest things can happen.
The same goes for overriding cleanup without calling super cleanup.
So I have been trying to understand the concept of 3D picking but as I can't find any video guides nor any concrete guides that actually speak English, it is proving to be very difficult. If anyone is well experienced with 3D picking in LWJGL, could you give me an example with line by line explanation of what everything means. I should mention that all I am trying to do it shoot the ray out of the center of the screen (not where the mouse is) and have it detect just a normal cube (rendered in 6 QUADS).
Though I am not an expert with 3D picking, I have done it before, so I will try to explain.
You mentioned that you want to shoot a ray, rather than go by mouse position; as long as this ray is parallel to the screen, this method will still work, just the same as it will for a random screen coordinate. If not, and you actually wish to shoot a ray out, angled in some direction, things get a little more complicated, but I will not go in to it (yet).
Now how about some code?
Object* picking3D(int screenX, int screenY){
//Disable any lighting or textures
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE);
//Render Scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
orientateCamera();
for(int i = 0; i < objectListSize; i++){
GLubyte blue = i%256;
GLubyte green = min((int)((float)i/256), 255);
GLubyte red = min((int)((float)i/256/256), 255);
glColor3ub(red, green, blue);
orientateObject(i);
renderObject(i);
}
//Get the pixel
GLubyte pixelColors[3];
glReadPixels(screenX, screenY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixelColors);
//Calculate index
int index = pixelsColors[0]*256*256 + pixelsColors[1]*256 + pixelColors[2];
//Return the object
return getObject(index);
}
Code Notes:
screenX is the x location of the pixel, and screenY is the y location of the pixel (in screen coordinates)
orientateCamera() simply calls any glTranslate, glRotate, glMultMatrix, etc. needed to position (and rotate) the camera in your scene
orientateObject(i) does the same as orientateCamera, except for object 'i' in your scene
when I 'calculate the index', I am really just undoing the math I performed during the rendering to get the index back
The idea behind this method is that each object will be rendered exactly how the user sees it, except that all of a model is a solid colour. Then, you check the colour of the pixel for the screen coordinate requested, and which ever model the colour is indexed to: that's your object!
I do recommend, however, adding a check for the background color (or your glClearColor), just in case you don't actually hit any objects.
Please ask for further explanation if necessary.
I have a piece of code in my iOS project that swaps the texture of a CCSprite via setTexture, like so:
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"Circle.png"]];
However, the dimensions of the CCSprite's texture before and after the swap are different, and as a result, the Circle.png texture is getting cropped; stuck at the size of original texture (as the circle is larger).
How can I adjust the size after swapping the Texture?
(Related, but not helpful in solving this)
Try this:
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:#"Circle.png"];
if (texture)
{
// Get the size of the new texture:
CGSize size = [texture contentSize];
[sprite setTexture:texture];
// use the size of the new texture:
[sprite setTextureRect:CGRectMake(0.0f, 0.0f, size.width,size.height)];
}
You can just recreate sprite with spriteWithFile constructor. the dimensions will be set automatically
I have a CCSprite subclass. In the draw method, I am drawing some cocos2d primitives like lines and such. How can I create a CCTexture2D of the sprite? I can't use sprite.texture because that doesn't include the primitives I am drawing.
You can add sprite to object of CCRenderTexture2D and after this can draw sprite to texture.
look at the example
CCSprite *spr = nil;//your sprite
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:spr.contentSize.width height:spr.contentSize.height];
spr.anchorPoint = ccp(0, 0);
spr.position = ccp(0, 0);
[renderTexture addChild:spr];
[renderTexture begin];
[spr draw]; // or [spr visit];
[renderTexture end];
CCTexture2D *result = renderTexture.sprite.texture;
Now you will have texture, that contains sprite and primitives, that it draws in draw method.
Hope, it will help you:)
Making a 2D OpenGL game. When rendering a frame I need to first draw some computed quads geometry and then draw some textured sprites. When the body of my render method only draws the sprites, everything works fine. However, when I try to draw my geometric quads prior to the sprites the texture of the sprite changes to be the color of the last GL.Color3 used previously. How do I tell OpenGL (well, OpenTK) "Ok, we are done drawing geometry and its time to move on to sprites?"
Here is what the render code looks like:
// Let's do some geometry
GL.Begin(BeginMode.Quads);
GL.Color3(_dashboardBGColor); // commenting this out makes my sprites look right
int shakeBuffer = 100;
GL.Vertex2(0 - shakeBuffer, _view.DashboardHeightPixels);
GL.Vertex2(_view.WidthPixelsCount + shakeBuffer, _view.DashboardHeightPixels);
GL.Vertex2(_view.WidthPixelsCount + shakeBuffer, 0 - shakeBuffer);
GL.Vertex2(0 - shakeBuffer, 0 - shakeBuffer);
GL.End();
// lets do some sprites
GL.Begin(BeginMode.Quads);
GL.BindTexture(TextureTarget.Texture2D, _rockTextureId);
float baseX = 200;
float baseY = 200;
GL.TexCoord2(0, 0); GL.Vertex2(baseX, baseY);
GL.TexCoord2(1, 0); GL.Vertex2(baseX + _rockTextureWidth, baseY);
GL.TexCoord2(1, 1); GL.Vertex2(baseX + _rockTextureWidth, baseY - _rockTextureHeight);
GL.TexCoord2(0, 1); GL.Vertex2(baseX, baseY - _rockTextureHeight);
GL.End();
GL.Flush();
SwapBuffers();
The default texture environment mode is GL_MODULATE, which does that, it multiplies the texture color with the vertex color.
A easy solution is to set the vertex color before drawing a textured primitive to 1,1,1,1 with:
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
Another solution is to change the texture environment mode to GL_REPLACE, which makes the texture color replace the vertex color and doesn't have the issue:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);