Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


SelethD

Member Since 21 Oct 2003
Offline Last Active May 20 2015 07:55 AM

Topics I've Started

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

 

I load the material with...  Material mat = Resources.Load<Material>("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];
        Material mat = Resources.Load<Material>("JmaMaterial");
        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 


Trouble loading and displaying with transparency, 8bit indexed bitmaps

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.ImageLockMode.ReadOnly,
                           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();
            shader.SetVariable("texture", (int)0);

            GL.ActiveTexture(TextureUnit.Texture1);
            mPalette.Bind();
            shader.SetVariable("colorTable", (int)1);
            GL.ActiveTexture(TextureUnit.Texture0);

            shader.Begin();
            mCurrentFrame.RenderNoBindAt(posx, posy);
            shader.End();

        }

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();
shader.SetVariable("texture", (int)0);

 

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 :P

 

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.

 

Here is my shader.begin and shader.end code...

        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 :P

 


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.


How do I import Vector2D

27 January 2013 - 10:02 PM

I need to do some vector operations on a 2d Asteroids type game.

 

I found this page here.....

 

http://geom-java.sourceforge.net/api/math/geom2d/Vector2D.html

 

Unfortunately, I have no idea what to 'import' to get this to work.

 

I saw in an online example someone used the following....

 

import math.geom2d.Vector2D;

 

when I tried this, I only got....

 

error: package math.geom2d does not exist
import math.geom2d.Vector2D;

 

I am totally new to Java, and im not sure what I am doing wrong, other than there might be something I need to download extra?

And if so... how will others run my java app or applet if they too dont have the 'something' i might be missing?

 

Thanks for any help

 


PARTNERS