|
I'm not certain what you're asking here.
Inside OpenGL_Stencil , the name of the parameter that was passed in (a function) is Stencil . If you're talking about the int parameter for Stencil , OpenGL_Stencil has to provide that itself. It isn't the parameter associated with Stencil , because that only describes Stencil 's function signature and could actually be left out, the same as in a function declaration:
void OpenGL_Stencil(void (*Stencil)(int))
If OpenGL_Stencil doesn't know what value to pass to Stencil, you could provide it like this:
void OpenGL_Stencil(void (*Stencil)(int), int value) after which you can invoke
Stencil(value)
|
|
|
|
|
May try it this way
the parameter is a function - Stencil
such function is passed with its own parameter - such in Stencil(parameter)
how do I access that parameter "value" in OpenGL_Stencil function?
I need to do
if(Stencil(parameter == x )
|
|
|
|
|
You have to pass it as a parameter to OpenGL_Stencil , the way I described in the second half of my post.
|
|
|
|
|
OK, I think I have the concept - have passed function return value insed of being void.
Now I am still not sure about the proper syntax.
Here is my code
OpenGL_Stencil_Circle_Build(parameter); returns parameter OK
OpenGL_Stencil(OpenGL_Stencil_Circle_Build(6)); wrong syntax
And here is the error
Description Resource Path Location Type
Invalid arguments '
Candidates are:
int OpenGL_Stencil(void (*)(int))
' A_STENCIL.cpp /A_STENCIL/src line 766 Semantic Error
invalid conversion from ‘int’ to ‘void (*)(int)’ [-fpermissive] A_STENCIL.cpp /A_STENCIL/src line 766 C/C++ Problem
The declaration may be the issue ?
int OpenGL_Stencil(void (*f)(int parameter));
Could somebody please reply with correct syntax or help me with declaration ?
|
|
|
|
|
I believe that this
invalid conversion from ‘int’ to ‘void (*)(int)
means that you are passing an int argument when a function argument is expected, specifically a function that takes one int parameter and returns void .
The problem appears to be
OpenGL_Stencil(OpenGL_Stencil_Circle_Build(6))
where, based on previous posts in this thread, I assume you want OpenGL_Stencil to call OpenGL_Stencil_Circle_Build with a value of 6 . If that's the case, you did not read my previous post very carefully, because you need to do it as follows:
OpenGL_Stencil(OpenGL_Stencil_Circle_Build, 6)
where OpenGL_Stencil is defined as
void OpenGL_Stencil(void (*func) (int), int arg)
{
func(arg); }
|
|
|
|
|
It is now working, however, the mechanics are NOT what you have presented.
The initial problem was to pass a parameter to the passed function such as
Stencil(parameter); // parameter);
Now in my poor English interpretation I have
A function OpenGL_Stencil with first argument int (*Stencil)(int) and second argument int parameter
The OpenGL_Stencil returns int which is not currently used.
The first argument - function takes the second argument as a -parameter- and returns it.
The returned value is used to switch the code which follows.
That was the original task , however, I could just use the passed second argument to do the switching, without the use of the return value.
But that is just the way I like to make sure the code is actually processing the first argument - the function.
I believe my original misunderstanding was trying to pass the second argument - parameter _ as a argument to the first argument - the function.
Next task - pass multiple arguments or a pointer....
Many thanks for your help, really appreciate it.
Cheers.
|
|
|
|
|
|
Just FYI, this is NOT a repost.
As usual I have posted the original in WRONG pew.
However, I found another even weirder failure of simple math case and I really do not have a clue what I am doing wrong.
Are my local variable assignments incorrect ?
Is "printf" lying to me ?
Here is the entire test code
#define MAX_CIRCLES 5
struct TAG_Circles {
float radius;
float center;
float center_x;
float center_y;
} REAL_Circle[MAX_CIRCLES], IMG_Circle[MAX_CIRCLES];
void OpenGL_Stencil_Circle_Build(void);
void OpenGL_Stencil_Circle_Build(void) {
int index = 5;
{
float radius = IMG_circle[index].radius;
float center_x = IMG_circle[index].center_x;
float center_y = IMG_circle[index].center_y;
printf("\n Last circle #%i radius struct %f radius %f", index,
REAL_Circle[index].radius, radius);
printf("\n Last circle #%i center_x struct %f center_x %f", index,
REAL_Circle[index].center_x, center_x);
printf("\n Last circle #%i center_y struct %f center_y %f", index,
REAL_Circle[index].center_y, center_y);
}
}
Here is the output
Last circle #5 radius struct 0.065789 radius 0.131579
Last circle #5 center_x struct 0.000000 center_x 0.118421
Last circle #5 center_y struct 0.000000 center_y 0.000000
The questions are NOT what I am trying to accomplish with the code ,
the questions are:
1. why values printed FROM struct DO NOT match assigned values ?
2. why is "radius" TWICE of the value assigned from struct?
PS
Only
radius struct 0.065789 is of correct value.
Cheers
|
|
|
|
|
Could it be because you're assigning radius from IMG_circle but printing the value of REAL_circle ?
Keep Calm and Carry On
|
|
|
|
|
I am not so sure how to answer this (honestly).
Maybe I should get a different hobby - like basket weaving...
For what it is worth
I started with IMG and then realized it was wrong struct, but changed only part of the code !
|
|
|
|
|
Vaclav_ wrote: Here is the entire test code As usual it is not the entire code, since none of your struct entities have been initialised with values. Please provide the values of all the variables that you are using, explain what you are trying to calculate, and why you consider the results are wrong.
|
|
|
|
|
Yet another one of my favourites - academic questions.
Hopefully a decent discussion will follow, but please no more RTFM.
Cheers
|
|
|
|
|
Unless you have a good, compelling argument otherwise (and there are a few), you should prefer to pass structs as parameters. If its small (e.g. something like a struct timeval ), and you don't need to modify the members, its OK to pass by value. If its large, then pass by reference (pointer). If you're not going to modify the struct in the function then you should mark the reference as const.
struct small_obj {
char description[16];
int count;
};
struct large_obj {
char text[1024][1024];
double factor[1024][1024];
};
void f1(small_obj arg);
void f2(small_obj* arg);
void f3(const small_obj *arg);
void f4(small_obj& arg);
void f5(const small_obj& arg);
void f6(large_obj arg);
The problem with global values, especially global values that are not local to a single file (e.g. declared as static), is it becomes increasingly more difficult to reason about where the variable gets modified. Given a function say print_item(void) you might not expect your global Item to get modified, but print_item() calls foo(void), which calls bar(void) which calls frobnicate(void), which modifies Item. If you are trying to trace down a bug, trying to see where Item gets modified can be difficult. On the other hand, calling print_item(const Item&), you can be (more or less) sure that Item doesn't get modified during the lifetime of print_item() (more or less because print_item() could be nasty and cast away constness, but that's an evil for another tale). Alternatively, you might have print_item(Item foo), which gets a local copy of Item, so the caller knows that whatever changes print_item might make to its copy, the callers Item remains unchanged.
Keep Calm and Carry On
|
|
|
|
|
Thanks, you may have answered my other problem.
( I'll posting it soon.)
At this point I use struct sort-off attached to a class.
Not static.
I am saying sort-off because the main code is OpenGL and it does not work well in C++ class.
I do not foresee any other code to modify it.
But for learning experience I'll go with passing the struct via pointer to the function which is also "passed" as parameter to another function.
Pretty convoluted , mostly because OpenGL "pipe" architecture.
But so far it id working as expected.
|
|
|
|
|
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
|
|
|
|
|
No - it depends on the compiler, the library, the OS, the processor, the system loading, the ... you get the idea.
You want to find out? Write code to execute the same function for a huge range of values and run it many, many times. Time that, and average out the run time by the number of sin operations. Ruin this a dozen times, and average out the values.
Then take out the "payload sin call" and repeat the tests. The difference should be the execution time of the sin operation.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks, if we talk about functions ( and other math operations, additions, substractions, multiplication, division) with a wide margin cost difference can`t you make a ranking of them.
I want to make a raytracer and I`m just bracing myself against making foolish things/mistakes.
|
|
|
|
|
That is more generally the compiler/optimizer responsibility. You are better off leaving that sort of thing to the end.
I can highly recommend learning the whole process on a simple software renderer
You will be hard pressed to find a better start point than this link because it's really a couple of simple files.
GitHub - ssloy/tinyrenderer: A brief computer graphics / rendering course[^]
He has a nice easy to follow sequence from raycaster to the renderer being the most complex but on 500 lines of code.
Home · ssloy/tinyrenderer Wiki · GitHub[^]
In vino veritas
modified 17-Apr-20 13:10pm.
|
|
|
|
|
hey leon de boer
Quote: I can highly recommend learning the whole process on a simple software renderer
I`m familiar to DirectX, I find raytracing intriguing, I want to give it a try.
|
|
|
|
|
Write the code AND THEN refactor (based on analysis of the actual code.)
|
|
|
|
|
That is the agile spirit! Do something quick and dirty, AND THEN discover that it was rather silly...
I admire fearless_ for wanting to do a prestudy do determine the cost of various alternatives, to rule out the obviously invalid ones. Then he dan make a small testbed for the candidates, and from that choose the one looking most promising, and then start coding.
I know very well that agile principles demand that you start your "int main(int argc, char *argv[]) {return 0;}" an make sure it compiles, as a prerequisite before you start asking what the problem is, that you are on your way to solving that problem that hasn't even been ideitified yet. I am so old styel that I prefer to tanke a look at both problem definition and tools available before I race along in that "implementation before problem understanding" race. I know that I am a silly old fool, thinkoing that way
|
|
|
|
|
This has NOTHING to do with agile. This is a tried and true principle of software engineering; first solve the problem and then refactor and optimize.
|
|
|
|
|
I'm sorry for stepping on your toes.
Obiviously, "Code first, refactor later" is not at all related to agile coding.
|
|
|
|
|
I suppose you might find benchmarks on the web. At least you may perform some tests yourself.
|
|
|
|
|
Are you really sure that it matters?
It might, in some very special applications. But most developers tend to overestimate the relative fraction of "calculation" in their code. The data handling often takes up a far larger fraction of the processing time that they thought.
Also: How often do you have a real choice? If your algorithm calls for a sin(), is there anything you can do to avoid it? Not very often! If you do have a choice, implementations vary so much that you should set up a simple test bed and try it out.
One thing I have learnt through simple timing tests: On any CPU architected the last 30 years, timing individual instructions is more or less meaningless. Prefetching and pipelining and speculative execution and whathaveyou can make addition of a simple instruction (e.g. arithmetic) almost unnoticable on the execution time of a tight loop. So don't worry about those.
What really matters is not the execution time of the trig functions (and other mathematiclly complex operations) in themselves, but the number of times you call them. I never programmed any ray tracing myself, but I would not be surprised if there is a lot to be saved in caching, reusse and clever prioritizing to make the most visible effects appear first, details only if you have got the time to do it, before the next update.
|
|
|
|
|