glUniform1i(int, Bool) vs. glUniform1i(int, int) - opengl-es

I'm trying to port some OpenGLES code from iOS to Android.
In iOS I have this code:
- (void)setColorOn:(BOOL)yes
{
glUniform1i(colorOnUniform, yes);
}
where the glUniform1i() method takes an Integer as a Uniform location and a Boolean.
In Android, the closest I can get is this:
public void setColorOn() {
GLES20.glUniform1i(colorOnUniform, 0);
}
where the glUniform1i() method takes an Integer as a Uniform location and another Integer, I think as a texture id...
I've dug around the Kronos documentation, but can't seem to find a proper translation ...
Thoughts ?

Wait what? The glUniform1i takes 2 integers in both cases. Why the developer inserted a boolean value I have no idea. Anyway the iOS boolean value translates as YES(true) = 1 and NO(false) = 0. This value is probably used in shader as if(colorOnUniform == 0)...; else...;
This method should probably be like:
- (void)setColorOn:(BOOL)yes
{
glUniform1i(colorOnUniform, yes?1:0);
}
Issue resolved...

Related

Easiest way to get time between TouchesMoved method calls

I've been searching for easy way to get time step between TouchedMoved method calls in cocos2d-x, but so far I find nothing.. Could you help me out here?
You can accomplish it directly with the C++ primitives, follow this link:
http://www.cplusplus.com/reference/ctime/time/
You'll find a sample script which demonstrates how to calculate difference between two times.
Another way is to sum the delta time of the update method into an instance var, like this:
void YourClass::update(float dt)
{
m_timer += dt;
}
Then in your onTouchBegin, onTouchMoved and onTouchEnded methods get the value of m_timer and count the difference. For example:
void YourClass::onTouchBegin(cocos2d::Touch *touch, cocos2d::Event *event) {
float m_beginTime = m_timer;
}
void YourClass::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
float m_endTime = m_timer;
float time_diff = m_endTime - m_beginTime;
}

How to have a method return a float with multiple parameters in xcode?

I know there have been questions asked on this topic but they all differ slightly
what I want is a simple method that you pass an x value a y value and a quadrant value and it returns angle so far I have...
-(float)getAngle: (float)x yvalue:(float)y Quadrant:(float) quadrant{
float angle=0.0;
if(quadrant==1){
}
NSLog(#"%f",x);
return angle;
}
im not concerned about calculations at this point. I just want to know how to properly declare this method, because it wont let me name the first parameter.
Your code, as is, would work fine. Suppose you have a method like what you posted:
-(float)getAngle: (float)x yvalue:(float)y Quadrant:(float) quadrant{
float angle=0.0;
if(quadrant==1){
}
NSLog(#"%f",x);
return angle;
}
You could call this within the current class, and receive the returned value, like so:
// sample values added
float result = [self getAngle:-1.0 yvalue:0.5 Quadrant:2.0];
NSLog(#"%f",result);

Custom NSAnimationCurve

Does somebody know if is it possible somehow to create a custom NSAnimationCurve so that it could be used with NSViewAnimation objects but was different from standard linear, EaseIn/Out?
Actually a have already found an answer to my question. I've created a delegate for my animation NSViewAnimation object and set it using:
[animationObject setDelegate: delegateObject];
Then in header file for my delagateObject I set it to use "NSAnimationDelegate" protocol typing the following string:
#interface delegateObject : NSObject <NSAnimationDelegate> {
After that I create a method
-(float)animation:(NSAnimation *)animation valueForProgress:(NSAnimationProgress)progress;
This should be a function that describes your custom animation curve. So it takes the progress of animation as value from 0.0 to 1.0 and converts it to new value from 0.0 to 1.0 according to function which you use.
I used in my code the following function:
-(float)animation:(NSAnimation *)animation valueForProgress:(NSAnimationProgress)progress {
float value = -1/(20*(progress+0.047)) +1.045;
return value;
}
It is something like EaseOut but working properly without need to change Start and End KeyFrames and with much more significant difference of speed at the beginning and end of animation.

Basic syntax for an animation loop?

I know that jQuery, for example, can do animation of sorts. I also know that at the very core of the animation, there must me some sort of loop doing the animation. What is an example of such a loop?
A complete answer should ideally answer the following questions:
What is a basic syntax for an effective animation recursion that can animate a single property of a particular object at a time? The function should be able to vary its target object and property of the object.
What arguments/parameters should it take?
What is a good range of reiterating the loop? In milliseconds? (Should this be a parameter/argument to the function?)
REMEMBER:
The answer is NOT necessarily language specific, but if you are writing in a specific language, please specify which one.
Error handling is a plus. {Nothing is more irritating (for our purposes) than an animation that does something strange, like stopping halfway through.}
Thanks!
typically (for jQuery at least) this is not done in a loop, but rather in a series of callbacks.
pseudojavascript:
function startAnimation(element, endPosition, duration) {
var startPosition = element.position;
var startTime = getCurrentTime();
function animate() {
var timeElapsed = getCurrentTime() - startTime;
if (timeElapsed > duration) {
element.position = endPosition;
stopTimer();
} else {
// interpolate based on time
element.position = startPosition +
(endPosition - startPosition) * timeElapsed / duration;
}
}
startRepeatingTimerWithCallbackAndInterval(animate, 1.0 / 30.0);
}
It's also possible to use objects to store starting data instead of closures.
This doesn't completely answer all the points in the question, but it's a starting point.

Retain a random number across different functions in Cocoa?

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "initializer element is not constant." The compiler doesn't want to make a variable from a random number because the random number function is not constant.
How do I generate a random number and then use that same value for more than one action? (For example, to define a color and then write that value to a label?)
Code:
#import "Slider_with_IBAppDelegate.h"
float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);
//^this is where I get the error: "initializer element is not constant"
#synthesize label
//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:#"%f", hue];
}
//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{
self.label5.backgroundColor = [UIColor colorWithHue:hue
saturation:1.0
brightness:1.0
alpha:1.0];
}
----edit------
Thanks for the suggestions. It still doesn't work for me, though, what am I doing wrong?
New code:
#import "Slider_with_IBAppDelegate.h"
float const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);
//^I still get the error: "initializer element is not constant."
#synthesize label
//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:#"%f", hue];
}
//^this is where I get the error "'hue' undeclared (first use of this function)"
//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{
hue = ((arc4random() % ((unsigned)1000 + 1))/1000.0);
/*here I get the error "assignment of read-only variable 'hue.'"
If I insert "float" just before hue, I do not get this error,
but it still won't compile because of the error above.*/
self.label5.backgroundColor = [UIColor colorWithHue:hue
saturation:1.0
brightness:1.0
alpha:1.0];
}
Make it non-const and initialize it in applicationDidBecomeActive. Is there a reason it must be constant?
I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "incompatible types in initialization."
float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);
That's not a function; it's an expression. I'd be surprised if you're not also getting an error here, because you can't initialize a global variable with an expression that isn't constant. As alltom.com says, you need to assign to it from applicationDidBecomeActive:.
The warning is because you've given the variable a pointer type (float *), but you're not assigning a pointer to it. Cut out the asterisk, because you're not going to put a pointer in this variable.
Xcode doesn't want to make a variable from a random number because the random number function is not constant.
Xcode doesn't care one way or the other. It's just reporting the findings of the compiler. By default, the compiler for Objective-C is GCC, but Xcode supports other compilers (and Xcode does come with one other C/Objective-C compiler: LLVM-GCC).
… I couldn't call the same value for the label.
You're not showing a label here, and you can't call a value. You can only call a function, and you don't have one in the code shown.
It gave me the error "function undefined: first use of this function" in doButton even though it was defined in applicationDidBecomeActive.
No, it wasn't. Assigning to a variable does not create a function.
In case anyone is wondering, I finally found a way to do this effectively. (I am sure this is what alltom was saying, I was just too dumb to understand.)
I declared a float and a seed in my .h file:
- (float)generate:(id)sender;
- (void)seed;
And in the implementation file, I defined the float as a random number, and I used srandom() as a random seed generator.
- (float)generate:(id)sender
{
//Generate a number between 1 and 100 inclusive
int generated;
generated = (random() % 100) + 1;
return(generated);
}
- (void)seed {
srandom(time(NULL));
}
Then anywhere I wanted to retain a random number, I used
srandom(time(NULL));
generated1 = ((random() % 100) + 1)/100.0;
to initiate the number, and from there I was able to use generated1, generated2, hue, etc. as variables in any function I wanted (and I made sure to declare these variables as floats at the top of the file).

Resources