为什么我的OpenGL对象使用相同的纹理进行渲染?

时间:2011-05-02 15:11:37

标签: visual-c++ opengl c++-cli

UPDATE :在@datenwolf的帮助下,我知道gluBuild2DMipmaps的返回值不是指向纹理的指针,而只是一个错误代码。我忘了打电话给glGenTextures和glBindTexture。在这个答案中查看方法LoadTextureRaw

渲染多个对象时遇到问题,每个对象都有自己的纹理文件定义,也就是说,它们都绘制了相同的纹理。我创建了一个类层次结构CDrawObject->CBall。在CDrawObject中,我定义了这个:

public ref class CDrawObject
{
protected:
BYTE * dataTexture;
GLuint * texture;

public:
String ^ filename;  
CDrawObject(void);
virtual void draw();
void LoadTextureRaw();
};

在LoadTextureDraw()中,我定义了这个:

void CDrawObject::LoadTextureRaw()
{
//GLuint texture;
if(!filename) return;
if(filename->Equals("")) return;
texture = new GLuint;

System::Drawing::Bitmap ^ bitmap = gcnew Bitmap(filename);  
int h = bitmap->Height;
int w = bitmap->Width;
int s = w * h;
dataTexture = new BYTE[s * 3];

System::Drawing::Rectangle rect =  System::Drawing::Rectangle(0,0,w,h);
System::Drawing::Imaging::BitmapData ^  bitmapData = 
    bitmap->LockBits(rect,System::Drawing::Imaging::ImageLockMode::ReadWrite , System::Drawing::Imaging::PixelFormat::Format24bppRgb);

::memcpy(dataTexture,bitmapData->Scan0.ToPointer(),s*3);

/* old code
bitmap->UnlockBits(bitmapData); 
pin_ptr<GLuint*> pt = &texture;//pin managed pointer, to be unmanaged
**pt = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w,h,GL_BGR_EXT, GL_UNSIGNED_BYTE, dataTexture);
    */


    //new code : working fine this way. I forgot to call glGenTextures and glBindTexture
    bitmap->UnlockBits(bitmapData); 
pin_ptr<GLuint*> pt = &texture;//pin managed pointer, to be unmanaged... a must here :)
    glEnable(GL_TEXTURE_2D);
glGenTextures(1,*pt);
    glBindTexture(GL_TEXTURE_2D,**pt);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, w,h,GL_BGR_EXT, GL_UNSIGNED_BYTE, dataTexture);

}

正如CBall:绘制自己,我定义了这个:

void CBall::draw(){
glLoadIdentity();

if(texture!=NULL && !filename->Equals(""))
{
    glEnable(GL_TEXTURE_2D);
    pin_ptr<GLuint*> pt = &texture; 
    glBindTexture(GL_TEXTURE_2D,**pt);
}

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glTranslatef(this->x,this->y,this->z);
glRotatef(this->sudut_rotasi_x,1,0,0);
glRotatef(this->sudut_rotasi_y,0,1,0);
glRotatef(this->sudut_rotasi_z,0,0,1);
glScalef(this->x_scale,this->y_scale,this->z_scale);
GLUquadricObj *q = gluNewQuadric();
gluQuadricNormals(q, GL_SMOOTH);
gluQuadricTexture(q, GL_TRUE);
gluSphere(q, r, 32, 16);
glFlush();
    glDisable(GL_TEXTURE_2D);
 }

问题是,当我绘制两个(或更多)球对象时,它们都使用相同的纹理绘制。我已经调试了代码,对于每个对象,它们都有不同的texture变量。这是我的代码的快照,用于绘制这些球:

    ...
    CBall ^ ball = gcnew CBall();
ball->x=Convert::ToSingle(r->GetAttribute("x"));
ball->y=Convert::ToSingle(r->GetAttribute("y"));
ball->z=Convert::ToSingle(r->GetAttribute("z"));
ball->r=Convert::ToSingle(r->GetAttribute("r"));
ball->filename=r->GetAttribute("filename");
ball->LoadTextureRaw();
addGraphic(id, ball);
    ...

这些代码是从读取XML文件方法调用的。

我对这个OpenGL代码做了什么错误?

1 个答案:

答案 0 :(得分:4)

你的问题是,gluBuild2DMipmaps不会返回纹理名称,而是返回错误代码。您需要单独创建纹理名称。

试试这个:

public ref class CDrawObject
{
protected:
    GLuint texture; // just a GLuint, not a pointer!

public:
    String ^ filename;  
    CDrawObject(void);
    virtual void draw();
    void LoadTextureRaw();
};

稍微改变LoadTextureRaw:

void CDrawObject::LoadTextureRaw()
{

    if(!filename)
        return;
    if(filename->Equals(""))
        return;

    System::Drawing::Bitmap ^ bitmap = gcnew Bitmap(filename);  
    int h = bitmap->Height;
    int w = bitmap->Width;
    int s = w * h;

    System::Drawing::Rectangle rect =  System::Drawing::Rectangle(0,0,w,h);
    System::Drawing::Imaging::BitmapData ^  bitmapData = 
        bitmap->LockBits( rect,
                          System::Drawing::Imaging::ImageLockMode::ReadWrite, 
                          System::Drawing::Imaging::PixelFormat::Format24bppRgb );

    // This is the important part: We generate a texture name and...
    glGenTextures(1, &texture); // this should not require a pin_ptr, after all were in the middle of a member function of the class, so the garbage collector will not kick in.

    // ...bind it, causing creation of a (yet uninitialized) texture object 
    glBindTexture(GL_TEXTURE_2D, texture);

    GLint error = gluBuild2DMipmaps(
        GL_TEXTURE_2D,
        GL_RGB, // this should be a valid OpenGL token, not the number of components!
        w, h,
        GL_BGR_EXT, GL_UNSIGNED_BYTE,
        bitmapData->Scan0.ToPointer() );

    bitmap->UnlockBits(bitmapData); 
}

最后画画(我重新安排了一下)

void CBall::draw(){

    glLoadIdentity();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);

    glTranslatef(this->x,this->y,this->z);
    glRotatef(this->sudut_rotasi_x,1,0,0);
    glRotatef(this->sudut_rotasi_y,0,1,0);
    glRotatef(this->sudut_rotasi_z,0,0,1);
    glScalef(this->x_scale,this->y_scale,this->z_scale);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    GLUquadricObj *q = gluNewQuadric();
    gluQuadricNormals(q, GL_SMOOTH);
    gluQuadricTexture(q, GL_TRUE);
    gluSphere(q, r, 32, 16);
    // glFlush is not required
    glDisable(GL_TEXTURE_2D);

}