• Create Account

# SelethD

Member Since 21 Oct 2003
Offline Last Active Apr 01 2016 09:26 PM

### How do I get a 3D position from a Camera Vector

01 April 2016 - 07:26 PM

Ok... I am quite the novice when it comes to game related math, especially in the 3D realm.  So I shall try to convey my problem as clearly as possible, since I do not know much of the terminology.

I have a Camera in 3D space, this camera can be located at any x,y,z and it can face any direction valid for a FPS view.

I have a target, the target can be at any location in 3D space along the x,y,z however the target will be STATIC and non movable.

I would like to be able to have my camera position itself in front of the target so that the target will appear a certain size and location on the 2D render screen.

I found online a function, when when supplied with variables of x,y (location of target to be displayed on 2D screen) and height (the height the target should display on the 2D screen) the function will return a Vector3 stating where the target should be placed in 3D space, so that it will render at the x,y at desired height on the 2D render screen.

The problem I am having is converting that Vector3 into something that I can use to position my Camera (since my target is stable) in the correct position to get the same result.

This is what I am doing so far... the code is in Lua

-- This line is calling the function I found online, I am requesting a Vector3 position so the target will be shown on the 2D render at position screenW/2, screenH/2 (so that should be in center of screen) and I want the object to appear to be .5 the size of my screen's height so thats the second screenH/2.  The last argument is the unit size of the object (2 units high)

local pos = ScreenSpace.ScreenToWorldByHeight(screenW/2,screenH/2,screenH/2, 2)

Now the result is ... pos = (0,0,-2)   -- Meaning i need to have the target positioned -2 away from my camera on the Z axis to display as needed.

This is the same result no matter which way my camera is facing, so I know the result is based off of my camera's .. facing vector?

So I tried this to get my new camera position...

newCamPos = TargetPosition - pos

This however, causes my camera to be positioned at the targets location offset by +2 on the Z axis, when in reality, I need it to be +2 on the Z axis in relation to how the Target is facing, the targets facing vector.

So I am lost... is there an easier way to do this?  What terms should I search for?  Or if you have any tips or tricks to share that would be great.

Thanks

### How do I load a custom material?

13 May 2015 - 07:37 AM

I am sure this is something that has been answered a million times over, but for the life of me, I cant seem to figure it out, nor can I find any helpful info.

I have a mesh (quad) and meshrenderer created in c#.  I would like to give the meshrenderer a material that already exists in my resources folder....   Assets/Resources/JmaMaterial

If I assign it to my meshrenderer like this....

Material BodyMat = new Material(mat);
mMeshRenderer.material = BodyMat;

This works, no problem... however, if I want to add 2 materials to my quad (one for character body, another for chatacter hair)

It doesnt seem to work, this is how im doing it...

mMeshRenderer.materials = new Material[2];
if (mat != null)
{
mBodyMat = new Material(mat);
mHairMat = new Material(mat);
mMeshRenderer.material[0] = mBodyMat;
mMeshRenderer.materials[1] = mHairMat;
}
else Debug.Log("failed to find material");

So... obviously im doing something wrong, I try to Google about assigning multiple materials to a mesh, but all the results either have no solution, or they explain how to do it in the editor, not in code.

Is this even the correct way to do an image with multiple parts?  (body + hair) Instead of having one quad with 2 materials, should I instead go with 2 mesh quads and each have 1 material?

Thanks for any help

24 January 2015 - 11:51 PM

I am working on a project, that uses some legacy graphics, that are in an 8bit indexed bitmaps.  These are the ones with a 1 byte pixel, pointing to an array of 256 colors.

So far, this is how I have been doing things.... (note, I'm using C#, with OpenTK, although a solution in C++ can be easily converted)

```Bitmap bitmap = new Bitmap(path);
System.Drawing.Imaging.BitmapData textureData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

int srcmax = textureData.Height * textureData.Stride;
byte[] origBytes = new Byte[srcmax];
Marshal.Copy(textureData.Scan0, origBytes, 0, srcmax);

bitmap.UnlockBits(textureData);
```

this gives me the bitmap pixel data (the 1 byte index numbers) in textureData.

So now, I very slowly and very painfully, go through the data, as such....

```                        //loop through all the pixels
byte[] data = new byte[(bitmap.Width * bitmap.Height) * 4];

int srcloc = 0;
int dstloc = 0;
byte index;
Color color;
for (int y = 0; y < textureData.Height; y++)
{
srcloc = y * textureData.Stride;
for (int x = 0; x < textureData.Width; x++)
{
index = origBytes[srcloc];
if (index == 0)
{
data[dstloc] = 0;
data[dstloc + 1] = 0;
data[dstloc + 2] = 0;
data[dstloc + 3] = 0;
}
else
{
color = bitmap.Palette.Entries[index];
data[dstloc] = color.R;
data[dstloc + 1] = color.G;
data[dstloc + 2] = color.B;
data[dstloc + 3] = color.A;
}
dstloc += 4;
srcloc++;
}
}
```

as you can see, I am looking at the color, pointed to, by each pixel index... and manually reversing the red, and blue values, and also, setting alpha to 0, when the index number is 0.  This converted data is then used to create an opengl texture, as follows...

```                GL.Enable(EnableCap.Texture2D);
uint textureID = 0;
GL.GenTextures(1, out textureID);

GL.BindTexture(TextureTarget.Texture2D, ret.ID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0,
PixelFormat.Rgba, PixelType.UnsignedByte, pixelData);

GL.Disable(EnableCap.Texture2D);

```

Now... this totally works, I end up with a rgba opengl texture2d, with transparency that can be used to texture a quad  and it looks perfect.... only problem is the super slow speed of going through each pixel, as some of these images are > 800x800

So... If anyone knows of a better solution, something faster, or if you know of anything I seem to be doing wrong (opengl wise, as I am an opengl noob) please let me know.

Thanks

### How 'in terms of structure' do I use my shader?

05 December 2014 - 10:06 AM

Sorry if I am unsure of the proper terms to use,  this is all fairly new to me.

I am working on a game, that uses 8bit indexed graphics (for palette shifting/swapping effects).

After about a month of searching tutorials, forums, etc... I am at the point, where I am doing this.....

Each 'character' has a reference to a sprite (object containing a Texture2D) which holds my character image (using the red value of rgb to be an index to the color palette per pixel)

And each 'character' has a local Texture2D for holding that 'character's unique color palette (since different players might color their character's skin, hair, eyes , etc... a different color)

So, each character has to pass its 'image' and 'palette' to the shader.... this is how I am doing it.

Every time I draw a 'character' I call this member function

```         public void RenderAt(float posx, float posy, DuShader shader)
{
GL.ActiveTexture(TextureUnit.Texture0);
mCurrentFrame.Bind();

GL.ActiveTexture(TextureUnit.Texture1);
mPalette.Bind();
GL.ActiveTexture(TextureUnit.Texture0);

mCurrentFrame.RenderNoBindAt(posx, posy);

}

```

My issue with this is that I am having to call shader.Begin, and shader.End per draw.  I am no expert at openGL, but it just screams at me as 'bad code'

My original thought , was to have the shader.Begin located at the beginning of the draw loop, then have all my Render calls happen, then at the end call shader.End

This however, did not work,  for some reason, I cannot execute the code....

GL.ActiveTexture(TextureUnit.Texture0);
mCurrentFrame.Bind();

from between the shader.Begin and shader.End calls, it only works when the code is executed 'before' the shader.Begin

I have no idea why this is the case.   My understanding of shaders and howto use them is from examining about 30+ articles and tutorials over a period of about 4 weeks

So... finally my question... what is the 'best' meaning most correct or efficient way to have multiple draw calls to a shader, where the two textures (image and palette) are never the same, and not have to begin and end my shader for each one.

```        public void Begin()
{
GL.UseProgram(mProgram);
}
public void End()
{
GL.UseProgram(0);
}
```

And the actual GLSL fragment shader...

```uniform sampler2D texture;
uniform sampler2D colorTable;

void main()
{
vec2 pos = gl_TexCoord[0].xy;
vec4 color = texture2D(texture, pos);
vec2 palPos = vec2(color.r,0);
vec4 newColor = texture2D(colorTable, palPos);
newColor.a = color.a;
gl_FragColor = newColor;
}
```

The output i get is correct, meaning it 'works' Ijust need to know if im using it correctly or a better way of doing this.

Thanks in advance, I know its a long winded question, and I tend to ramble

### What Language or Languages for Web and Mobile Apps

08 December 2013 - 11:18 AM

If this is not the correct forum, my apologies.  I thought, since I am a beginner to web or mobile games, this would be the best place to ask the quesiton.

I have been programming 'forever' in C++ and recently in C# with a sprinkle of Java thrown in for flavor.  However, all my games or apps are the standard .exe, run this in windows, variety.

Recently though, I have been trying to learn what language or languages I need to know to make games on a mobile device or that can be played from a web site.

I know the Apple market is fairly proprietary, and thus not owning a Mac, I dont think I have any possibility for developing anything for the IOS market.

The Android market, has so many different devices I'm confused of which one I should target (or if I even need to target anything specific) and definately what is the best Game Creation language to program it in.

I would love for my little games to not only be played on a mobile device, but also be playable from a web browser.  So I'm not sure if I could use the same language, or if the mobile device, and the web based, would need different languages for the program to be in.

My apologies if this is an 'over asked' question.  I promise you I have googled myself silly over the last few weeks trying to find answers.  I seriously just have lousy luck searching for answers.

BTW, I am only able to code from a windows platform.

Thanks in advance for any advice, or links you can provide to help me make the transition into the web / mobile device gaming arena.

PARTNERS