cocos2d 2.1 and HKTMXLayer opacity problems - cocos2d-iphone

I'm building a game with coco2d 2.1 and I currently started using the HKTMXTiledMap extension.
Overall, the extension is great, anyone who is making a game with tiled maps (especially big ones) should check it out. But I need to be able to change the opacity of the layers during the course of my levels.
I have tried changing the shader and drawing methods, but there must be something I'm missing.
I changed line 97 of HKTMXLayer.m to: self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor];, but then the layer is shown all black.
Anyone got any tips for me? Thanks!

not certain of your overall context, but the draw method of the same class (line 465) is only seting the 'position' attribute : which is normal considering the class derives from 'CCNode'.
The shader you are setting (normally for a sprite) needs a vertices attribute, as well as a colors attrib. See the code in CCSprite (draw) for an example. Maybe there is another way to meet your need (the 'why' you are changing the shader for a Node derivative class to a shader normally used for CCSprite/textures).

The fix was posted here.
Along with changing the shader to kCCShader_PositionTextureColor, the color attribute needed to be passed when drawing the layer.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glVertexAttrib4f(kCCVertexAttrib_Color, color_.r/255., color_.g/255., color_.b/255., opacity_/255.);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, NULL);

Related

Drawing the grid over the texture

Before diving into details, I have added opengl tag because JoGL is a Java OpenGL binding and the questions seem to be accessible for experts of both to answer.
Basically what I am trying to do is to render the grid over the texture in JoGL using GLSL. So far, my idea was to render first the texture and draw the grid on top. So what I am doing is:
gl2.glBindTexture(GL2.GL_TEXTURE_2D, textureId);
// skipped the code where I setup the model-view matrix and where I do fill the buffers
gl2.glVertexAttribPointer(positionAttrId, 3, GL2.GL_FLOAT, false, 0, vertexBuffer.rewind());
gl2.glVertexAttribPointer(textureAttrId, 2, GL2.GL_FLOAT, false, 0, textureBuffer.rewind());
gl2.glDrawElements(GL2.GL_TRIANGLES, indices.length, GL2.GL_UNSIGNED_INT, indexBuffer.rewind());
And after I draw the grid, using:
gl2.glBindTexture(GL2.GL_TEXTURE_2D, 0);
gl2.glDrawElements(GL2.GL_LINE_STRIP, indices, GL2.GL_UNSIGNED_INT, indexBuffer.rewind());
Without enabling the depth test, the result look pretty awesome.
But when I start updating the coordinates of the vertices (namely updating one of its axes which corresponds to height), the rendering is done in a wrong way (some things which should be in front appear behind, which makes sense without the depth test enabled). So I have enabled the depth test:
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthMask(true);
An the result of the rendering is the following:
You can clearly see that the lines of the grid are blured, some of the are displayed thinner then others, etc. What I have tried to do to fix the problem is some line smoothing:
gl2.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST);
gl2.glEnable(GL2.GL_LINE_SMOOTH);
The result is better, but I am not still satisfied.
QUESTION: So basically the question is how to improve further the solution, so I can see solid lines and those are displayed nicely when I start updating the vertex coordinates.
If it is required I can provide the code of Shaders (which is really simple, Vertex Shader only calculates the position based on projection, model view matrix and the vertex coords and Fragment Shader calculates the color from texture sampler).

White (maybe uncolored) OBJ in GLSL/C++

Hi I'm working to create a space environment with a ship inside.But after the creation of the skybox (no errors) I put my ship inside but it hasnt colour. It's something like white-black
I did(modeled) the ship with OPENSCAD and after with MESHLAB I exported it into .OBJ format. I load it inside the source code but he hasn't the texture/colour . That my ship in Meshlab :
I need to know only I have to add something about color in the code or it's error in input. What this?
If you need I post the code, but if is somthing normal this error explain me, anyway I'm a little newbie in opengl, so be patient, thank you
EDIT :
Look my .obj file in windows:
And the same project in ubuntu :
What's this difference?
Anyway the openscad code :
module navicella(){
$fn=100;
rotate([0,180,270]){
union(){
rotate([270,180,0]){
rotate([90,0,0])
cylinder(50,7,10,center=true);
intersection(){
translate([0,-25,0])
sphere(10);
translate([0,-25,0])
cube(19,center=true);
}
difference(){
translate([0,35,0])
cube([10,15,15],center=true);
translate([0,40,0])
sphere(13);
}
translate([5,-10,0])rotate([90,0,70])
cube([35,1,15],center=true);
translate([-2,0,0])rotate([90,0,95])
cube([50,1,10],center=true);
translate([0,3,6])rotate([0,-15,90])
cube([40,1,20],center=true);
translate([0,3,-6])rotate([0,15,90])
cube([40,1,20],center=true);
translate([0,-35,0])rotate([90,0,0])
cylinder(10,5,0,center=true);
translate([0,20,0])rotate([0,90,0])
cube([45,1,2],center=true);
translate([0,25,0]) rotate([90,0,0])
cylinder(5,4,7,center=true);
}
}
}
}
navicella();
Looks like you have forgot to disable all used textures that are not used by your model. That is very common mistake (I do it all the time still today).
What is probably happening?
You rendered skybox or any object with textures
So for example GL_CUBE_MAP and or GL_TEXTURE_2D are enabled. Now when you start rendering your mesh that does not contain texture the textures are still enabled. So for every fragment/pixel GL will always sample the last set texture coordinate from last binded texture in all of the enabled texture targets and combine color according to GL settings.
So if you model does not contain texture coordinates GL will use the last set one. That is usually in corner area where black border is ... or you are just in some dark region of texture. Also if you unbind textures that only means you bind default texture 0 which is usually black.
To test/remedy this just callglDisable(GL_....); for all previously used texture targets. If it helps you know where the problem is.
Also if your object contains texture coordinates and texture is not loaded properly (like wrong file name/path) then the result is usually black.
Missing or negative normals while rendering with lighting enabled
Does not looks like it but it could be also the reason. If your model has wrong or no normals then the lighting computation result in wrong lighting. If the object is always dark even if you rotate it the the normals are negated (facing the other way) and you should change the front face for lighting/material or negate the normals.
If the color intensity is changing with rotation then your object has probably no normal and again the last set normal is used (from previous rendering). In such case either compute normals for the object (via cross product) or disable lighting for that object.

OpenGL3 two sets of shaders, texture showing black

I've recently succeeded at making a small test app with a GL_TEXTURE_RECTANGLE. Now I'm trying to integrate it into my larger project, but when I call glBindTexture(GL_TEXTURE_RECTANGLE, _tex_id[0]) inside the render function, it's causing the GL_INVALID_OPERATION​ error. The texture image sometimes shows for a fraction of a second, then turns black and stays black.
I am trying to do this by using two sets of vertex and fragment shaders, one set for the 3D scene, and one set for the 2D overlay, but I've never tried this before so I don't know if that's what's causing the error, or if I should be going about this a different way. The shaders are all compiling and linking fine.
Any insight would be much appreciated, and if it would help to see some code, let me know and I'll post some of it (although I think it may be too much for anyone to reasonably look through).
Edit: gDEBugger breaks at the call to glBindTexture(), and when clicking on the breakpoint, the properties window shows a picture of one of my other textures (one that's being loaded by the 3D scene's shaders), it shows that it's trying to load texture number 1, but I know this number is already being used to draw the same 3D scene's texture shown in the properties window... why would glGenTextures() be giving me overlapping texture id numbers? Is this normal or maybe part of the problem?
The black texture was due to me not forwarding some vertex shader inputs (normals) through to the fragment shader, even though I'm not using normals for anything in the 2D overlay shaders. As soon as I added outputs for all the inputs, and forwarded them along to the fragment shader, the texture was no longer black, but it was still disappearing after a fraction of a second. This was because I was calling glBindTexture(GL_TEXTURE_RECTANGLE, 0) at the end of the render function with the hopes that it would clean up some state... this was clearly the wrong thing to do, because removing that call caused the 2D texture to stay on-screen. Furthermore, calling glBindTexture() with the GL_TEXTURE_RECTANGLE target seems to work during the texture setup stage, but during rendering the GL_TEXTURE_RECTANGLE target was causing the GL_INVALID_OPERATION​ error. Changing the target to GL_TEXTURE_2D only in the render function made the error go away, and everything seems to work nicely now.

OpenGL FBO - White pixels appear transparent

I'm making a 2D game using OpenGL. I recently tried implementing Framebuffer-objects, and I am having some problems regarding blending.
I'm creating an FBO (using GL_RGBA as format).
When I render to the FBO, I first clear it to fully transparent black, and disable GL_BLEND. I then draw my textures and then I enable GL_BLEND again.
When I'm drawing the FBO-texture, I use GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as source and destination pixels respectively, as the blending-function. I am rendering it as a textured quad.
This does not work properly, as white pixels appear transparent. I have tried experimenting with different blend-function values, but all that I have tried have had issues. I do not fully understand how blending works, so it's hard for me to wrap my head around this. Maybe I'm missing something obvious here?
Here's an image of how it looks right now. There is supposed to be a glow around the button when it is being highlighted, but instead the pixels around it appear transparent: http://i.snag.gy/RnV4s.jpg
You can also see two boxes of text in the image. The top one is drawn normally, without an FBO. The textures are also rendered normally without an FBO, so I know that the problem lies within my framebuffer-code somewhere.
I have pasted my "RenderTarget" class to pastebin (I'm used to calling it a rendertarget instead of FBO): http://pastebin.com/dBXgjrUX
This is how I use it:
RT->Begin();
// draw stuff
RT->End();
RT->Draw();
Can someone help me? Let me know if you need any more info about my issue.
Edit:
Here are the properties of OpenGL that I set on startup:
// Initialize shaders
shaderManager.InitializeStockShaders();
// Set some OpenGL properties
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glAlphaFunc(GL_GREATER, 0.0f);
// Enables/disables
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_DITHER);
glDisable(GL_LIGHTING);
I'ts a bit difficult to tell what your problem is exactly, because you didn't provide source code. Alas, I see several potential troublemakers:
First you told that you want to draw a glow around the button. I presume, that all the buttons are drawn into the FBO, merging them into a UI overlay. Glow sounds to me, like you want to blend something, so you probably also want to have blending enabled, drawing to the FBO.
Next be aware of depth buffer issues. Blending and Depth Buffering have peculiar interactions. In your case I suggest disabling depth testing and depth writes to the FBO (or not using a depth buffer attachment to the FBO at all). Draw the glowing button last, so that it won't block the other buttons from being drawn. Also you must make sure, that your glow comes out with a nonzero alpha value, otherwise it will blend transparent. This is something you control in your shaders, or texture environment (depending on what you use).
Update 1:
Your FBO class doesn't propperly ensure, that textures attached to a bound framebuffer must not be bound themself. It's easy to fix though, by moving attachment code into bind, where the textures are also unbound apropriately. See my edited pastebin http://pastebin.com/1uVT7VkR (I probably missed a few things).

"Rendering" polygons that are transparent OpenGL

I am developing an engine and the way I am handling boundaries the player is not supposed to reach is to have actual polygons as these boundaries. Now, I am wondering how to "render" the polgon but have it non visible.
My main question is: does OpenGL have a way to do this natively?
If not, what if I was to create a texture the way I usually load in the texture but have this texture simply be a single pixel. I could set the alpha channel to that specific pixel color and then use an alpha mask as I do normally with masked textures.
Any advice?
My main question is: does OpenGL have a way to do this natively?
No!
OpenGL only draws nicely colored triangles, lines and point to a framebuffer, and that's it.
It is not a scene graph.
It is not a geometry library.
It is not a collision detection framework.
The question comments solved the problem you are having but there's a way to do what you actually asked so I'm putting it here anyway.
The question as I understood it is about rendering something in the depth buffer (and maybe stencil) but nowhere else. To achieve this, you simply have to use glColorMask like so before drawing the transparent polygons:
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
and restore the color mask afterwards.
If you want to render the boundaries as transparent polygons - you could use the blending method as given here : http://www.opengl.org/resources/faq/technical/transparency.htm
Is that what you were looking for ?