Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Xeom
Mar 16, 2007
I've started to experiment a little with opengl for some 2D rendering. I've been coding my own math functions and such because hey its a fun hobby.
I got a little scene with some quads going, but I've run into a problem I can't seem to figure out.

I decided to use different Z depths to control what quad goes in front of the other, but they begin to shrink as they go away from the camera even though I'm using an orthographic matrix, which as far as I understand means that distance should not affect size. Yet they do shrink, and their location in relation to the x, y axis also changes. Its almost as if everything is being scaled towards the origin. Everything seems to work fine, until I play with the Z axis.

I do all my scaling and rotation in a 2x2 matrix, and then "promote" that matrix into a 4x4 matrix. Meaning I just copy the 2x2 values into the 4x4 identity matrix. Then I multiply that matrix by a 4x4 translation matrix. All my functions seem correct, and I'm following the second edition of 3D math for Graphics and games development for the math. Left handed convention and row ordered.

Here is some of the pertinent code.
https://pastebin.com/2CuKTUwW

Adbot
ADBOT LOVES YOU

Xeom
Mar 16, 2007

Xerophyte posted:

Remember that opengl matrices are column-major. Your ortho matrix sets w = n2 * p.z + 1 if using column vector math, which means you will do a sort of perspective division.

AAaaahhh!! I remember telling myself this before implementing it and then I totally forgot. I remember even having to convince myself that row-major would actually work for everything else. Now it all looks weird to me and I'll have to convince myself again.

Thanks.

Xeom
Mar 16, 2007
So my question is for functions like "glUniformMatrix4fv" that can be placed into row major mode what does that mean inside GLSL?
Currently everything seems to be working right, but I really feel like I'm missing some key element because all my math seems to mostly be in row major form yet I had to make that change yesterday. I am doing the math inside glsl with row major form meaning things read left to right rather than right to left.

Why is it working?

I should just switch to column major mode as ugly as it is to my eyes.

Xeom
Mar 16, 2007
Now I'm even more confused because the book I'm using claims that matrix is already in row major form. The 4th row being in the form {dx, dy, dz, 1}. In fact the other matrix being in row form shouldn't have mattered either because my uniform call is set with GL_TRUE.

code:
	glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_TRUE, matrix.n);

Xeom
Mar 16, 2007
When multiplying a vec4(row) by a mat4 I don't see how your row matrix would lead to translation. Your w term in your vector would have all the translation information.

https://imgur.com/Y5ITil8

Seems to make sense to me, but clearly I'm missing something.

EDIT: written towards Suspicious Dish reading Xeros post now.

Xeom
Mar 16, 2007
I did understand the difference between row and column vectors, but I totally forgot that GLSL was doing the math assuming a column vector. Currently my shader is setup as if GLSL did row vector multiplication. Somehow it all worked out because everything seems fine in my test program. I'll have to figure out exactly WHY it worked at a latter time. At least I can go about fixing everything now. Bugs and math can be really weird sometimes.

Xeom
Mar 16, 2007
:goofy:

Xeom
Mar 16, 2007
After those interesting posts I'm going to post a beginner question that is completely boring.

I'm making a font texture atlas using Freetype2. Everything seems to be working well and I can get a png out with the exact results I want, but something goes completely wrong when I try to load it into opengl. The best way I can describe it is that the texture becomes skewed and compressed. Funnily enough I can load the png I saved into that same quad with the same VAO and shader and it looks completely fine.

code:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, mem);
Is there something special I should be doing when working with a single byte per pixel image?
I tried vec4(1,1,1,texture(blah,blah).r), but it doesn't seem to help.

Xeom
Mar 16, 2007

haveblue posted:

Skewed image usually means the row length is wrong, are you calling glPixelStore?

Thank you for the help with the stupid questions.
Everything looks good, just gotta flip it now.

Adbot
ADBOT LOVES YOU

Xeom
Mar 16, 2007
I'm rendering font, but it seems to be taking a long time to render even with a texture atlas. Printing the string "The quick fox jumped over the brown fence" is taking about 0.1 to 0.05 milliseconds, which seems like a long time for this sort of things. Right now I'm using a texture atlas and using glBufferSubData to update the texture coordinates for each character printed. I'm also using glUniform to provide updates to a projection,view, and model matrix. Only the model matrix gets updated per character. I'm guessing updating the texture coordinates is what is taking so long, but I'm not quite sure what to do. Should I just build a VAO for each character and just switch between those?

I originally switched to the texture atlas to avoid switching between textures, but I guess updating a VBO is worse.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply