gpt4 book ai didi

c - 我正在尝试用 C 中的 openGL 创建一个天空盒,但只得到黑屏

转载 作者:行者123 更新时间:2023-11-30 15:24:52 25 4
gpt4 key购买 nike

我正在尝试用 C 语言创建一个带有 OpenGL 的天空盒。我已经查看了很多教程并查看了很多人的代码,我不认为我错过了任何东西,但却得到了黑屏。我是不是把整件事都搞错了?

#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <stdio.h>
#include <math.h>

GLuint texture[6];

/**
* Init function initializing the background to black.
*/
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glMatrixMode(GL_MODELVIEW | GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}

GLuint LoadTexture(const char * filename, int width, int height)
{
GLuint texture;
unsigned char * data;
FILE * file;

//Open the image file
file = fopen(filename, "rb");
//If it doesn't open just return 0
if(file == NULL)
{
return 0;
}

//Allocate space for data and read
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);

//Close the file
fclose(file);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );

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

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
free(data);

return texture;
}

void draw()
{
int width = 512;
int height = 512;
int length = 512;

//start in this coordinates
int x = 0;
int y = 0;
int z = 0;

//center the square
x = x - width / 2;
y = y - height / 2;
z = z - length / 2;

// Bind the BACK texture of the sky map to the BACK side of the cube
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
glEnd();

//FRONT
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z + length);
glEnd();

//BOTTOM
glBindTexture(GL_TEXTURE_2D, texture[4]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z);
glEnd();

//TOP
glBindTexture(GL_TEXTURE_2D, texture[5]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z);
glEnd();

//LEFT
glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z + length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);

glEnd();

//RIGHT
glBindTexture(GL_TEXTURE_2D, texture[3]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z);
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
texture[0] = LoadTexture("back.bmp", 512, 512);
/*texture[0] = SOIL_load_OGl_texture
{
"back.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
};*/
texture[1] = LoadTexture("front.bmp", 512, 512);
texture[2] = LoadTexture("left.bmp", 512, 512);
texture[3] = LoadTexture("right.bmp", 512, 512);
texture[4] = LoadTexture("cesped.bmp", 512, 512);
texture[5] = LoadTexture("top.bmp", 512, 512);

glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

draw();
glutSwapBuffers();
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("blah");

init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();

return 0;
}

最佳答案

问题似乎是天空盒不在可见区域内。当查看投影矩阵 glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); 的定义时,我们发现沿 z 轴的可见值必须在 1.5 之间和20.0。但天空盒是从 -256.0 到 256.0 绘制的,位于远平面/近平面后面并被剪掉。

对此有两种可能的解决方案:可以绘制较小的天空盒或增加可见区域。一般来说,两种情况下的结果是相同的(至少在渲染天空盒而不将深度测试作为第一个对象时,通常会这样做)。

根据评论:纹理的大小(以像素为单位)与几何体的大小之间没有关系。所以不需要画边长为512的正方体。

关于c - 我正在尝试用 C 中的 openGL 创建一个天空盒,但只得到黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28188078/

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