gpt4 book ai didi

c - 奇怪的 OpenGL 纹理问题? :

转载 作者:行者123 更新时间:2023-12-01 23:34:53 25 4
gpt4 key购买 nike

这是我尝试加载的纹理:

enter image description here

这是我在运行代码时看到的内容:

enter image description here

这是我使用的代码:

#include "sdl.h"
#include "sdl_opengl.h"
#include <stdio.h>
#include <gl/GL.h>
#include <gl/GLU.h>

Uint32 loadTexture(char* fileName)
{
Uint32 id;
SDL_Surface *img = NULL;

//load into memory using SDL
img = SDL_LoadBMP(fileName);
//generate an id for this texture
glGenTextures(1, &id);
//use this texture
glBindTexture(GL_TEXTURE_2D, id);
//load the texture into video memory via OpenGL
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
img->w,
img->h,
0,
GL_RGB,
GL_UNSIGNED_SHORT_5_6_5,
img->pixels
);

//set mip map settings
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(img);

return id;
}

Uint32 tex;

void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

tex = loadTexture("fireball.bmp");
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, tex);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(0.25, 0.25, 0.0);


glTexCoord2f(1.0, 0.0);
glVertex3f(0.5, 0.25, 0.0);


glTexCoord2f(1.0, 1.0);
glVertex3f(0.5, 0.5, 0.0);


glTexCoord2f(0.0, 1.0);
glVertex3f(0.25, 0.5, 0.0);
glEnd();
}

int main()
{
INT32 isRunning = 1;
SDL_Surface *screen = NULL;
SDL_Event event;
INT32 start;
INT32 FPS = 30;

SDL_Init(SDL_INIT_EVERYTHING);

screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);

init();

while(isRunning)
{
start = SDL_GetTicks();

while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT: isRunning = 0; break;
}
}

display();

SDL_GL_SwapBuffers();

if(1000 / FPS > SDL_GetTicks() - start)
{
SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));
}
}

SDL_Quit();

return(0);
}

最佳答案

glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
img->w,
img->h,
0,
GL_RGB,
GL_UNSIGNED_SHORT_5_6_5,
img->pixels
);

这实际上是它的格式吗?每 2 个字节是一个 5/6/5 RGB 像素。我不知道 Windows BMP 文件可以存储 5/6/5 图像数据,但我从来没有费心编写图像加载代码,所以我不确定。我以为 BMP 数据只能是 8/8/8 BGR 格式。

好吧,即使它可以是 5/6/5,你确定这张图片是那种格式的吗?

还有,什么是行对齐?我没有看到您使用 glPixelStorei 设置 GL_UNPACK_ALIGNMENT,因此您必须期望每行的大小对齐到 4 个字节。这是真的吗?

关于c - 奇怪的 OpenGL 纹理问题? :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965146/

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