【发布时间】:2014-01-22 02:35:23
【问题描述】:
编辑: 我把它缩小到这个:看起来如果我在我的函数中执行以下操作,然后在渲染器函数中不再调用 glBindTexture 它仍然会渲染......纹理......
NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 );
//glGenTextures ( 1 , &NM->texture [ 0 ] ); // ** OFF **
//glBindTexture ( GL_TEXTURE_2D, NM->texture [ 0 ] ); // ** OFF **
//glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ); // ** Enableing This Will Render White (no texture)
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ); // ** Enableing This Will Render the texture for both objects *** SUPER WEIRD ***
// *** ALSO *** I dont ever call glBindTexture Again after this but still the object is being textured ***
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->width , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data );
stbi_image_free ( NM->data );
我一定是没看懂,或者我的代码有很大的错误。
我只是想获取加载和渲染材质的 WaveFront 对象。话虽如此,我只是想拼凑一些代码,以更好地理解一切是如何工作的,这样我就可以回去重新编程一切。 (我在窗户上)
我可以从文件加载 .objects 甚至加载纹理...问题是,最后加载的纹理用于所有对象....
这里是 glIntercept 用于加载图像...
glGenTextures(1,05331C10)
glBindTexture(GL_TEXTURE_2D,1)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1024,1024,0,GL_RGBA,GL_UNSIGNED_BYTE,05733040)
glGenTextures(1,05332780)
glBindTexture(GL_TEXTURE_2D,2)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1024,1024,0,GL_RGBA,GL_UNSIGNED_BYTE,0573D040)
.. 来自:
NM->MapUsed = true;
sscanf ( coord [ i ]->c_str() , "map_Kd %s" , NM->map_Kd );
char FileBuf [ 256 ] = { 0x0 };
sprintf ( FileBuf , "C:\\OpenGL\\Debug\\%s" , NM->map_Kd );
NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 );
glGenTextures ( 1 , &NM->texture );
glBindTexture ( GL_TEXTURE_2D, NM->texture );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data );
stbi_image_free ( NM->data );
上面的代码是我加载纹理的方式。 NM 在 VectorMaterialLibrary 中的一个向量数组中,也就是在 ObjectLoader 向量中...
void Renderer() {
updatecam();
// all My Objects
for ( int i = 0 ; i < ObjectLoader.size() ; i++ ) {
sObjectLoader *TempObject = ObjectLoader [ i ];
if ( TempObject->MTLFile ) {
for ( int z = 0 ; z < TempObject->VectorMaterialLibrary.size() ; z++ ) {
sMaterialLibrary *TempMaterial = TempObject->VectorMaterialLibrary [ z ];
if ( strcmp ( TempMaterial->newmtl , TempObject->usemtl ) == 0 ) {
if ( TempMaterial->MapUsed ) {
glBindTexture ( GL_TEXTURE_2D, TempMaterial->texture ); // *** This Dings 1/2 respectively ***
}
break;
}
}
}
for ( int z = 0 ; z < TempObject->_3f.size() ; z++ ) {
s3f *Temp3f = TempObject->_3f [ z ];
if ( Temp3f->GL_TYPE == GL_QUADS ) {
glBegin ( GL_QUADS );
if ( Temp3f->boolvn ) {
glNormal3f ( Temp3f->vn [ 0 ] , Temp3f->vn [ 1 ] , Temp3f->vn [ 2 ] );
}
if ( Temp3f->boolv ) {
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 0 ] , Temp3f->vt [ 1 ] );
glVertex3f ( Temp3f->v [ 0 ] , Temp3f->v [ 1 ] , Temp3f->v [ 2 ] );
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 2 ] , Temp3f->vt [ 3 ] );
glVertex3f ( Temp3f->v [ 3 ] , Temp3f->v [ 4 ] , Temp3f->v [ 5 ] );
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 4 ] , Temp3f->vt [ 5 ] );
glVertex3f ( Temp3f->v [ 6 ] , Temp3f->v [ 7 ] , Temp3f->v [ 8 ] );
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 6 ] , Temp3f->vt [ 7 ] );
glVertex3f ( Temp3f->v [ 9 ] , Temp3f->v [ 10 ] , Temp3f->v [ 11 ] );
}
glEnd();
} else {
glBegin ( GL_TRIANGLES );
if ( Temp3f->boolvn ) {
glNormal3f ( Temp3f->vn [ 0 ] , Temp3f->vn [ 1 ] , Temp3f->vn [ 2 ] );
}
if ( Temp3f->boolv ) {
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 0 ] , Temp3f->vt [ 1 ] );
glVertex3f ( Temp3f->v [ 0 ] , Temp3f->v [ 1 ] , Temp3f->v [ 2 ] );
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 2 ] , Temp3f->vt [ 3 ] );
glVertex3f ( Temp3f->v [ 3 ] , Temp3f->v [ 4 ] , Temp3f->v [ 5 ] );
if ( Temp3f->boolvt ) glTexCoord2f ( Temp3f->vt [ 4 ] , Temp3f->vt [ 5 ] );
glVertex3f ( Temp3f->v [ 6 ] , Temp3f->v [ 7 ] , Temp3f->v [ 8 ] );
}
glEnd();
}
}
}
}
此外,渲染的 glintercept(进行最后一次 image1 调用和 image2 的开始...)
glBegin(GL_TRIANGLES) Textures[ (0,1) ]
glNormal3f(0.000000,0.000000,-1.000000)
glTexCoord2f(1.000000,0.000000)
glVertex3f(1.892851,-0.950534,-3.331728)
glTexCoord2f(1.000000,1.000000)
glVertex3f(-0.107149,-0.950534,-3.331728)
glTexCoord2f(0.000000,1.000000)
glVertex3f(-0.107149,1.049466,-3.331728)
glEnd()
glBindTexture(GL_TEXTURE_2D,2)
glBegin(GL_TRIANGLES) Textures[ (0,2) ]
glNormal3f(0.000000,-1.000000,-0.000000)
glTexCoord2f(0.000000,0.000000)
glVertex3f(1.892851,-0.950534,-3.331728)
glTexCoord2f(1.000000,0.000000)
glVertex3f(1.892851,-0.950534,-1.331729)
glTexCoord2f(0.000000,1.000000)
glVertex3f(-0.107149,-0.950534,-3.331728)
glEnd()
我想知道我哪里出错了。我真的只是想让它工作,这样我就可以从头开始重新编码框架。
如果你需要更多代码,我可以贴出来,但我不要大墙!!!
// 编辑,手动执行以下操作.. 不好
// I only have 2 objects with this test, and 2 materials...
if ( testing ) {
glGenTextures ( 2 , Gtextures ); // ********Global
testing = false;
NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 );
glBindTexture ( GL_TEXTURE_2D, Gtextures[0] );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data );
stbi_image_free ( NM->data );
} else {
NM->data = stbi_load ( FileBuf , &NM->width , &NM->height , &NM->bit , 0 );
glBindTexture ( GL_TEXTURE_2D, Gtextures[1] );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , NM->width , NM->height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NM->data );
stbi_image_free ( NM->data );
}
**
for ( int z = 0 ; z < TempObject->VectorMaterialLibrary.size() ; z++ ) {
sMaterialLibrary *TempMaterial = TempObject->VectorMaterialLibrary [ z ];
if ( strcmp ( TempMaterial->newmtl , TempObject->usemtl ) == 0 ) {
if ( TempMaterial->MapUsed ) {
//mbci ( "Changing Texture" , i );
//mbci ( "Gtextures [ i ]" , Gtextures [ i ] );
glBindTexture ( GL_TEXTURE_2D, Gtextures [ i ] ); // ** I only have 2 objects so this works for loading the 2 textures...
//glBindTexture ( GL_TEXTURE_2D, TempMaterial->texture );
}
break;
}
}
但是对象还是一样的纹理...
这是我正在渲染的东西的图片。注意一切都是相同的颜色...中间的两个立方体应该是两种不同的颜色,并且其他对象也采用相同的纹理...这是两个纹理(按比例缩小)
What I See http://i.stack.imgur.com/s1NXn.png ** No Idea **
Cube UV1 http://i.stack.imgur.com/s44Tb.png ** This is loaded first**
Cube UV2 http://i.stack.imgur.com/8KXdo.png ** This is loaded last **
【问题讨论】:
-
您是否在第一次 glBegin 绘制调用之前更改纹理?
-
@concept3d 是的,我在其中放置了一些消息框进行检查。嗯。
-
您还需要确保您没有多次上传相同的纹理。 GLdebugger 有一个不错的 OpenGL 调试功能。它是免费的。
-
@concept3d 我也用 FileBuf 的消息框检查了这一点。两者都加载一次。该加载函数仅在解析 .obj 文件时调用一次。生病下载应用程序!谢谢。
-
是否要求您学习已弃用的固定功能管道。如果没有,那么您避免学习使用 opengl 的旧的已弃用方式。