gpt4 book ai didi

c - 纹理不显示

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:50 25 4
gpt4 key购买 nike

我正在加载位图文件并以这种方式创建纹理:

LoadBitmapFromFile( "img.bmp", &bitmapFile );

glGenTextures(1, &texture[0] );
glBindTexture(GL_TEXTURE_2D, texture[0] );


glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmapFile.width, bitmapFile.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapFile.data);


glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

稍后,我尝试对三角形进行纹理处理:

glEnable( GL_TEXTURE_2D );
glPushMatrix();
glBindTexture( GL_TEXTURE_2D, texture[0] );
glBegin( GL_TRIANGLE_STRIP );
glTexCoord2f( 0, 0 ); glVertex2f( 0, 768 );
glTexCoord2f( 1, 0 ); glVertex2f( 1024, 768 );
glTexCoord2f( 1, 1 ); glVertex2f( 0, 0 );
glTexCoord2f( 0, 1 ); glVertex2f( 1024, 0 );
glEnd();
glPopMatrix();
glDisable( GL_TEXTURE_2D );

但纹理不会显示在屏幕上,其他彩色图元会显示。

它可以与 glDrawPixel 一起使用,但不能对三角形进行纹理处理,这有什么问题吗?

我简化了我的应用程序只是为了显示问题:(还必须包括 glext,因为它没有找到 GL_CLAMP_TO_EDGE)

#include <GL/glfw.h>
#include <fstream>
#include <cstdlib>
#include "glext.h"

typedef struct BitmapFile
{
int width, height;
unsigned char* data;
};

BitmapFile bitmapFile;

GLuint texture[1];

void LoadBitmapFromFile( char *filename, BitmapFile *bitmap )
{
int bitmapSize = 0;
int i = 0;

FILE *file;

file = fopen( filename, "rb" );
if( !file )
{
fclose(file);
return;
}

fseek(file, 18, SEEK_SET);
fread(&bitmap->width, 4, 1, file);

fseek(file, 22, SEEK_SET);
fread(&bitmap->height, 4, 1, file);

fseek(file, 34, SEEK_SET);
fread(&bitmapSize, 4, 1, file);

bitmap->data = (unsigned char*)malloc(bitmapSize);
fseek(file, 54, SEEK_SET);
fread(bitmap->data, bitmapSize, 1, file);

for( i = 0; i < bitmapSize; i+=3 )
{
int tempByte = bitmap->data[i];
bitmap->data[i] = bitmap->data[i+2];
bitmap->data[i+2] = tempByte;
}

fclose(file);
}

int LoadGLTextures()
{
LoadBitmapFromFile( "img.bmp", &bitmapFile );

glGenTextures(1, &texture[0] );
glBindTexture(GL_TEXTURE_2D, texture[0] );


glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmapFile.width, bitmapFile.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapFile.data);


glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

/*glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );*/
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

return true;
}

int main()
{
glfwInit();

glEnable( GL_TEXTURE_2D );
glShadeModel( GL_SMOOTH );

LoadGLTextures();

glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE, GL_TRUE );
glfwOpenWindow( 1024, 768, 0, 0, 255, 0, 32, 0, GLFW_WINDOW );


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1024, 768, 0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );

while( !glfwGetKey( GLFW_KEY_ESC ) )
{
glClearColor(0.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture( GL_TEXTURE_2D, texture[0] );
glPushMatrix();

glBegin( GL_TRIANGLE_STRIP );
glTexCoord2f( 0, 0 ); glVertex2f( 0, 768 );
glTexCoord2f( 1, 0 ); glVertex2f( 1024, 768 );
glTexCoord2f( 1, 1 ); glVertex2f( 0, 0 );
glTexCoord2f( 0, 1 ); glVertex2f( 1024, 0 );
glEnd();
glPopMatrix();

glDrawPixels( bitmapFile.width, bitmapFile.height, GL_RGB, GL_UNSIGNED_BYTE, bitmapFile.data);

glfwSwapBuffers();
glFlush();
}

glfwTerminate();
}

http://imageshack.us/photo/my-images/254/89567090.jpg/


好吧,我无能为力,在上次更改后加载函数看起来像这样:

int LoadGLTextures()                                  
{
LoadBitmapFromFile( "img.bmp", &bitmapFile );

glGenTextures(1, &texture[0] );
glBindTexture(GL_TEXTURE_2D, texture[0] );

//glGenerateMipmap(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapFile.width, bitmapFile.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapFile.data);

for( int i = 0 ; i < 8 ; i++ )
{
glTexImage2D(GL_TEXTURE_2D, i+1, GL_RGB, bitmapFile.width/(2*(i+1)), bitmapFile.height/(2*(i+1)), 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapFile.data);
}

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
return true;
}

但在屏幕上仍然没有任何变化。(位图为 256x256)

最佳答案

您只加载一个级别,但通过调用启用 mipmaping

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

如果只加载一个级别,你必须禁用 mipmaping

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

因看过代码而更新:

OP 尝试在 OpenGL 上下文可用之前加载纹理。

关于c - 纹理不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com