PDA

View Full Version : Matrix Multiplication (Open GL behaviour problem)



blessman11
03-18-2011, 05:06 AM
Why does Open GL draw my object on the origin the first time round when I place my object?
Additional info:

•I have an object which is written/ initialised on the origin? (x = 0, y=0)
•However I call a matrix transformation just after its in draw(); E.G.: draw_shape(); glMatrixLoad();......
•Afterwards the following objects are drawn accordingly but I'm annoyed at the first object which ruins the whole function.


for (int letter = 0;letter != strlen(s);letter++)

{



//draw font in array

qva_Fonts[((int)*s)- 32]-> draw();

s++;



//loadmatrix Position & Scalling


gfaaSca[0] = gfFont_Size;

gfaaSca[5] = gfFont_Size;

gfaaSca[10] = gfFont_Size;

gfaaSca[15] = gfFont_Size;


gfText_PosX += gfFont_Spacing;

gfaaPos[12] = gfText_PosX;

gfaaPos[13] = gfText_PosY;


//multiply matrix



glMultMatrixf(gfaaSca);

glMultMatrixf(gfaaPos);

glLoadMatrixf(gfaaSca); glLoadMatrixf(gfaaPos);

}


glLoadIdentity();

}

Alfonse Reinheart
03-18-2011, 05:12 AM
However I call a matrix transformation just after its in draw(); E.G.: draw_shape(); glMatrixLoad();......

All OpenGL rendering functions use the state of OpenGL at the time the rendering command was issued. Calling glLoadMatrix after rendering will not affect the rendering of the stuff you called before glLoadMatrix.

So if you want some matrix functions to affect the rendering of something, call them before rendering that something.

blessman11
03-18-2011, 12:20 PM
so basically, its just loading the matrix before multiplying. By the way would it be possible to load it when there is nothing being draw.

Or would it be a case of drawing then glSwapBuffers();(for clearing the default drawing)at which afterwards you'll redraw the same at the desire location?

mhagain
03-18-2011, 12:32 PM
I'm not entirely sure what you're trying to accomplish here, but you seem to have misunderstood how matrixes work. The correct order of operation for each object should be: Set up matrix. Load matrix. Draw stuff.In your case, as well as getting the order completely backwards, you're calling glLoadMatrix twice - are you aware that the second glLoadMatrix (for gfaaPos) will overwrite the first (for gfaaSca)?

A SwapBuffers call has nothing to do with clearing stuff; it exchanges the front and back buffers in a double buffered context, and should only be called once per frame and at the end of the frame. glClear is for clearing, and thinking of using SwapBuffers to do it is - to put it bluntly and with no offense intended - a kind of cargo-cult hacking and slashing at code in the desperate hope that something will work.

I think that you're trying to jump too far ahead of your current level of ability to be honest. You need to step back a little and focus on how transforms actually work, what the correct order of operations actually is, and correct your current misunderstandings first.

blessman11
03-18-2011, 01:14 PM
Oh is it.

All I just wanted to do was to scale my matrix then reposition it (I thought I needed two matrices for it).But, how would I go about doing this?