我正在努力在 openGL 中构建篮球游戏,但是我正在采取婴儿步骤并将我的工作分成几个部分。我目前专注于纹理,尝试使用 SOIL 库建造房屋。
我对这一切都非常陌生,所以我可能设置错了,但我的主要问题是纹理的行为就像它们被夹在我所看到的边缘一样。
目前该项目是一堵用砖块纹理化的奇异墙,这是我得到的输出图像: Texture Fail
这是源代码:
#include <stdlib.h>
#include <GL/glut.h>
#include <SOIL.h>
#include <stdio.h>
float _angle = 0.0;
GLuint _textureBrick;
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Front side brick wall
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, _textureBrick);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTranslatef(0,0,-6);
glRotatef(_angle, 0.0, 1.0, 0.0);
glBegin(GL_QUADS); // Wall
glTexCoord3f(-50.0,2.0,0.1); glVertex3f(-50,0,1);
glTexCoord3f(50.0,2.0,0.1); glVertex3f(50,0,1);
glTexCoord3f(50.0,0.0,0.1); glVertex3f(50,-1.5,1);
glTexCoord3f(-50.0,0.0,0.1); glVertex3f(-50,-1.5,1);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void mySpecialFunc(int key, int x, int y){
switch (key) {
case GLUT_KEY_RIGHT:
_angle += 1;
if (_angle > 360) _angle = 0.0;
break;
case GLUT_KEY_LEFT:
_angle -= 1;
if (_angle > 360) _angle = 0.0;
break;
}
glutPostRedisplay();
}
GLuint loadTex(const char* texname)
{
GLuint texture = SOIL_load_OGL_texture
(
texname,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if( 0 == texture )
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
}
//glBindTexture(GL_TEXTURE_2D, texture);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return texture;
}
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
//_textureFloor = loadTex("C:\\Users\\Mikle\\OneDrive\\Uni work\\Second Year\\Projects\\3D-House-using-OpenGL-and-C--master\\court.png");
_textureBrick = loadTex("C:\\Users\\Mikle\\OneDrive\\Uni work\\Second Year\\Projects\\3D-House-using-OpenGL-and-C--master\\bricks.bmp");
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(600,600);
glutCreateWindow("Textured House");
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(resize);
glutSpecialFunc(mySpecialFunc);
glutDisplayFunc(renderScene);
Initialize();
glutMainLoop();
return 0;
}
任何帮助将不胜感激,谢谢!