OpenGL尝试绘制多个2d纹理,仅出现第一个 [英] OpenGL trying to Draw Multiple 2d Textures, only 1st one appears
问题描述
我遇到了一个问题,就是我试图使用纹理(每个星球一个纹理)绘制一个太阳系,并且在绘制纹理时,只有第一个出现.其余的都不做.
I've got a problem in that I'm trying to draw a solar system using textures (one texture for each planet) and as I draw my textures, only the 1st one appears. None of the rest do.
我的init函数遍历文件,将纹理保存到对象中,然后遍历对象.在进行迭代时,它会生成纹理并将其使用OpenGL调用绑定到名称.
My init function iterates through my files, saves the textures into an object, and then iterates through the objects. As it's iterating, it generates the textures and binds them to a name using the OpenGL calls.
SharpGL.OpenGL gl = args.OpenGL;
gl.Enable(SharpGL.OpenGL.GL_BLEND);
gl.Enable(SharpGL.OpenGL.GL_TEXTURE_2D);
gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);
gl.ClearColor(0, 0, 0, 1);
foreach (ImageWrapper iw in m_images)
{
uint[] texNames = new uint[1];
gl.GenTextures(1, texNames);
gl.BindTexture(SharpGL.OpenGL.GL_TEXTURE_2D, texNames[0]);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_WRAP_S, SharpGL.OpenGL.GL_REPEAT);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_WRAP_T, SharpGL.OpenGL.GL_REPEAT);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_MAG_FILTER, SharpGL.OpenGL.GL_LINEAR);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_MIN_FILTER, SharpGL.OpenGL.GL_LINEAR);
gl.TexImage2D(SharpGL.OpenGL.GL_TEXTURE_2D,
0,
3,
iw.BitmapSource.PixelWidth,
iw.BitmapSource.PixelHeight,
0,
SharpGL.OpenGL.GL_BGRA,
SharpGL.OpenGL.GL_UNSIGNED_BYTE,
iw.Pixels);
iw.TextureHandle = texNames;
}
这是我绘制所有内容的函数.我遍历所有具有纹理数据的对象,并尝试一次绘制一个.我还想执行一些旋转和变换以使我的太阳系旋转.该部分对于出现的纹理效果很好.
Here is my function that draws everything. I iterate through all my objects that have the texture data and try to draw them one at a time. I also want to execute some rotations and transformations to get my solar system to spin. That part works fine for the texture that appears.
SharpGL.OpenGL gl = args.OpenGL;
gl.Clear(SharpGL.OpenGL.GL_COLOR_BUFFER_BIT | SharpGL.OpenGL.GL_DEPTH_BUFFER_BIT );
gl.LoadIdentity();
float[] data = new float[4];
foreach (ImageWrapper iw in m_images)
{
//gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview);
gl.Ortho2D(0, this.Width, 0, this.Height);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, iw.TextureHandle[0]);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_WRAP_S, SharpGL.OpenGL.GL_REPEAT);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_WRAP_T, SharpGL.OpenGL.GL_REPEAT);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_MAG_FILTER, SharpGL.OpenGL.GL_LINEAR);
gl.TexParameter(SharpGL.OpenGL.GL_TEXTURE_2D, SharpGL.OpenGL.GL_TEXTURE_MIN_FILTER, SharpGL.OpenGL.GL_LINEAR);
gl.PushMatrix();
//gl.Translate(this.Width / 2, this.Height / 2, 0);
//gl.Rotate(iw.RotationAboutSun, 0, 0, 1);
//gl.Translate(iw.X + 1 * this.Width / 2, iw.Y + 1 * this.Height / 2, 0);
gl.Translate(32,32, 0);
//gl.Rotate(iw.RotationAboutAxis, 0, 0, 1);
//
gl.Begin(SharpGL.Enumerations.BeginMode.Quads);
gl.Color(new float[] { 1f,1f,1f});
gl.TexCoord(0,0);
gl.Vertex(new double[] { 0 - iw.PixelWidth / 2, 0 - iw.PixelHeight / 2});
gl.TexCoord(0,1);
gl.Vertex(new double[] { 32 - iw.PixelWidth / 2, 0 - iw.PixelHeight / 2 });
gl.TexCoord(1,1);
gl.Vertex(new double[] { 32 - iw.PixelWidth / 2, 32 - iw.PixelHeight / 2 });
gl.TexCoord(1,0);
gl.Vertex(new double[] { 0 - iw.PixelWidth / 2, 32 - iw.PixelHeight / 2 });
gl.End();
gl.PopMatrix();
gl.Disable(OpenGL.GL_TEXTURE_2D);
}
我正在使用C#中的SharpGL库来做到这一点.
I'm using the SharpGL library in C# to do this.
推荐答案
gluOrtho2D()
不会像您期望的那样发布glLoadIdentity()
.
gluOrtho2D()
doesn't issue a glLoadIdentity()
like you seem to expect.
通常,您仅每帧设置一次投影矩阵 .
Generally you only set your projection matrix once per frame.
尝试这样的事情:
void DrawFrame()
{
...
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D(0, this.Width, 0, this.Height);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// "camera" transform(s)
foreach object
{
glPushMatrix();
// per-object matrix transform(s)
// draw object
glPopMatrix();
}
}
这篇关于OpenGL尝试绘制多个2d纹理,仅出现第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!