带有 SOIL 的 OpenGL 纹理
OpenGL Texturing with SOIL
我正致力于在 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;
}
任何帮助将不胜感激,谢谢!
迈克尔。为了生成贴图并可能修复纹理,您应该将土壤加载函数的参数更改为
GLuint texture = SOIL_load_OGL_texture
(
texname,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
这将自动从 OpenGL 库生成 mipmap,因为您之后没有调用 glGenerateMipmaps()。它还节省了存储纹理所需的内存。纹理夹紧和包裹的另一个建议是使用
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
使用加载功能后。祝您的 OpenGL 学习体验顺利。我希望这对你有所帮助。
编辑:
您也不应该每一帧都重复 glTexParameteri。这会给您(或用户)带来不必要的压力CPU。
我正致力于在 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;
}
任何帮助将不胜感激,谢谢!
迈克尔。为了生成贴图并可能修复纹理,您应该将土壤加载函数的参数更改为
GLuint texture = SOIL_load_OGL_texture
(
texname,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
这将自动从 OpenGL 库生成 mipmap,因为您之后没有调用 glGenerateMipmaps()。它还节省了存储纹理所需的内存。纹理夹紧和包裹的另一个建议是使用
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
使用加载功能后。祝您的 OpenGL 学习体验顺利。我希望这对你有所帮助。
编辑:
您也不应该每一帧都重复 glTexParameteri。这会给您(或用户)带来不必要的压力CPU。