Why my tiles became small? - c#

I'm currently doing a game that I will present for my software engineering subject in school. To be truth, I am not experienced in C# that's why I'm following this TileEngine tutorial. But when I'm in the part of adding the tiles to the game, my tile are rendered so small. I even used 256 x 256 image. Just like in the tutorial video.
You can see a screenshot of the problem here. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TileEngine
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Texture2D> tileTextures = new List<Texture2D>();
int[,] tileMap = new int[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
};
int tileWidth = 64;
int tileHeight = 64;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture;
texture = Content.Load<Texture2D>("Tiles/se_free_dirt_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_grass_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_ground_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_mud_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_road_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_rock_texture");
tileTextures.Add(texture);
texture = Content.Load<Texture2D>("Tiles/se_free_wood_texture");
tileTextures.Add(texture);
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);
for (int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int textureIndex = tileMap[y, x];
Texture2D texture = tileTextures[textureIndex];
spriteBatch.Draw(
texture,
new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
Color.White);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}

You just need to replace 3rd line in below block:
spriteBatch.Draw(
texture,
new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
Color.White);
so that it is like 3rd line in next block:
spriteBatch.Draw(
texture,
new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight),
Color.White);
So problem was: in your code texture's rectangle width and height was set to your map's width and height - which appears to be much smaller than size of your texture's image.

Change the width and height of each tile to make it bigger:
int tileWidth = 300;
int tileHeight = 300;
Basically add whatever number produces the size you please.
Also the rectangle is formed by adding start point + size so the size needs to match the points as well otherwise they will be drawn on top of each other.
If you want to render it in squares for example then titleWidth should be the same as tileMapWidth.
Currently it looks like it is the length of the matrix which is certainly wrong because that number is small (10) and would render small squares (10x10).
Therefore do this:
int tileMapWidth = tileWidth;
int tileMapHeight = tileHeight;
Instead of
int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);

you can try like this
width = 64 ' or texture.width
height = 64 ' or texture.height
pos = new vector2d(x * width , y * height)
spriteBatch.Draw(texture, pos, Color.White);
if x starts from 0 then first texture will be at 0,0 second at 64,0 ....

Related

Change image color with transparent background

I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).
That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.
I can't use fixed yellow circle image because the desired color is dynamic.
So in the code below, how can I change the color of the image before adding it to the bitmap?
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//--> Here I need to change the color of the green circle to yellow
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.
Fixed:
Thanks to #TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//Here I need to change the color of the green circle to yellow
i = ChangeToColor(b, Color.Gold)
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
While ChangeToColor function is as follows:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
This will create a new Bitmap with all non-transparent pixels moved strongly toward a new color:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1} // kudos to OP!
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
do make sure not to leak the Bitmaps you create!
Note that there are other methods as well. Here is a link to a method that uses ColorMapping. This allows for a range of colors to be replaced by another range, so it can keep gradients like the ones you get in anti-alised graphics..
Here's my solution you just need to create a new Control
then inherit the Picturebox check this out.
public partial class UICirclePicture : PictureBox
{
[Browsable(false)]
public int Depth { get; set; }
[Browsable(false)]
public SprikiwikiUI Ui
{
get { return SprikiwikiUI.Instance; }
}
[Browsable(false)]
public MouseState MouseState { get; set; }
public UICirclePicture()
{
BackColor = Ui.GetApplicationBackgroundColor();
SizeMode = PictureBoxSizeMode.StretchImage;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
using (var gp = new GraphicsPath())
{
gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
this.Region = new Region(gp);
}
}
}

OpenGL doesn't render to Framebuffer but to Window

My problem is that OpenGL renders to the Main-Window, although I bound the Framebuffer I want to use.
This is my Main Rendering-Method
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
renderer.BeginFrame();
renderer.RenderEntity(testEntity);
renderer.EndFrame();
SwapBuffers();
}
This is my Renderer
class Renderer
{
List<Vertex> screenQuadVertecies = new List<Vertex>
{
new Vertex(new Vector3(-1, 1, 0), new Vector3()),
new Vertex(new Vector3(1, 1, 0), new Vector3()),
new Vertex(new Vector3(-1, -1, 0), new Vector3()),
new Vertex(new Vector3(1, -1, 0), new Vector3())
};
List<int> screenQuadIndices = new List<int>
{
0, 1, 2,
1, 2, 3
};
List<Vector2> screenQuadUVs = new List<Vector2>
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
TexturedMesh screenQuad;
Framebuffer mainPassFramebuffer;
Camera activeCamera;
Shader ModelShader;
Shader PostProcessingShader;
int width, height;
public Renderer(int width, int height)
{
this.width = width;
this.height = height;
ModelShader = new MainShader();
PostProcessingShader = new PostProcessingShader();
mainPassFramebuffer = new Framebuffer(width, height);
screenQuad = new TexturedMesh(screenQuadVertecies, screenQuadIndices, screenQuadUVs);
}
public void BeginFrame()
{
mainPassFramebuffer.EndRendering();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
mainPassFramebuffer.ClearBuffer();
mainPassFramebuffer.BeginRendering();
}
public void EndFrame()
{
mainPassFramebuffer.EndRendering();
mainPassFramebuffer.BindTexture();
PostProcessingShader.UseShader();
screenQuad.PrepareRendering();
GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0);
screenQuad.EndRendering();
PostProcessingShader.UnuseShader();
}
public void RenderEntity(Entity e)
{
e.Mesh.PrepareRendering();
ModelShader.UseShader();
ModelShader.LoadCamera(activeCamera);
ModelShader.LoadModel(e.GetModelMatrix());
GL.DrawElements(PrimitiveType.Triangles, e.Mesh.GetSize(), DrawElementsType.UnsignedInt, 0);
ModelShader.UnuseShader();
}
public void RenderTerrain(Terrain t)
{
foreach (var chunk in t.chunks)
{
RenderEntity(chunk.GetEntity());
}
}
public void SetActiveCamera(Camera camera)
{
activeCamera = camera;
}
}
And this is my Framebuffer Class
class Framebuffer
{
int frameBufferID;
int textureID;
int width, height;
public Framebuffer(int width, int height)
{
this.width = width;
this.height = height;
frameBufferID = GL.GenRenderbuffer();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
textureID = CreateTexture();
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, textureID, 0);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
protected int CreateTexture()
{
int returnID;
returnID = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, returnID);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, width, height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, (IntPtr)0);
int nearest = (int)TextureMagFilter.Nearest;
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref nearest);
GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref nearest);
return returnID;
}
public void BeginRendering()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
GL.Viewport(new System.Drawing.Point(0, 0), new System.Drawing.Size(width, height));
}
public void EndRendering()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
public void BindTexture()
{
GL.BindTexture(TextureTarget.Texture2D, textureID);
}
public void ClearBuffer()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
GL.Viewport(new System.Drawing.Point(0, 0), new System.Drawing.Size(width, height));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
}
The expected result is a black quad because I have been implementing Texturing when I discovered that my Model still renders to the screen. I found that out when I commentend out the GL.DrawElements call in EndRendering() which should render the quad to the screen. When I now don't draw that quad the image still appears.
What am I doing wrong?
A render buffer is not a framebuffer object:
frameBufferID = GL.GenRenderbuffer();
When you try to bind an ID you got from glGenRenderbuffers as a framebuffer, you will get an GL_INVALID_OPERATION error from glBindFramebuffer(), and the command will have no further effect (leaving the default framebuffer bound).
New FBO names are generated via glGenFramebuffers, so you should use that.

Beginner troubles with 2D projection and textures in OpenGL

Lately I have been trying to learn/use OpenGL 3+. I have looked through tutorials and examples but I've run into a wall trying to get textures and 2D projection to work without problems.
The goal for now is to have a function which can draw a textured quad to the screen with it's position specified by pixels (not [-1,1]).
For readability and testing I made a new barebones program with the knowledge I currently have, and it exhibits nearly the same problems. Help would be appreciated since i'm starting to go bald over this :(..
The current code shows a garbled texture instead of the image itself (texture is 128x128px).
[Program.cs]
namespace OpenGLTester
{
static class Program
{
public static GameWindow window;
public static String programDirectory = Directory.GetCurrentDirectory();
public static int testTexture;
public static int uniform_fragment_texture;
public static int shaderProgram;
[STAThread]
static void Main()
{
window = new GameWindow(1024, 768, new GraphicsMode(new ColorFormat(8, 8, 8, 8), 0, 8), "OpenGLTester", GameWindowFlags.Default, DisplayDevice.Default, 3, 1, GraphicsContextFlags.Default);
GL.Viewport(new Size(1024,768));
shaderProgram = GL.CreateProgram();
int vertexShader = GL.CreateShader(ShaderType.VertexShader);
int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(vertexShader, File.ReadAllText(programDirectory + #"\vertex.vert"));
GL.ShaderSource(fragmentShader, File.ReadAllText(programDirectory + #"\fragment.frag"));
GL.CompileShader(vertexShader);
GL.CompileShader(fragmentShader);
GL.AttachShader(shaderProgram, vertexShader);
GL.AttachShader(shaderProgram, fragmentShader);
GL.LinkProgram(shaderProgram);
if (GL.GetError() != ErrorCode.NoError) { System.Diagnostics.Debugger.Break(); }
Console.WriteLine(GL.GetProgramInfoLog(shaderProgram));
GL.UseProgram(shaderProgram);
Matrix4 projectionMatrix = Matrix4.CreateOrthographic(1024, 768, 0, 1);
GL.UniformMatrix4(GL.GetUniformLocation(shaderProgram, "vertex_projection"), false, ref projectionMatrix);
uniform_fragment_texture = GL.GetUniformLocation(shaderProgram, "fragment_texture");
testTexture = loadTexture(programDirectory + #"\test.png");
GL.Disable(EnableCap.DepthTest);
GL.Disable(EnableCap.Lighting);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
window.UpdateFrame += window_UpdateFrame;
window.RenderFrame += window_RenderFrame;
window.Resize += window_Resize;
window.TargetRenderFrequency = 60;
window.Run();
}
static void window_Resize(object sender, EventArgs e)
{
//Don't allow resizing for now.
window.Size = new Size(1024, 768);
}
static void window_UpdateFrame(object sender, FrameEventArgs e)
{
ErrorCode currentError = GL.GetError();
if (currentError != ErrorCode.NoError)
{
Console.WriteLine(Enum.GetName(typeof(ErrorCode), currentError));
System.Diagnostics.Debugger.Break();
}
}
static void window_RenderFrame(object sender, FrameEventArgs e)
{
GL.ClearColor(0, 0, 0, 0);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit);
//test texture is 128x128pixels.
drawTexRect(100, 228, 100, 228, testTexture);
window.SwapBuffers();
}
static int loadTexture(String filePath)
{
GL.Enable(EnableCap.Texture2D);
int id = GL.GenTexture();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, id);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
Bitmap bmp = new Bitmap(filePath);
BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, bmp_data.Scan0);
bmp.UnlockBits(bmp_data);
bmp.Dispose();
return id;
}
static void drawTexRect(float top, float bottom, float left, float right, int texture)
{
//topLeft,bottomLeft,bottomRight,topRight
float[] vertices = new float[] {
left, top, 0, 0,
left, bottom, 0, 1,
right, bottom, 1, 1,
right, top, 1, 0,
};
int buffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
GL.BufferData<float>(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
//vec2 - screen position
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4, 0);
//vec2 - texture coordinates
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 4, 2 * sizeof(float));
GL.Enable(EnableCap.Texture2D);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.Uniform1(uniform_fragment_texture, 0);
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
GL.DeleteBuffer(buffer);
}
}
}
[vertex.vert]
#version 330
in vec2 vertex_position;
in vec2 vertex_texturePosition;
uniform mat4 vertex_projection;
out vec2 fragment_texturePosition;
void main()
{
gl_Position = vec4(vertex_position,0.0,1.0) * vertex_projection;
fragment_texturePosition = vertex_texturePosition;
}
[fragment.frag]
#version 330
in vec2 fragment_texturePosition;
uniform sampler2D fragment_texture;
out vec4 output_color;
void main()
{
output_color = texture(fragment_texture,fragment_texturePosition);
}
After changes suggested by #j-p one problem remains:
After texture position change suggested by #j-p:
The projection is also wrong given the position i expect it to be 100 px from the left and 100 px from the top, don't see how i can fix this..
The stride parameter is in byte:
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
//vec2 - texture coordinates
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2 * sizeof(float));
Also,the corresponding opengl pixel format for windows argb bitmap is BGRA. (link)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,**OpenTK.Graphics.OpenGL.PixelFormat.Bgra**, PixelType.UnsignedByte, data.Scan0);
And finally, your texture coordinates should be adjusted as follow:
float[] vertices = new float[] {
left, top, 0, 1,
left, bottom, 0, 0,
right, bottom, 1, 0,
right, top, 1, 1
};

Preventing effects from over laying?

So basically I've got 2 effects. 1 is Sepia and the other is some Aqua effect, when I put the Aqua effect onto the image and then go to put the Sepia effect onto it the Sepia effect will overlay the Aqua. I don't understand why it is doing it, I know to add an additional picture box to it and make the picturebox invisible but this isn't an efficient way of doing it, their must be another more efficient way of doing it, can anyone please share?
Sepia
private void btnGrayscale_Click(object sender, EventArgs e)
{
Bitmap grayScale = (Bitmap)picOriginal.Image.Clone();
int height = grayScale.Size.Height;
int width = grayScale.Size.Width;
for (int yCoordinate = 0; yCoordinate < height; yCoordinate++)
{
for (int xCoordinate = 0; xCoordinate < width; xCoordinate++)
{
Color color = grayScale.GetPixel(xCoordinate, yCoordinate);
int grayColor = (color.R + color.G + color.B) / 3;
grayScale.SetPixel(xCoordinate, yCoordinate, Color.FromArgb(grayColor,grayColor,grayColor));
}
}
picModified.Image = grayScale;
}
Aqua
ColorMatrix matrix = new ColorMatrix(new float[][]{
new float[] {0, 4, 0, 0, 0},
new float[] {0, 0, 4, 0, 4},
new float[] {0, 0, 0, 4, 0},
new float[] { 0, 0, 4, 0, 1},
new float[] { 0, 0, 0, 4, 0}
});
Image image = (Bitmap)picOriginal.Image.Clone();
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix);
Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(image,
new Rectangle(0, 0, image.Width, image.Height),0,0,image.Width,image.Height,
GraphicsUnit.Pixel,attributes);
graphics.Dispose();
picModified.Image = image;
}

trouble with showing texture Tao-framework/OpenGl

I am using Tao Framework to work with OpenGl. I`m trying to load a texture and show it on the form. Image seems to be load correctly (texture id is generated and no errors occure) but it is not shown correctly.
Here is my code for loading texture
public static class TextureManager
{
static TextureManager()
{
Il.ilInit();
Il.ilEnable(Il.IL_ORIGIN_SET);
}
public static int LoadTexture(string url)
{
int texObject = -1;
int imageId;
Il.ilGenImages(1, out imageId);
Il.ilBindImage(imageId);
if (Il.ilLoadImage(url))
{
int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
Gl.glGenTextures(1, out texObject);
Gl.glPixelStorei(Gl.GL_UNPACK_ALIGNMENT, 1);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texObject);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_REPLACE);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, width, height, 0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, Il.ilGetData());
Il.ilDeleteImages(1, ref imageId);
}
return texObject;
}
}
And code for drawing it:
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glClearColor(255, 255, 255, 1);
Gl.glLoadIdentity();
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, mGlTextureObject);
Gl.glPushMatrix();
Gl.glTranslated(0, -1, -10);
Gl.glRotated(rot, 0, 1, 0);
Gl.glBegin(Gl.GL_QUADS);
Gl.glNormal3f(0, 0, 1);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(0, 0, 0);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(1, 0, 0);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(1, 1, 0);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(0, 1, 0);
Gl.glEnd();
Gl.glPopMatrix();
Gl.glDisable(Gl.GL_TEXTURE_2D);
AnT.Invalidate();
The form is just blank. But if I enable lightning then here what is showed:
The image I am loading is a simple bmp image 100x100 created in Paint.
The problem appeared because the image was in incorrect size. Size of images should be 64x64, 128x128 or 256x256.
Changed the size to 128x128 and everything worked fine.

Categories

Resources