Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411586 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 06, 2024, 06:22:56 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)General thread for quick questions
Pages: 1 ... 26 27 [28] 29 30 ... 69
Print
Author Topic: General thread for quick questions  (Read 135807 times)
Polly
Level 6
*



View Profile
« Reply #540 on: February 11, 2016, 07:23:05 AM »

is the tradeoff for swapping programs too big?

Binding a shader is relatively costly. However, if you can manage to arrange your render order so that you'll only need to bind / switch-between two shaders per frame, it will very likely be worth it when there's a performance difference between the them.

I'v researched edge detection, and the kernal seems to do exactly what I want, but I don't know how to get it to work the same at all distances

You need to render to a FBO / RenderTarget first, and then render that to a fullscreen quad using your outline fragment shader.
Logged
Oats
Level 1
*


High starch content.


View Profile
« Reply #541 on: February 11, 2016, 07:40:12 AM »

@Prinsessa Padding isn't the issue, and although I didn't read the full thread the posts I did read showed the game as 2d, my problem is that my game is 3d. Some game objects are rendered as sprites that either always face the camera or face the camera along the horizontal plane. I render the sprites on quads with fragments that have alpha < 0 ignored, I would like a 1 pixel black outline of the non ignored fragments. Right now I'm semi achieving this by having a black border around my sprites in the texture, but as the sprite gets further and further away the sampling of the texture in the fragment shader sometimes doesn't choose to pick the border, so as sprites move away from the camera the borders look like they have holes in it.
eg:

As you can see in the plant on the left, the border is lost, on the right the plant was pasted there. I would like it be so that rendering a sprite with just one colour and transparency gives a correct, full 1 pixel wide border, I do not know how to do this.

@Polly who wrote their post while I was writing this, I now I could do that, but the problem is if I did only a border between change in colour or transparency would have a border, so sprites next to each other would merge, and changes in colour on the sprite would also have a border, which I do not want.
Logged

eh
Polly
Level 6
*



View Profile
« Reply #542 on: February 11, 2016, 07:55:26 AM »

the problem is if I did only a border between change in colour or transparency would have a border, so sprites next to each other would merge, and changes in colour on the sprite would also have a border, which I do not want.

In that case you could render all sprites in a unique color* per sprite with the alpha channel of their texture applied to a separate target.

*Alternatively you could use your depth pass .. if you're already using one.
Logged
oahda
Level 10
*****



View Profile
« Reply #543 on: February 11, 2016, 07:57:15 AM »

is the tradeoff for swapping programs too big?

Binding a shader is relatively costly. However, if you can manage to arrange your render order so that you'll only need to bind / switch-between two shaders per frame, it will very likely be worth it when there's a performance difference between the them.
Yeah, that was the intention, ordering the lights by type first. I guess I'll try it out... It'll be more than two shaders, tho. There are currently three types and I might add more.

Another question: geometry shader perfomance? Slower than passing a quad to a buffer object? I thought myself clever when using them for particles, but I read today someone claiming geometry shaders to be slow?

my problem is that my game is 3d
Aaah, sorry. Didn't catch that. I dunno... Scaling the the objects a little bit to make sure they can always contain their border, and then setting the border size in the shader based on the distance from the camera, zoom level et cetera? Huh?

How are you outlining the walls, tho? They seem to have a consistent line width no matter where in the scene they are...
« Last Edit: February 11, 2016, 08:22:56 AM by Prinsessa » Logged

Oats
Level 1
*


High starch content.


View Profile
« Reply #544 on: February 11, 2016, 08:45:03 AM »

@Polly, I think using an FBO is my current best bet, maybe render the sprites in a different target sharing the same depth buffer and camera perspective then use colour transitions? It's 3 in the morning in Australia so Imma sleep now but I think I'll try that tomorrow.

How are you outlining the walls, tho? They seem to have a consistent line width no matter where in the scene they are...
They are using a different technique, they're rendered as filled polygons, I then render a wireframe of them. The filled portions have their depth values slightly offset to avoid Z fighting with the lines. I thought of just generating a wireframe mesh for each sprite then using the same technique, which I'm quite sure would work but seems to be less then optimal for both performance and programming time.
Logged

eh
Polly
Level 6
*



View Profile
« Reply #545 on: February 11, 2016, 09:35:06 AM »

maybe render the sprites in a different target sharing the same depth buffer and camera perspective then use colour transitions?

That works yes Smiley In fact, you're going to need the depth pass when you only want to outline the closest sprite of overlapping sprites ( see below ).

Logged
ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #546 on: February 17, 2016, 03:01:26 AM »

Why can't I simultaneously create a variable and pass it's pointer to a function in C++?

It seems like this would be a useful thing to have for classes, where I can create and initialize an object, and then pass it's pointer directly to a function so that I don't have to backtrack in code and create it beforehand, especially when I wouldn't intend to use it later in code. Or am I wrong and there's a secret way to do it that would require a crazy workaround?
Logged

oahda
Level 10
*****



View Profile
« Reply #547 on: February 17, 2016, 03:26:02 AM »

You can. What's going wrong?

Code:
function(&Vector{});
function(new Vector);

Both of these should work...

How are you doing it?
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #548 on: February 17, 2016, 03:32:41 AM »

Don't these lead to memory leaks though? I meant to create objects of local scope and give their pointer to a function at the same time.

Edit: I mean to say return the pointer at the same time, I don't necessarily want to give it to a function.
Logged

oahda
Level 10
*****



View Profile
« Reply #549 on: February 17, 2016, 06:03:10 AM »

Don't these lead to memory leaks though? I meant to create objects of local scope and give their pointer to a function at the same time.
The former is on stack, so it cannot leak. It dies when the function to which it is passed finishes execution.

The latter leaks if you don't delete it inside the function to which it was passed, but as long as you do that, it's fine.

Edit: I mean to say return the pointer at the same time, I don't necessarily want to give it to a function.
Then I'm not quite sure what you mean. Sad Could you try to explain it in more detail, perhaps along with pseudocode and/or the C++ code you're trying to run that's not working?
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #550 on: February 17, 2016, 06:35:11 AM »

Let's say that I want to create an object on the stack. I want to know if there is a way to do that AND return it's pointer at the same time, something like this:
Code:
classname* foo = &(classname object{arguments})

Basically combine these two statements:
Code:
classname object{arguments};
classname * foo = &object;

Edit: I should mention as well that the code snippet you provided would be adequate for all useful cases that I can think of, I just wanted to make sure that there wasn't a more general rule that would allow for some clever coding practice or something.
Logged

oahda
Level 10
*****



View Profile
« Reply #551 on: February 17, 2016, 07:03:28 AM »

Oh.

First off, since you're not storing it in a variable in the latter expression, you cannot specify a variable name there. Just like you can't pass a temporary to a function by writing function(classname object{}); you have to write, simply, function(Vector{}).

But the reason this doesn't work is that the stack object created in a statement like auto *a(Vector{}} dies at the end of that statement, which would render the pointer invalid immediately. If you separate them, however, the temporary will belong to the scope above, in which case it will live until the end of that scope, which is why it is safe to take its address within that scope.

But even if you could contract the statement, whichever way you write it, the pointer will only be valid within that scope, meaning that you have no use of the address outside the scope, meaning that taking the address is completely pointless in the first place.

Code:
{
Vector v;
Vector *p{&v};

// What is the point of this...
p->x = 5;

// ... when you can just do this?
v.x = 5;
}

// You cannot access either variable out here anyway.

Code:
// Declaring this in the upper scope.
Vector *p;

{
Vector v;
p = &v;
}

// Crash / undefined behaviour; the address pointed to is no longer
// valid since it went out of scope and died just above.
p->x = 5;
« Last Edit: February 17, 2016, 07:50:44 AM by Prinsessa » Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #552 on: February 17, 2016, 08:05:59 AM »

Ok. I assume that your "vector" object automatically takes the form of whatever argument the function is expecting?

Btw I know how scope works lol, but thanks for the refresher anyways :D
Logged

oahda
Level 10
*****



View Profile
« Reply #553 on: February 17, 2016, 08:26:31 AM »

Ok. I assume that your "vector" object automatically takes the form of whatever argument the function is expecting?
Arguments in C++ are based on position, not name. You can't specify named arguments like in some other languages. So yes, the vector will end up as the argument at the position where it is passed.

Btw I know how scope works lol, but thanks for the refresher anyways :D
Then I still don't understand what you mean because I just told you why the literal code you posted won't work. Sad
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #554 on: February 17, 2016, 08:59:50 AM »

I know that the code I posted would't work, I tried it before posting. I was just trying to illustrate what I was asking, aka a c++ instruction that creates a variable/class of a specified type while also returning that thing's pointer. And I am aware that c++ functions work with the ordering of their arguments, it's actually pretty important for things such as overloading for an example. I'm just not familiar with vectors I guess.
Logged

oahda
Level 10
*****



View Profile
« Reply #555 on: February 17, 2016, 09:02:50 AM »

Oh, pay no attention to the vector. It was just my stand-in datatype. It could've been an int or a Boat or whatever, primitive or user-defined. There was nothing particular behind my choice of using Vector; it was the same as your classname.

But yeah, if you want to return a pointer you have to make sure that the pointer survives beyond the scope of the function that returns it, meaning that it cannot be allocated locally within the scope of the function; you need to allocate on the heap, but perhaps you can use one of the new C++ smart pointers to do this without having to deal manually with deällocation.
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #556 on: February 17, 2016, 09:21:06 AM »

So, if a function in C++ is expecting a particular type pointer, and I attempt to pass as an argument the pointer of a newly created object of a non-specified type, the function will automatically detect what the type of the new object should be and it will allow me to correctly give values to the members variables of the new object through the curly brackets, correct?

Also returning a pointer is the least of my worries right now.

I fact, let me give you a bit of context to my question before we go further.

In SDL, there are functions that expect objects of type SDL_Rect. But to be able to pass to them an object, you must create that object first and optionally give it's member variables starting values with the curly brackets. What I wanted to do is move the object creation to inside the function call, so that I don't have to think ahead of time what arguments I want to pass to a function. Basically I want to create a throwaway object just for the purpose of giving it values and passing it to a function. I won't need it again further in the code, and I assumed at first that this would be done by some sort of syntax that would return the pointer of a newly created object, while also making that object tied to the current scope.

Am I making sense?
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #557 on: February 17, 2016, 11:39:27 AM »

I understand what you are trying to do. But it cannot done with SDL. You can pass temporary arguments only using references, not pointers.

Code:
class MyRect{};

int Func1(const MyRect* myRect);
int Func2(const MyRect& myRect);

void main(){
Func1(&MyRect{}); // Compile error, cannot take address of temporary
Func1(new MyRect{}); // Compiles, but leaks
Func2(MyRect{}); // Fine
}

For this reason, many C++ libraries accept value types (such as rects) by const reference rather than const pointer. As SDL does not (being a C library), you won't be able to do this. There are some hacks to work around this, but they are not worth it. You are stuck with the verbose syntax.

You could maybe look for a C++ wrapper around SDL if you are fed up of the C-style verbosity.

Logged
oahda
Level 10
*****



View Profile
« Reply #558 on: February 17, 2016, 12:21:10 PM »

Ah, now I get it. But too late. Boris got here first. What Boris said!
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #559 on: February 17, 2016, 12:23:59 PM »

Well, thanks to the both of you for the explanation. I guess I'll have to use SDL the way it was meant to be used, that is, like a C library.

I guess I could just make a wrapper function for each function that takes a rectangle as an argument and then have that function create a rectangle form the arguments and pass its pointer to the function, but that would be both tedious and time consuming. Still closer to what I want than what the language allows though.
Logged

Pages: 1 ... 26 27 [28] 29 30 ... 69
Print
Jump to:  

Theme orange-lt created by panic