How do I MVP matrix?
Seriously, this is a pain in the ass. All I need to know is what order I load, multiply, etc. the matrices for the vertex shader and in my C++ code. I'm using GLM for the perspectiveFov and lookAt functions.
[QUOTE=Cesar Augusto;37693980]What C function do I use to print colored in console?[/QUOTE]
ansi escapes on sane systems, funky win32 api calls on windows
[QUOTE=swift and shift;37696893]ansi escapes on sane systems, funky win32 api calls on windows[/QUOTE]
An actual console API is ten times more sane than escape sequences.
[QUOTE=dajoh;37697856]An actual console API is ten times more sane than escape sequences.[/QUOTE]
i'm not arguing either way, i'm just saying that unix systems are generally more sane than windows
[QUOTE=ECrownofFire;37695248]How do I MVP matrix?
Seriously, this is a pain in the ass. All I need to know is what order I load, multiply, etc. the matrices for the vertex shader and in my C++ code. I'm using GLM for the perspectiveFov and lookAt functions.[/QUOTE]
Something like this
[cpp]
glm::mat4 model;
glm::mat4 view = glm::lookAt(glm::vec3(0, 20, 40), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
glm::mat4 projection = glm::perspective(60.0f, 800.0f / 600.0f, 1.0f, 100.0f);
glm::mat4 mvp;
[/cpp]
in update:
[cpp]
model = glm::rotate(glm::mat4(1.f), time, glm::vec3(1, 0, 0));
model = glm::translate(model, glm::vec3(-width/2.f, -height/2.f, -depth/2.f));
mvp = projection * view * model;
[/cpp]
You should post code there's probably other problem like indices etc
In C++ I want to have a triangle class, made from 3 verticies each of which will be a float[3]
so the class will probably be something like:
[cpp]
struct vertex
{
float x;
float y;
float z;
};
class Triangle
{
vertex a, b, c;
int someVariable;
void someFunctions;
}
[/cpp]
I will then have a VBO which accepts floats but I want to pass it an array of triangles
How can I make sure that when this gets compiled/executed all the floats/verticies are packed continuously?
[QUOTE=Richy19;37698769]In C++ I want to have a triangle class, made from 3 verticies each of which will be a float[3]
so the class will probably be something like:
[cpp]
struct vertex
{
float x;
float y;
float z;
};
class Triangle
{
vertex a, b, c;
int someVariable;
void someFunctions;
}
[/cpp]
I will then have a VBO which accepts floats but I want to pass it an array of triangles
How can I make sure that when this gets compiled/executed all the floats/verticies are packed continuously?[/QUOTE]
A perhaps ugly way is to map the struct as a float[3] array with something that I have forgot the name of but it saves a pair of two different types and exist in boost.
[cpp]struct vertex
{
union
{
float f [3];
struct
{
float x, y, z;
};
};
};[/cpp]
No padding is allowed between the individual coordinates, but the struct itself may be padded.
In Visual Studio (and apparently [url=http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html]GCC supports it as well[/url]), you can use [url=http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx]#pragma pack[/url]:
[cpp]
#pragma pack(4)
struct vertex
{
float x, y, z;
};
#pragma pack()[/cpp]
[QUOTE=ECrownofFire;37695248]How do I MVP matrix?
Seriously, this is a pain in the ass. All I need to know is what order I load, multiply, etc. the matrices for the vertex shader and in my C++ code. I'm using GLM for the perspectiveFov and lookAt functions.[/QUOTE]
I use matrices in my engine. You can look at it here:
I load in the model matrix on line 196 here: [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Button.cpp[/url]
The perspective matrices only when the window changes on line 163/168: [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Render.cpp[/url]
And the view matrices on line 192/197 here:
[url]https://github.com/naelstrof/Astrostruct/blob/master/src/Render.cpp[/url]
While they are all multiplied in the shaders here:
[url]https://github.com/naelstrof/Astrostruct/blob/master/bin/data/shaders/flat.vert[/url]
[QUOTE=ThePuska;37705386][cpp]struct vertex
{
union
{
float f [3];
struct
{
float x, y, z;
};
};
};[/cpp]
No padding is allowed between the individual coordinates, but the struct itself may be padded.
In Visual Studio (and apparently [url=http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html]GCC supports it as well[/url]), you can use [url=http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx]#pragma pack[/url]:
[cpp]
#pragma pack(4)
struct vertex
{
float x, y, z;
};
#pragma pack()[/cpp][/QUOTE]
Would that also make the triangle (and all triangles in an array) 's verticie floats padded continuously If the triangle class was like:
[cpp]class T
{
Verticie v[3];
int i;
string s;
}[/cpp]
Also Im looking at function pointers for the purpose of a GUI following this: [url]http://www.cprogramming.com/tutorial/function-pointers.html[/url]
However I would need to use class functions, so I have this example but it doesnt work
[cpp]
#include <iostream>
class button
{
public:
button(){}
void (*onClick)();
void Click()
{
if(onClick != NULL)
onClick();
}
};
class handler
{
public:
handler(){}
void clicked()
{
std::cout << "click" << std::endl;
}
};
int main()
{
button b;
handler h;
b.onClick = &h.clicked;
b.Click();
return 0;
}
[/cpp]
Any idea how its done?
Is there a good free GUI framework that is cross-platform and works a bit like WPF?
I need something that handles layout and allows drawing or rendering controls manually.
[QUOTE=Richy19;37705537]Also Im looking at function pointers for the purpose of a GUI following this: [URL]http://www.cprogramming.com/tutorial/function-pointers.html[/URL]
However I would need to use class functions, so I have this example but it doesnt work
#include <iostream>class button{public: button(){} void (*onClick)(); void Click() { if(onClick != NULL) onClick(); }};class handler{public: handler(){} void clicked() { std::cout << "click" << std::endl; }};int main(){ button b; handler h; b.onClick = &h.clicked; b.Click(); return 0;}
Any idea how its done?[/QUOTE]
Funny, because I was just wondering how to properly do function pointers this morning (I've always had trouble with them)
Here's what I got:
[cpp]
class Counter
{
public:
int count;
void Increase()
{
count++;
}
};
template<typename T>
void CallFunc( T* receiver, void(T::*func)() )
{
(receiver->*func)(); // ***
}
int main()
{
Counter A;
A.count = 0;
// *** In order for function pointers to work, you first need the address to the function ( &Counter::Increase ) and an object for that function to use ( &A )
CallFunc<Counter>( &A, &Counter::Increase );
std::cout << A.count << std::endl;
return 0;
}
[/cpp]
Someone correct me if I'm doing something wrong here, I'm still learning myself :)
[editline]17th September 2012[/editline]
Also some good info on this site:
[url]www.newty.de/fpt/fpt.html[/url]
[QUOTE=vombatus;37698600]-snip-[/QUOTE]
View matrix is taken through this, then sent to a uniform. Done every frame.
[cpp]
glm::mat4 viewmat() {
using namespace glm;
vec2 ang = Client::getClient().clPlayer.angleLook;//angleLook is two angles, one for the X-Z axis (left/right), one for the Y axis (up/down)
vec3 pLoc = Client::getClient().clPlayer.getLoc();//Location of player
//Calculate where the player is looking from the angles
vec3 d(0.0f);
d.x = cos(ang.x) * cos(ang.y);
d.y = sin(ang.y);
d.z = sin(ang.x) * cos(ang.y);
vec3 k = vec3(0.0f, 1.0f, 0.0f);
mat4 res = lookAt(pLoc, pLoc + d, k);
return res;
}
[/cpp]
The projection matrix is taken through this, done when/if the FOV or window height/width change.
[cpp]
glm::mat4 projmat() {
using namespace glm;
float winW = winWidth;
float windH = winHeight;
mat4 res = perspectiveFov(fov, winW, winH, zNear, zFar);
//zNear and zFar are a couple of consts, set to 0.01f and 256.0f, respectively.
return res;
}
[/cpp]
The two matrices are fed into the shader without transposing, then multiplied in the shader (projmat * viewmat), then the attribute array is multiplied by that result (mvp * vec4(coord3d, 1.0f)).
The index for each face is a simple 0, 1, 2, 2, 3, 0, drawn with GL_TRIANGLES.
The array buffer has these values in it (this is for one example face). These are the expected display locations, so it's not a problem with my buffers.[B]
1, 0, 0
1, 1, 0
1, 1, 1
1, 0, 1
[/B]
After the multiplication, going through the pipeline, etc., the vertices are displayed in these locations (same order as before, found by moving the camera around):
0, 1, 1
1, 1, 0
1, 0, 0
0, 0, 1
Clearly, none of those are where they're supposed to be except the one at 1, 1, 0...
-snip-
Edit: Oh why did you snip that, now my reply looks dumb :[
I feel that sometimes singletons are a necessary evil like globals at times (because they're basically just a bunch of globals packed into one class). The times are limited, but are basically whenever there's something that absolutely MUST be accessed from throughout the code. But 90% of the time, you're much better off just restructuring things to not use them.
Examples are things like display variables like window width and height, the "player" object, a console (cin and cout are technically globals), maybe some kind of sound manager (depending on the structure), and things like that. Basically anything that has its grubby little fingers everywhere in the entire system, but you should really avoid objects like that (see: "god object").
To avoid using them, I would pass things around by reference and declare some kind of "client" object (or whatever) that contains all the managers. That way if you need to access multiple managers you just pass in the entire client by reference. Be warned that this is basically a god object, but I see it as more as just encapsulating the managers, which makes things in general a bit more... manageable.
Also, does anybody have any clue what's going on with my MVP matrix issue? Seriously, I pass in 4 points on a single plane and they render in random fucking spots.
[QUOTE=ECrownofFire;37713290]Edit: Oh why did you snip that, now my reply looks dumb :[
I feel that sometimes singletons are a necessary evil like globals at times (because they're basically just a bunch of globals packed into one class). The times are limited, but are basically whenever there's something that absolutely MUST be accessed from throughout the code. But 90% of the time, you're much better off just restructuring things to not use them.
Examples are things like display variables like window width and height, the "player" object, a console (cin and cout are technically globals), maybe some kind of sound manager (depending on the structure), and things like that. Basically anything that has its grubby little fingers everywhere in the entire system, but you should really avoid objects like that (see: "god object").
To avoid using them, I would pass things around by reference and declare some kind of "client" object (or whatever) that contains all the managers. That way if you need to access multiple managers you just pass in the entire client by reference. Be warned that this is basically a god object, but I see it as more as just encapsulating the managers, which makes things in general a bit more... manageable.
Also, does anybody have any clue what's going on with my MVP matrix issue? Seriously, I pass in 4 points on a single plane and they render in random fucking spots.[/QUOTE]
Sorry ! I thought of something while writing it reducing my singletons from 3-infinite to 1 only which is for the gamestate, from there on I can pass down references / retrieve everything via the state manager (God object *sight*)
So, I'm attempting another dive into Source by making it so you can throw a crowbar on secondary attack. Here's the code so far:
[cpp]void CWeaponCrowbar::SecondaryAttack()
{
m_flNextSecondaryAttack = gpGlobals->curtime + CROWBAR_REFIRE;
CBaseCombatCharacter *owner = GetOwner();
Vector forward, right, up;
owner->GetVectors(&forward, &right, &up);
CPointEntity *ent = (CPointEntity *)CPointEntity::Create("prop_physics", owner->GetAbsOrigin() + forward * 10.0f, owner->GetAbsAngles(), owner);
if(!ent) return;
ent->SetModel("models/props_junk/watermelon01.mdl");
ent->SetAbsVelocity(forward * 16.0f);
ent->Spawn();
}[/cpp]
It's creating a watermelon because apparently the crowbar model needs some propdata or something which I can't be bothered to setup just yet.
For some reason when this runs, I get the message "prop at <pos here> missing modelname".
I have precached the model, what else do I need to do?
[QUOTE=Chris220;37717847]So, I'm attempting another dive into Source by making it so you can throw a crowbar on secondary attack. Here's the code so far:
[cpp]void CWeaponCrowbar::SecondaryAttack()
{
m_flNextSecondaryAttack = gpGlobals->curtime + CROWBAR_REFIRE;
CBaseCombatCharacter *owner = GetOwner();
Vector forward, right, up;
owner->GetVectors(&forward, &right, &up);
CPointEntity *ent = (CPointEntity *)CPointEntity::Create("prop_physics", owner->GetAbsOrigin() + forward * 10.0f, owner->GetAbsAngles(), owner);
if(!ent) return;
ent->SetModel("models/props_junk/watermelon01.mdl");
ent->SetAbsVelocity(forward * 16.0f);
ent->Spawn();
}[/cpp]
It's creating a watermelon because apparently the crowbar model needs some propdata or something which I can't be bothered to setup just yet.
For some reason when this runs, I get the message "prop at <pos here> missing modelname".
I have precached the model, what else do I need to do?[/QUOTE]
The usual usage I see is UTIL_CreateEntityByName (or something of that effect) and creating a prop_physics, then do ->SetPos(...), ->SetVelocity(...), ->SetAngles(...) on it.
The UTIL_CreateEntityByname function might be wrapping something that CPointEntity::Create is not.
[QUOTE=Lord Ned;37721583]The usual usage I see is UTIL_CreateEntityByName (or something of that effect) and creating a prop_physics, then do ->SetPos(...), ->SetVelocity(...), ->SetAngles(...) on it.
The UTIL_CreateEntityByname function might be wrapping something that CPointEntity::Create is not.[/QUOTE]
Thank you, I've replaced the code with this, and it works now.
[cpp]void CWeaponCrowbar::SecondaryAttack()
{
m_flNextSecondaryAttack = gpGlobals->curtime + CROWBAR_REFIRE;
CBaseCombatCharacter *owner = GetOwner();
Vector forward, right, up;
owner->GetVectors(&forward, &right, &up);
CPointEntity *ent = (CPointEntity *)CreateEntityByName("prop_physics");
if(!ent) return;
ent->SetModel(CROWBAR_MODEL);
ent->SetAbsOrigin(owner->EyePosition() + forward * 50.0f);
ent->SetAbsAngles(owner->GetAbsAngles());
ent->Spawn();
ent->ApplyAbsVelocityImpulse(forward * 25000.0f);
}[/cpp]
I'm a total newb to data structures. I've only ever really used arrays and ArrayLists, and now I want a two-dimensional array that can grow and shrink dynamically in any direction, like this:
[IMG]http://i.imgur.com/77PCv.png[/IMG]
Is there a data structure like that?
[editline]asdf[/editline]
I'm gonna use an arraylist of arraylists for now, but let me know if there's a better way!
A hashtable with the hash calculated from the coordinates of the entries.
[QUOTE=Nigey Nige;37723648]I'm a total newb to data structures. I've only ever really used arrays and ArrayLists, and now I want a two-dimensional array that can grow and shrink dynamically in any direction, like this:
[IMG]http://i.imgur.com/77PCv.png[/IMG]
Is there a data structure like that?
[editline]asdf[/editline]
I'm gonna use an arraylist of arraylists for now, but let me know if there's a better way![/QUOTE]
A map from coordinate to value. Alternatively, a graph.
Edit: Ninja'd
[QUOTE=Nigey Nige;37723648]I'm a total newb to data structures. I've only ever really used arrays and ArrayLists, and now I want a two-dimensional array that can grow and shrink dynamically in any direction, like this:
[IMG]http://i.imgur.com/77PCv.png[/IMG]
Is there a data structure like that?
[editline]asdf[/editline]
I'm gonna use an arraylist of arraylists for now, but let me know if there's a better way![/QUOTE]
You could use a graph:
[CPP]
struct Graph
{
DATA data; //Where 'DATA' is whatever information you want to store there.
vector<Graph*> nodes;
long x,y; //The coordinates of the node in physical space. Graphs have no direction.
//Write a nice little constructor
};
void append(Graph* a, Graph* b)
{
a->nodes.push_back(b);
b->nodes.push_back(a);
}
[/CPP]
[QUOTE=Eudoxia;37723845]You could use a graph:
[CPP]
struct Graph
{
DATA data; //Where 'DATA' is whatever information you want to store there.
vector<Graph*> nodes;
long x,y; //The coordinates of the node in physical space. Graphs have no direction.
//Write a nice little constructor
};
void append(Graph* a, Graph* b)
{
a->nodes.push_back(b);
b->nodes.push_back(a);
}
[/CPP][/QUOTE]
[QUOTE=ArgvCompany;37723749]A map from coordinate to value. Alternatively, a graph.
Edit: Ninja'd[/QUOTE]
[QUOTE=ThePuska;37723738]A hashtable with the hash calculated from the coordinates of the entries.[/QUOTE]
Why have I not been using hashtables this whole time? They're awesome! You all get informatives!
[IMG]http://i.imgur.com/FrshA.png[/IMG]
[QUOTE=Nigey Nige;37723967]Why have I not been using hashtables this whole time? They're awesome! You all get informatives![/QUOTE]
In most cases a standard array is simpler and faster, what were you going to use it for?
So in my game, when a packet is sent from the server to the client, the client deserializes it and passes all the data into a client event arg that gets called. The multiplayer state listens on them events. This works ok for most things but I have a very large prespawn packet that pretty much sends down all the game objects thats in the server to connecting players (flags and projectiles). So doing it that way wont work because I wont know before hand what entities get sent down and it's a large amount of data to make a client event arg.
Need a decent way to get a shit load of data from the client to the multiplayer state pretty much.
I think Source avoids all these problems by just making everything global, which I don't want to be doing.
I think it would be reasonable for the multiplayer state to give the client a pointer to it's ent list, that way any state can work with the client aslong as it gives it an ent list pointer.
Here's the final result. Finally, I decided to not use FBOs and adapted my algorithm to use the extra z coordinate.
Thanks for the help.
[IMG]http://imgur.com/BOFYO.png[/IMG]
[QUOTE=laylay;37724155]So in my game, when a packet is sent from the server to the client, the client deserializes it and passes all the data into a client event arg that gets called. The multiplayer state listens on them events. This works ok for most things but I have a very large prespawn packet that pretty much sends down all the game objects thats in the server to connecting players (flags and projectiles). So doing it that way wont work because I wont know before hand what entities get sent down and it's a large amount of data to make a client event arg.
Need a decent way to get a shit load of data from the client to the multiplayer state pretty much.
I think Source avoids all these problems by just making everything global, which I don't want to be doing.
I think it would be reasonable for the multiplayer state to give the client a pointer to it's ent list, that way any state can work with the client aslong as it gives it an ent list pointer.[/QUOTE]
If I read this right, you could make it a container that's opened by the network code and invokes the event once for each entity.
Alternatively you could have the server send a content table to the client and then stream the whole thing.
[QUOTE=Tamschi;37727554]If I read this right, you could make it a container that's opened by the network code and invokes the event once for each entity.
Alternatively you could have the server send a content table to the client and then stream the whole thing.[/QUOTE]
The client has packet handlers, the packet handler deserializes the packet and calls a client event with that data read from the packet, that way any game state can register client event handlers and do whatever with that data. The problem is that it's a really shit solution for packets that contain lots of data (like a snapshot of every entity on the server) The multiplayer game state has to create the game entities so it has to get that data somehow without client knowing anything about the game state. It's a tricky thing to design elegantly.
It wouldn't be a problem if the game states could deserialize packets but i felt that it should be handled in client and not game states.
[QUOTE=Nigey Nige;37723648]I'm a total newb to data structures. I've only ever really used arrays and ArrayLists, and now I want a two-dimensional array that can grow and shrink dynamically in any direction, like this:
[IMG]http://i.imgur.com/77PCv.png[/IMG]
Is there a data structure like that?
[editline]asdf[/editline]
I'm gonna use an arraylist of arraylists for now, but let me know if there's a better way![/QUOTE]
Why not just have a 2D array that can expand/contract X or Y and set the unused parts to NULL?
Sorry, you need to Log In to post a reply to this thread.