Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Member Since 14 Oct 2007
Offline Last Active Apr 02 2014 10:48 AM

Topics I've Started

Inconsistency between XNA/DirectX and OpenGL rendering

29 March 2014 - 05:42 AM

Hi all,


I have some drawing code that is expected to work on "all systems" ... it's working okay on DirectX/XNA, but in OpenGL, I get graphical glitches.


Here's a screenshot:



(Top text/image in each picture is filtering on, bottom tex/image in each picture is filtering off)


Some explanation (please read before you answer that I need to offset my pixels/texels by .5 in openGL):


1. Everything you see there, including the font, are just images combined onto a texture atlas

2. The texture atlas DOES have a white, zero alpha background-- I have no idea where the darker color is blending in from.

3. There IS space around all images on the texture atlas-- they are not grabbing pixels from another, adjacent image

4. I have tried many versions of "add .5 to pixels, add .5 to texels" to try to get the OpenGL version working.  I can make it look worse, but I can't make it look better.


My default render settings are these:


I am suspecting the GL_ONE_MINUS_SRC_ALPHA, because I read that that's supposed to be for premultiplied alpha.  I do not have premultiplied alpha in the images-- but what's the fix?  I tried every blend mode that didn't sound crazy, and I get either white boxes for everything, or the images you see here.


Please help!



Texture Matrix in DirectX vs OpenGL

11 March 2014 - 06:07 AM

Hi all,


I have some code that has to be implemented in both DirectX and OpenGL ES 1.0 (meaning no shaders).


What I'm doing is setting the texture matrix.  In DirectX via:


And in OpenGL via:


I'm using this code right now only to TRANSFORM the texture matrix-- basically translating small fractions to make the texture scroll.


In DirectX I found that I have to put the translation numbers in a 3x3 matrix rather than a 4x4 matrix.  I.E. the x/y transform coordinates have to go in matrix m20, m21 rather than what I use for all other transforms (m30,m31).


Is this also true in OpenGL?  Or will OpenGL accept the texture coordinate transforms in m30,m31?





Same model, different UV's

10 March 2014 - 04:44 PM

Hi all,


I realize this might be an impossible task, but then again, it might be a possible one!


First, I am locked to OpenGLES 1.0 for technical reasons at my company.  I realize doing it with a shader would be simple, but unless I want to announce that I'm going to be rewriting the whole core graphical section of the company framework (for which I will get fired), I have to do it some other way.


So here's the issue:


We have a model, all UV'd.


We have some skins of the model baked onto a texture atlas.


At load time, the model gets its UVs adjusted to fit the texture on the atlas so it draws well.  Like, we literally have a function that takes UV's in the range of 0-1 and adjusts them to the texture's position on the atlas.


Here's my problem:


I want a way to, on the fly, move those UV's over to another place on the atlas.  Because the skins that are backed on the atlas are all the same size, it would be sufficient to simply transform the UV's-- they don't need to be resized or recomputer.


But, because UV's are baked to the XYZ position (a featured designed by a madman), I would literally be stuck either making a whole new model with the UV coordinates, or changing the UV's on the fly.


Because this routine needs to be used to populate a screen that could have a lot of versions of the same object with different skins, both of those objects are prohibitive-- I'd need about a hundred models at worst, or I'd need to manually change the UV's a hundred times.


Since the target is mobile, no good!


So: does there exist some sort of call you can make to translate the texture coordinates before drawing a primitive?  Sort of the way you'd set the world matrix to move your model, except for texture coordinates?  Answer accepted for either DirectX or OpenGL.










Casting a picking ray from mouse with a unique camera setup

20 February 2014 - 12:10 PM

Hi all,


I've found a lot of algos online that tell how to cast a ray directly into the screen from the mouse position.  My problem is, I am dealing with code that has a legacy camera setup, and the setup is a little bit unique.  As a result, I'm able to get the DIRECTION of the picking ray accurately, but I'm not able to get a good origin point.


Here is the code I'm using to generate the picking ray (mPMatrix and mVMatrix are stored copies of the projection and view matrix.  The world matrix is identity, I haven't even gotten that far yet).

void Game::MouseToRay(float theX, float theY)

	Matrix aProjection=mPMatrix;

	Vector aV;

	Matrix aView=mVMatrix;

	Vector aRayOrigin;
	Vector aRayDir;
	aRayDir.mX  = aV.mX*aView.mData._11 + aV.mY*aView.mData._21 + aV.mZ*aView.mData._31;
	aRayDir.mY  = aV.mX*aView.mData._12 + aV.mY*aView.mData._22 + aV.mZ*aView.mData._32;
	aRayDir.mZ  = aV.mX*aView.mData._13 + aV.mY*aView.mData._23 + aV.mZ*aView.mData._33;
	aRayOrigin.mX = aView.mData._41;
	aRayOrigin.mY = aView.mData._42;
	aRayOrigin.mZ = aView.mData._43;


This code produces a 100% accurate direction, but it produces an origin that gets less and less accurate the further the mouse is from pointing right at 0,0,0.  Aspect ratio also seems to be an issue, in that the inaccuracy seems to be different along the y axis.  I am not sure what is missing.


Now, as I said, the camera code I'm using is unorthodox, but I don't have the option to not use it.  I already had a lot of problems with the camera code and setting fog values (I had to manually add a tweak), so I expect the secret is in there.  Here is what the camera setup code for the scene looks like:

void Set3DCamera(float theCameraX, float theCameraY, float theCameraZ,
	float theLookatX, float theLookatY, float theLookatZ,
	float theUpVectorX, float theUpVectorY, float theUpVectorZ, float theFOV)
	float aWorldMatrix[4][4];
	float aViewMatrix[4][4];

	theCameraZ = -theCameraZ;
	theLookatZ = -theLookatZ;
	theUpVectorZ = -theUpVectorZ;

	// We translate the world the opposite direction of the camera (relatively speakng, of course)

	// Look-at matrix vectors
	float aLookatVectorX = theLookatX-theCameraX;
	float aLookatVectorY = theLookatY-theCameraY;
	float aLookatVectorZ = theLookatZ-theCameraZ;

	// Side vector (UP cross LOOKAT)
	float aSideVectorX = theUpVectorY * aLookatVectorZ - theUpVectorZ * aLookatVectorY;
	float aSideVectorY = theUpVectorZ * aLookatVectorX - theUpVectorX * aLookatVectorZ;
	float aSideVectorZ = theUpVectorX * aLookatVectorY - theUpVectorY * aLookatVectorX;

	// Correct the UP vector (LOOKAT cross SIDE)
	theUpVectorX = aLookatVectorY * aSideVectorZ - aLookatVectorZ * aSideVectorY;
	theUpVectorY = aLookatVectorZ * aSideVectorX - aLookatVectorX * aSideVectorZ;
	theUpVectorZ = aLookatVectorX * aSideVectorY - aLookatVectorY * aSideVectorX;

	// Normalize the lookat vector
	float len = (float)sqrt(aLookatVectorX*aLookatVectorX + aLookatVectorY*aLookatVectorY + aLookatVectorZ*aLookatVectorZ);
	aLookatVectorX /= len;
	aLookatVectorY /= len;
	aLookatVectorZ /= len;

	// Normalize side vector
	len = (float)sqrt(aSideVectorX*aSideVectorX + aSideVectorY*aSideVectorY + aSideVectorZ*aSideVectorZ);
	aSideVectorX /= len;
	aSideVectorY /= len;
	aSideVectorZ /= len;

	// Normalize the up vector
	len = (float)sqrt(theUpVectorX*theUpVectorX + theUpVectorY*theUpVectorY + theUpVectorZ*theUpVectorZ);
	theUpVectorX /= len;
	theUpVectorY /= len;
	theUpVectorZ /= len;

	// The view matrix (look-at)




	// Combine the world and view (GL doesn't support View matrices)

	// Perspective projection matrix (as per Blinn)

	float aAspect = (float)gPageWidth/(float)gPageHeight;
	float aNear = gZNear;	// This was 1.0... is it hurting anything?  I made it 0.5f so things could be closer to the camera without 
	float aFar = GetZDepth();

	float aWidth=COS(theFOV / 2.0f);
	float aHeight=COS(theFOV / 2.0f);
	if (aAspect > 1.0) aWidth /= aAspect;
	else aHeight *= aAspect;


	float s  = SIN(theFOV / 2.0f);
	float d  = 1.0f - aNear/aFar;

	float aMatrix[4][4];
	aMatrix[3][2]=-(s * aNear / d);



See that line gFogTweak near the end?  I had to add that line to make fog work correctly-- it was the exact same situation I'm dealing with here, where there were lots of answers on the web, but none of them worked correctly.  I believe that something about how that projection matrix is set up is what's throwing off my picking ray. 


I'm only middling-strong on math and I've hit a wall.  And this point all I am able to do is try random things in the hopes that one of them will work, but none of them will.  If anyone can see the problem, or what I need to add into that MouseToRay function to make this all line up, you will have my infinite gratitude, and a "special thanks" line in the credits of the game I'm working on!

Flattening some polygons into an arbitrary plane

05 February 2014 - 07:56 AM

Hi all,


For a visual effect, I want to flatten the XYZ coordinates of an arbitrary triangle into a 2D plane where I can then convert those to UV coordinates while maintaining a specific size/aspect ratio for the triangle.


So for instance, I have a triangle in an arbitrary plane, but I want to convert it so that it rests in plane z=0 while maintaining the dimensions of the polygon. 


Is there a quick and easy way to do this?  I can think of all kinds of weird methods, like picking one point on the triangle and then using distance between the points to extrapolate the same triangle with z=0, but if there's a smarter way to do it, I'd like to know!