PDA

View Full Version : Loading Texture - Taoframework.



intenseza
05-08-2013, 05:11 AM
Hey all. I've been searching for hours on how to load an image and apply a texture to my particles with no resolve.
The following is my code which runs, but nothing appears on the screen. When I remove the texture code, the particles
do what they're supposed to do, but they're still simple GlPoints...
Any pointers will be appreciated.


private static int[] index;
private static int width;
private static int height;

/// <summary>
/// TEXTURE CODE
/// </summary>


[EnvironmentPermissionAttribute(SecurityAction.Link Demand, Unrestricted = true)]//For codeanalyst
public static void Texture()
{
index = new int[1];
Gl.glGenTextures(1, index);
//LoadTexture(path);
}

[EnvironmentPermissionAttribute(SecurityAction.Link Demand, Unrestricted = true)]//For codeanalyst
public static void LoadTexture(string path)
{
Bitmap image;

try
{
image = new Bitmap(path);

image.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
height = image.Height;
width = image.Width;
BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

Gl.glEnable(Gl.GL_TEXTURE_2D); // Enable 2D Texture Mapping
// Gl.glDisable(Gl.GL_DEPTH_TEST); // Disable Depth Testing
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE); // Set Blending Mode
Gl.glEnable(Gl.GL_BLEND);
Gl.glEnable(Gl.GL_TEXTURE_GEN_S); //enable texture coordinate generation
Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, image.Width, image.Height,
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
image.UnlockBits(bitmapdata);
image.Dispose();
}
catch (ArgumentException)
{
Environment.Exit(0);
}
}

public static void Activate()
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
}

public int GetWidth
{
get { return width; }
}

public int GetHeight
{
get { return height; }
}

/////END OF TEXTURE CODE


public static void DisplayRainSystem()
{

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Texture();
LoadTexture("particle.bmp");
Activate();

float spost = 0.0f; // Starting Texture Coordinate Offset
float alphainc = 0.9f / 2; // Fade Speed For Alpha Blending
float alpha = 0.2f; // Starting Alpha Value

Gl.glPointSize(2.5f);

int RandomColour = R.Next(4) + 1;
if (RandomColour == 1) Gl.glColor3f(0.0f, 0.0f, 1.0f);
if (RandomColour == 2) Gl.glColor3f(0.0f, 0.1f, 1.0f);
if (RandomColour == 3) Gl.glColor3f(0.0f, 0.2f, 1.0f);
if (RandomColour == 4) Gl.glColor3f(0.0f, 0.5f, 1.0f);
if (RandomColour == 5) Gl.glColor3f(0.0f, 0.8f, 1.0f);

Gl.glBegin(Gl.GL_POINTS);

for (int i = 0; i <= RS.RainParticles.Count - 1; i++)
{
RainParticle RP = (RainParticle)RS.RainParticles[i];
Gl.glVertex2d(RP.xpos, RP.ypos);
}

Gl.glFlush();
Gl.glEnd();

}

intenseza
05-08-2013, 05:21 AM
Might I add, this is in C#/Taoframework, so frustrating.

intenseza
05-08-2013, 07:45 AM
bump? anyone?

mobeen
05-08-2013, 05:01 PM
I suggest u remove these calls


Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);
// Set Blending Mode
Gl.glEnable(Gl.GL_BLEND);
Gl.glEnable(Gl.GL_TEXTURE_GEN_S);
//enable texture coordinate generation
Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

Are you setting any other OpenGL states?

intenseza
05-09-2013, 05:14 AM
I suggest u remove these calls


Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);
// Set Blending Mode
Gl.glEnable(Gl.GL_BLEND);
Gl.glEnable(Gl.GL_TEXTURE_GEN_S);
//enable texture coordinate generation
Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

Are you setting any other OpenGL states?

I've tried this yet the screen doesn't display anything.
I've even tried drawing2d pictures instead, still no success.


private static int[] index;
private static int width;
private static int height;

/// <summary>
/// TEXTURE CODE
/// </summary>


[EnvironmentPermissionAttribute(SecurityAction.Link Demand, Unrestricted = true)]//For codeanalyst
public static void Texture()
{
index = new int[1];
Gl.glGenTextures(1, index);
//LoadTexture(path);
}

[EnvironmentPermissionAttribute(SecurityAction.Link Demand, Unrestricted = true)]//For codeanalyst
public static void LoadTexture(string path)
{
Bitmap image;

try
{
image = new Bitmap(path);

image.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
height = image.Height;
width = image.Width;
BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

Gl.glEnable(Gl.GL_TEXTURE_2D); // Enable 2D Texture Mapping
// Gl.glDisable(Gl.GL_DEPTH_TEST); // Disable Depth Testing
// Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE); // Set Blending Mode
// Gl.glEnable(Gl.GL_BLEND);
// Gl.glEnable(Gl.GL_TEXTURE_GEN_S); //enable texture coordinate generation
// Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, image.Width, image.Height,
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
image.UnlockBits(bitmapdata);
image.Dispose();
}
catch (ArgumentException)
{
Environment.Exit(0);
}
}


public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}


public static void Activate()
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
}

public int GetWidth
{
get { return width; }
}

public int GetHeight
{
get { return height; }
}

/////END OF TEXTURE CODE


public static void DisplayRainSystem()
{

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);


float spost = 0.0f; // Starting Texture Coordinate Offset
float alphainc = 0.9f / 2; // Fade Speed For Alpha Blending
float alpha = 0.2f; // Starting Alpha Value

Gl.glPointSize(2.5f);

int RandomColour = R.Next(4) + 1;
if (RandomColour == 1) Gl.glColor3f(0.0f, 0.0f, 1.0f);
if (RandomColour == 2) Gl.glColor3f(0.0f, 0.1f, 1.0f);
if (RandomColour == 3) Gl.glColor3f(0.0f, 0.2f, 1.0f);
if (RandomColour == 4) Gl.glColor3f(0.0f, 0.5f, 1.0f);
if (RandomColour == 5) Gl.glColor3f(0.0f, 0.8f, 1.0f);

// Texture();
// LoadTexture("particle.bmp");
// Activate();

System.Drawing.Image tempImage;
tempImage = System.Drawing.Image.FromFile("particle.bmp");

byte[] imgbyte = ImageToByte(tempImage);

Gl.glBegin(Gl.GL_POINTS);

for (int i = 0; i <= RS.RainParticles.Count - 1; i++)
{
RainParticle RP = (RainParticle)RS.RainParticles[i];
// Gl.glVertex2d(RP.xpos, RP.ypos);
// Gl.glTexCoord2d(RP.xpos, RP.ypos);

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 512, 512, 0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, imgbyte);


}

Gl.glFlush();
Gl.glEnd();

}

mobeen
05-09-2013, 06:04 AM
I've even tried drawing2d pictures instead, still no success.

This is not how u draw pictures. You are contiuously allocating texture for each rain particle.

What I suggest u do first is render a simple full screen quad with the texture on it.
Try something like this in ur render function (this is in c++) you would know how to convert to c#.


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFERBIT|GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
glTexCoord2f(0,0);glVertex3f(-1,-1);
glTexCoord2f(1,0);glVertex3f(1,-1);
glTexCoord2f(1,1);glVertex3f(1,1);
glTexCoord2f(0,1);glVertex3f(-1,1);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
SwapBuffers();


Note that i havent tested this code but this should work.
See if the texture appears on a full screen window using this code.

intenseza
05-09-2013, 06:30 AM
Good news, the quad is textured nicely, but how do I apply the textures to Gl.Points that are nothing more than of GlPoint size 1.5?

EDIT: Got it! Thank you so much :)

mobeen
05-14-2013, 12:54 AM
You would have to distribute the texture coordinates between the points based on their position.