Boonerm's picture

Loading a texture + loading a texture into a shader

hi

i having a problem loading a texture into my program. i ve searching for hours right now but no solution i found works.

i just want to load an image (.jpg is the only file were the exception "invalid parameter" doesnt appear) , parse it to ah shader.

in rendermonkey the shader works. therefore i dont thing the failure is in the shader.

here my code:

loading the texture (like in the tutorial and other posts)

testImage = new Bitmap("Textures/testImage.jpg");
            System.Drawing.Imaging.BitmapData testImage_data = testImage.LockBits(new Rectangle(0, 0, testImage.Width, testImage.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            testImage.UnlockBits(testImage_data);
 
            testImageTexture = GL.GenTexture();
            GL.ActiveTexture(TextureUnit.Texture2);
            GL.BindTexture(TextureTarget.Texture2D, testImageTexture);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, testImage_data.Width, testImage_data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, testImage_data.Scan0);
            #endregion

here is the code to put it in the shader:
GL.Uniform1(Shader.UniformLocation(shader_program, "Texture0"), testImageTexture);

anyone who can help me or finds the error will safe my day ;)

thanks


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture
GL.Uniform1(Shader.UniformLocation(shader_program, "Texture0"), testImageTexture);

This is not right. The correct approach is:

GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2d, testImageTexture);
GL.Uniform1(Shader.UniformLocation(shader_program, "Texture0"), 0);

In other words, you bind the texture to a sampler and pass the sampler id to the shader (instead of the texture id directly). Don't worry, everyone trips on this at first. :)

Boonerm's picture

argh damnit.

i feelt i have missed something.

better look twice xD or a power of twice i think. ^^

i have it the right way in the code allready a few lines obove this xD *arghs*

thanks for this.

i ve bind the txture to textureunit2. so i have to tell the sampler with
GL.Uniform1(Shader.UniformLocation(shader_program, "Texture0"), 2);
to use this unit for this sampler right?

but if i try the code the model is black.

the other questions is why the new Bitmap(); contruktor does only use .jpg?

iliak's picture

Look at my Blit method (http: code.google.com/p/arcadeengine/source/browse/trunk/Framework/Graphic/Texture.cs#340).
You are not limited to jpg, I use it with PNG files in my framework.

Hortus Longus's picture

Yes, System.Drawing.Bitmap can handle BMP, GIF, EXIG, JPG, PNG and TIFF. Because of your "invalid parameter"-exception in VS press F1 and type "types of bitmaps".

Boonerm's picture
iliak wrote:

Look at my Blit method (http: code.google.com/p/arcadeengine/source/browse/trunk/Framework/Graphic/Texture.cs#340).
You are not limited to jpg, I use it with PNG files in my framework.

thanks for teh tipp. i saw this in many other posts and lokked at it but i need it for my bachelor thesis. therefore i have to make it on my own ^^

Boonerm's picture

oh ok i missunderstood you - its just a method you mean. sry.

but i dont understand it anyway - the texture has a size of 512x512 px. i ve saved it with photoshop in .png, .tiff, .bmp (with paint) and .jpg and only .jpg doesnt throw an exception.

hortus:

thanks fpr the tipp. i was on this side allready. nothing new there to learn for me or something that solves my problem i think.

i try the solution of iliak's method. hopefully it will work...

Hortus Longus's picture
Boonerm wrote:

i ve saved it with photoshop in .png, .tiff, .bmp (with paint) and .jpg and only .jpg doesnt throw an exception.

You say it, you have found the source of the problem. ;-)
Yes, photoshop and paint can save, and mostly they can load this also, but another applications can have heavy problems with the format which is written.
Hint, hint: load your jpg-image with http://www.irfanview.com and save from irfanview the format of your choice, so other applications (and you) can load it without problems.

iliak's picture

Or use Paint .Net (www.getpaint.net)

Boonerm's picture

first of all thanks for the help.

and here is the solution thats works. the tipp gave me a friend.

            testImage = new Bitmap(Bitmap.FromFile("Textures/testImage.png"));
            System.Drawing.Imaging.BitmapData testImage_data = testImage.LockBits(new Rectangle(0, 0, testImage.Width, testImage.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            testImage.UnlockBits(testImage_data);
 
            testImageTexture = GL.GenTexture();
            GL.ActiveTexture(TextureUnit.Texture2);
            GL.BindTexture(TextureTarget.Texture2D, testImageTexture);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, testImage_data.Width, testImage_data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, testImage_data.Scan0);

Finaly it works with this line of code: testImage = new Bitmap(Bitmap.FromFile("Textures/testImage.png"));

The issue with the images was that i have forgotten to put them in the directory of the .exe xD *shame on me i know* ^^

than thanks again for the help.

Hortus Longus's picture

Better to found a mistake than to searching an error. :-))