編輯:好的,這段代碼仍然不允許我在程序中使用兩種不同的紋理。它的行爲就像它應該工作,但是當我告訴它使用第一個紋理時,它與第二個紋理相同,這是最後一次加載的紋理。C#SFML OpenGl多紋理問題
private int[] iTextures = new int[3];
public void main()
{
Initialize();
LoadContent();
float Time = 0.0F;
// Start game loop
while (App.IsOpened())
{
// Process events
App.DispatchEvents();
// Clear the window
App.Clear();
App.Draw(Background);
Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT);
// Transformations
Time += App.GetFrameTime();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glTranslatef(0.0F, 0.0F, -200.0F);
Gl.glPushMatrix();
Gl.glScalef(10.0f, 50.0f, 10.0f);
DrawCube(50.0f, 50.0f, 50.0f, 0);
Gl.glPopMatrix();
//Gl.glRotatef(Time * 50, 1.0F, 0.0F, 0.0F);
//Gl.glRotatef(Time * 30, 0.0F, 1.0F, 0.0F);
//Gl.glRotatef(Time * 90, 0.0F, 0.0F, 1.0F);
/*Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 50.0F, 50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(-50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 50.0F, 50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, -50.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, -50.0F, 50.0F);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, 50.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 50.0F, 50.0F);
Gl.glVertex3f(50.0f, 50.0f, 50.0f);
Gl.glVertex3f(50.0f, 0.0f, 50.0f);
Gl.glVertex3f(0.0f, 0.0f, 50.0f);
Gl.glVertex3f(0.0f, 50.0f, 50.0f);
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 10.0F, 50.0F);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, 10.0F, -50.0F);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, 10.0F, -50.0F);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 10.0F, 50.0F);
Gl.glEnd();*/
Draw();
// Finally, display the rendered frame on screen
App.Display();
}
// Don't forget to destroy our texture
int tex = 0;
Gl.glDeleteTextures(1, ref tex);
}
public void Initialize()
{
// Create main window
App.PreserveOpenGLStates(true);
// Setup event handlers
App.Closed += new EventHandler(OnClosed);
App.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
App.Resized += new EventHandler<SizeEventArgs>(OnResized);
}
private void LoadContent()
{
BackgroundImage = new Image("background.jpg");
Background = new Sprite(BackgroundImage);
Text = new String2D("This is a cube");
Text.Position = new Vector2(0, 0);
Text.Color = Color.Black;
// Enable Z-buffer read and write
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glDepthMask(Gl.GL_TRUE);
Gl.glClearDepth(1.0F);
// Setup a perspective projection
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(90.0F, 1.0F, 1.0F, 500.0F); // I assume this is setting up the camera
LoadTexture(new Image("texture.jpg"), 0);
LoadTexture(new Image("Otexture.jpg"), 1);
}
private void Draw()
{
App.Draw(Text);
}
private void LoadTexture(Image Texture, int texNum)
{
using (Image TempImage = Texture)
{
Gl.glGenTextures(1, out iTextures[texNum]); // Texture name, which is a number
Gl.glBindTexture(Gl.GL_TEXTURE_2D, iTextures[texNum]); // Start using the texture
Console.WriteLine(texNum + "");
// Texture options and filters and stuff
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_MIPMAP_LINEAR);
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGBA, (int)TempImage.Width, (int)TempImage.Height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, TempImage.Pixels);
//Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, (int)TempImage.Width, (int)TempImage.Height, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, TempImage.Pixels);
}
Gl.glEnable(Gl.GL_TEXTURE_2D);
}
private void UseTexture(int iTexture)
{
// Bind our texture for use
//Gl.glEnable(Gl.GL_TEXTURE_2D); // Start using the 2D texture
Gl.glBindTexture(Gl.GL_TEXTURE_2D, iTexture); // Bind our texture for current use
Gl.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); // Set color to..white, I think.
}
void DrawCube(float xPos, float yPos, float zPos, int texture)
{
Gl.glPushMatrix();
//UseTexture(1);
Gl.glBegin(Gl.GL_QUADS);
UseTexture(iTextures[0]);
//Gl.glEnable(Gl.GL_TEXTURE_2D);
/* This is the top face*/
Gl.glVertex3f(0.0f, 0.0f, 0.0f);
Gl.glVertex3f(0.0f, 0.0f, -1.0f);
Gl.glVertex3f(-1.0f, 0.0f, -1.0f);
Gl.glVertex3f(-1.0f, 0.0f, 0.0f);
/* This is the front face*/
Gl.glTexCoord2f(0, 1); Gl.glVertex3f(0.0f, 0.0f, 0.0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-1.0f, 0.0f, 0.0f);
Gl.glTexCoord2f(1, 0); Gl.glVertex3f(-1.0f, -1.0f, 0.0f);
Gl.glTexCoord2f(1, 1); Gl.glVertex3f(0.0f, -1.0f, 0.0f);
/* This is the right face*/
Gl.glVertex3f(0.0f, 0.0f, 0.0f);
Gl.glVertex3f(0.0f, -1.0f, 0.0f);
Gl.glVertex3f(0.0f, -1.0f, -1.0f);
Gl.glVertex3f(0.0f, 0.0f, -1.0f);
/* This is the left face*/
Gl.glVertex3f(-1.0f, 0.0f, 0.0f);
Gl.glVertex3f(-1.0f, 0.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, 0.0f);
/* This is the bottom face*/
Gl.glVertex3f(0.0f, 0.0f, 0.0f);
Gl.glVertex3f(0.0f, -1.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, 0.0f);
/* This is the back face*/
Gl.glVertex3f(0.0f, 0.0f, 0.0f);
Gl.glVertex3f(-1.0f, 0.0f, -1.0f);
Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
Gl.glVertex3f(0.0f, -1.0f, -1.0f);
Gl.glEnd();
Gl.glPopMatrix();
}
任何幫助都會很棒。
啊,這是有道理的。老實說,我認爲OpenGl在創建它們時存儲了這些名稱,顯然我錯了。 我會給你建議一個鏡頭,非常感謝。 – Elec0
@ Elec0:當然,OpenGL存儲紋理名稱 - 因爲它是內部管理。但它沒有一種水晶球,它無法讀懂你的想法,也不知道你想使用哪種紋理。你必須告訴OpenGL使用哪個紋理,紋理名稱是這個句柄。 – datenwolf
@Nicol Bolas:如果Elec0忽略了對glGenTextures的調用,假設OpenGL-1.x操作,這實際上已經起作用,因爲OpenGL-1允許使用未顯式生成的隱式對象名稱(然後靜默地生成新的紋理對象與請求的名字)。名稱0保留,所以LoadTexture iTexture參數將以1開頭。 – datenwolf