PDA

View Full Version : Color doesn't change



bansgoo
04-13-2014, 06:35 AM
I'm using Tao Framework, C# and Visual studio 12.
When I draw lines, or quads (2D), or anything, color always stays the same (textures are drawn correctly, though). I don't know what to do really, cause I searched in entire web and didn't find anything that could solve the problem. Please help!
Drawing сode example:

public static void Draw4Angle(RGBubColor Color, FPoint p1, FPoint p2, FPoint p3, FPoint p4)
{
Gl.glColor3ub(Color.r, Color.g, Color.b);
Gl.glBegin(Gl.GL_QUADS);
Gl.glVertex2d(p1.x, p1.y);
Gl.glVertex2d(p2.x, p2.y);
Gl.glVertex2d(p3.x, p3.y);
Gl.glVertex2d(p4.x, p4.y);
Gl.glEnd();
Visualizer.Invalidate();
}
Initialization:

public static void GlutTune2D(SimpleOpenGlControl Graph)
{
Visualizer = Graph;
WorldH = Visualizer.Height;
WorldW = Visualizer.Width;
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);

// очитка окна
Gl.glClearColor(255, 155, 155, 0.2f);

// установка порта вывода в соотвествии с размерами элемента anT
Gl.glViewport(0, 0, Graph.Width, Graph.Height);

// настройка проекции
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glLoadIdentity();
// теперь необходимо корректно настроить 2D ортогональную проекцию
// в зависимости от того, какая сторона больше
// мы немного варьируем то, как будет сконфигурированный настройки проекции
double ScreenW, ScreenH;
if ((float)Visualizer.Width <= (float)Visualizer.Height)
{
ScreenW = 500.0;
ScreenH = 500.0 * (float)Visualizer.Height / (float)Visualizer.Width;

Glu.gluOrtho2D(0.0, ScreenW, 0.0, ScreenH);
}
else
{
ScreenW = 500.0 * (float)Visualizer.Width / (float)Visualizer.Height;
ScreenH = 500.0;

Glu.gluOrtho2D(0.0, 500.0 * (float)Visualizer.Width / (float)Visualizer.Height, 0.0, 500.0);
}
GlH = Convert.ToInt32(ScreenH);
GlW = Convert.ToInt32(ScreenW);
Gl.glEnable(Gl.GL_ALPHA_TEST);
// Gl.glBlendFunc(Gl.GL_ONE, Gl.GL_ONE);
Gl.glEnable(Gl.GL_BLEND);
Gl.glEnable(Gl.GL_COLOR_MATERIAL);
}
public static void DevilTune()
{

// инициализация библиотеки openIL
Il.ilInit();
Ilu.iluInit();
Ilut.ilutInit();
Ilut.ilutRenderer(Ilut.ILUT_OPENGL);
Il.ilEnable(Il.IL_ORIGIN_SET);
}
Visualizer is
private static SimpleOpenGlControl Visualizer;

tonyo_au
04-13-2014, 11:59 PM
You don't show that code that calls Draw4Angle

bansgoo
04-15-2014, 03:42 PM
You don't show that code that calls Draw4Angle

Trouble is that I've tried million of ways of calling that function. And nothing happened. Ok, here's the code (with one of overloads of Draw4Angle):


timer.Tick += (a, b) =>
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

MyGlutUser.DrawSquare(() => { Gl.glColor3d(0.8, 0.10, 0.10); }, new FPoint(100, 100), 200);
simpleOpenGlControl1.Invalidate();
};

//---------------------------------------
public delegate void proced();
public static void DrawSquare(proced ColorProc, FPoint p, double size)
{
DrawRect(ColorProc, p, size, size);
}
public static void DrawRect(proced ColorProc, FPoint p, double a, double b)
{
Draw4Angle(ColorProc, p, p.NewAddY(b), p.NewAddX(a).NewAddY(b), p.NewAddX(a));
}
public static void Draw4Angle(proced ColorProc, FPoint p1, FPoint p2, FPoint p3, FPoint p4)
{
ColorProc();
Gl.glBegin(Gl.GL_QUADS);
ColorProc();
Gl.glVertex2d(p1.x, p1.y);
ColorProc();
Gl.glVertex2d(p2.x, p2.y);
Gl.glVertex2d(p3.x, p3.y);
Gl.glVertex2d(p4.x, p4.y);
Gl.glEnd();
// Visualizer.Invalidate();
}

I noticed that when I use 2D textures to draw, or, to be exact, when I load and bind texture into memory, color trouble appears. If I don't use textures, color works fine.

public abstract class Visible
{
public abstract void Display();
public String PathToImage
{ get; set; }
public RGBubColor Color
{ get; set; }
protected int imageID;
public int ImageID
{
get
{
return imageID;
}
set
{
imageID = value;
}
}
protected uint mGlTextureObject { get; set; }
public FPoint Location
{ get; set; }
public static int Counter = 0;
public uint TextureObj()
{
return mGlTextureObject;
}
public void LoadTextureForModel(string FileName)
{
// saving filename

PathToImage = FileName;
// creating the image with imageId identifier
Il.ilGenImages(1, out imageID);
// Making the image the current one
Il.ilBindImage(imageID);

// if loaded successfully
if (Il.ilLoadImage(PathToImage))
{
// saving size of image
int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);

// find bits per pixel count
int bitspp = Il.ilGetInteger(Il.IL_IMAGE_BITS_PER_PIXEL);

// creating texture using GL_RGB or GL_RGBA
switch (bitspp) // в зависимости оп полученного результата
{
case 24:
mGlTextureObject = MakeGlTexture(Gl.GL_RGB, Il.ilGetData(), width, height);
break;
case 32:
mGlTextureObject = MakeGlTexture(Gl.GL_RGBA, Il.ilGetData(), width, height);
break;
}
Il.ilDeleteImages(1, ref imageID);
}
}

private static uint MakeGlTexture(int Format, IntPtr pixels, int w, int h)
{
// texture object identifier
uint texObject=1;

// generating texture object
Gl.glGenTextures(1, out texObject);

// setting pixel storing mode
Gl.glPixelStorei(Gl.GL_UNPACK_ALIGNMENT, 1);

// binding to newly created texture

Gl.glBindTexture(Gl.GL_TEXTURE_2D, texObject);

// setting modes of filtering and repeating for texture
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);

// creating RGB or RGBA texture
switch (Format)
{

case Gl.GL_RGB:

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, w, h, 0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, pixels);
break;

case Gl.GL_RGBA:
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, w, h, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, pixels);
break;
}
// returning ID of texture object
return texObject;
}
}
Here's class I used in my case - class of background image

public class Background : Visible
{
public double dx=0;
public double dy=0;
public double dw=100;
public double dh=100;
public Background(string Path)
{
LoadTextureForModel(Path);
}
public override void Display()
{
MyGlutUser.DrawRect((int)TextureObj(), new FPoint(dx, dy),
MyGlutUser.WorldW + dw, MyGlutUser.WorldH + dh);
}
}
DrawRect function with texture:

public static void DrawRect(int tex, FPoint p, double a, double b) //a, b - стороны
{
Draw4Angle(tex, p, p.NewAddY(b), p.NewAddX(a).NewAddY(b), p.NewAddX(a));
}
public static void Draw4Angle(int tex, FPoint p1, FPoint p2, FPoint p3, FPoint p4)
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, tex);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2i(0, 0);
Gl.glVertex2d(p1.x, p1.y);
Gl.glTexCoord2i(0, 1);
Gl.glVertex2d(p2.x, p2.y);
Gl.glTexCoord2i(1, 1);
Gl.glVertex2d(p3.x, p3.y);
Gl.glTexCoord2i(1, 0);
Gl.glVertex2d(p4.x, p4.y);
Gl.glEnd();
}
Is there something really wrong in all this? Something that crashes colors of drawing? Thank you