本文介绍了OpenGl纹理.......... ppm背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用ppm加载程序将图像设置为背景,但有一个问题
在这里的颜色是我使用的代码和图像。
p>
这里是代码.....
texture.h
#ifndef TEXTURE_H
#define TEXTURE_H
struct Image
{
unsigned char * pixels;
int width;
int height;
int numChannels;
};
class Texture
{
public:
Texture();
void Prepare(int texN);
void ReadPPMImage(char * fn);
GLuint texName;
图像图像;
};
#endif
texture.cpp
#include< fstream>
#include< glut.h>
#pragma warning(disable:4996)
#includeTexture.h
Texture :: Texture()
{
}
void Texture :: Prepare(int texN)
{
texName = texN;
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glBindTexture(GL_TEXTURE_2D,texName);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,image.width,
image.height,0,GL_RGB,GL_UNSIGNED_BYTE,
image.pixels);
}
void Texture :: ReadPPMImage(char * fn)
{
int tmpint;
char str [100];
FILE * inFile = fopen(fn,rb);
if(inFile == NULL)
{
printf(无法打开输入文件%s。Exiting.\\\
,fn);
exit(1);
}
fscanf(inFile,P%d \\\
,& tmpint);
if(tmpint!= 6)
{
printf(Input file is not ppm。Exiting.\\\
);
exit(1);
}
//跳过标题中嵌入的注释
fgets(str,100,inFile);
while(str [0] =='#')
fgets(str,100,inFile);
//读取图片尺寸
sscanf(str,%d%d,& image.width,& image.height);
fgets(str,100,inFile);
sscanf(str,%d,& tmpint);
if(tmpint!= 255)
printf(Warning:maxvalue is not 255 in ppm file\\\
);
image.numChannels = 3;
image.pixels =(unsigned char *)malloc(image.numChannels * image.width * image.height * sizeof(unsigned char));
if(image.pixels == NULL)
{
printf(无法分配大小为%dx%d的图片。退出\\\
,image.width ,image.height);
exit(1);
}
else
printf(读取图像%s的大小%dx%d \\\
,fn,image.width,image.height);
fread(image.pixels,sizeof(unsigned char),image.numChannels * image.width * image.height,inFile);
fclose(inFile);
}
Main.cpp
#include< glut.h>
#includeTexture.h
#pragma warning(disable:4996)
const float fMinX = -5.0,fMinY = -5.0,fNearZ = 1.0,
fMaxX = 5.0,fMaxY = 5.0,fFarZ = 10.0;
纹理ImageOne;
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_DEPTH_TEST);
glGenTextures(1,& ImageOne.texName);
ImageOne.ReadPPMImage(wood_1.ppm);
ImageOne.Prepare(1);
}
void Reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(fMinX,fMaxX,fMinY,fMaxY,fNearZ,fFarZ);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
glBindTexture(GL_TEXTURE_2D,ImageOne.texName);
glBegin(GL_QUADS);
glTexCoord2f(0,1);
glVertex3f(-5.5,5,-6);
glTexCoord2f(0,0);
glVertex3f(-5.5,-5,-6);
glTexCoord2f(1,0);
glVertex3f(5,-5,-6);
glTexCoord2f(1,1);
glVertex3f(5,5,-6);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
glFlush();
}
void main(int argc,char ** argv)
{
//初始化GLUT并创建窗口
glutInit(& argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow(OpenGL - Rotating Cubes);
Init();
//注册回调
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutIdleFunc(Display); //用于动画
//输入GLUT事件处理周期
glutMainLoop();
}
解决方案
p>
?
它对你的用例没有意义(并完全显示颜色值的反转)。您可能需要 GL_REPLACE
或 GL_MODULATE
。
i am using a ppm loader to set image as a background , but there is a problemin colors here is the code and the image that i am use .
here is the code .....
texture.h
#ifndef TEXTURE_H
#define TEXTURE_H
struct Image
{
unsigned char* pixels;
int width;
int height;
int numChannels;
};
class Texture
{
public:
Texture ();
void Prepare (int texN);
void ReadPPMImage (char *fn);
GLuint texName;
Image image;
};
#endif
texture.cpp
#include <fstream>
#include <glut.h>
#pragma warning (disable : 4996)
#include "Texture.h"
Texture::Texture ()
{
}
void Texture::Prepare (int texN)
{
texName = texN;
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture (GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width,
image.height, 0, GL_RGB, GL_UNSIGNED_BYTE,
image.pixels);
}
void Texture::ReadPPMImage (char* fn)
{
int tmpint;
char str[100];
FILE* inFile = fopen (fn,"rb");
if (inFile == NULL)
{
printf ("Can't open input file %s. Exiting.\n",fn);
exit (1);
}
fscanf (inFile,"P%d\n", &tmpint);
if (tmpint != 6)
{
printf ("Input file is not ppm. Exiting.\n");
exit (1);
}
// skip comments embedded in header
fgets (str,100,inFile);
while (str[0]=='#')
fgets(str,100,inFile);
// read image dimensions
sscanf (str,"%d %d",&image.width, &image.height);
fgets (str,100,inFile);
sscanf (str,"%d",&tmpint);
if (tmpint != 255)
printf("Warning: maxvalue is not 255 in ppm file\n");
image.numChannels = 3;
image.pixels = (unsigned char*) malloc (image.numChannels * image.width * image.height * sizeof (unsigned char));
if (image.pixels == NULL)
{
printf ("Can't allocate image of size %dx%d. Exiting\n", image.width, image.height);
exit (1);
}
else
printf("Reading image %s of size %dx%d\n", fn, image.width, image.height);
fread (image.pixels, sizeof (unsigned char), image.numChannels * image.width * image.height, inFile);
fclose (inFile);
}
Main.cpp
#include <glut.h>
#include "Texture.h"
#pragma warning (disable : 4996)
const float fMinX = -5.0, fMinY = -5.0, fNearZ = 1.0,
fMaxX = 5.0 , fMaxY = 5.0 , fFarZ = 10.0;
Texture ImageOne ;
void Init ()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable (GL_DEPTH_TEST);
glGenTextures (1, &ImageOne.texName);
ImageOne.ReadPPMImage("wood_1.ppm");
ImageOne.Prepare(1) ;
}
void Reshape (int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (fMinX, fMaxX, fMinY, fMaxY, fNearZ, fFarZ);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void Display ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_TEXTURE_2D);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBindTexture (GL_TEXTURE_2D, ImageOne.texName);
glBegin(GL_QUADS);
glTexCoord2f(0,1);
glVertex3f(-5.5,5,-6);
glTexCoord2f(0,0);
glVertex3f(-5.5,-5,-6);
glTexCoord2f(1,0);
glVertex3f(5,-5,-6);
glTexCoord2f(1,1);
glVertex3f(5,5,-6);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers ();
glFlush ();
}
void main (int argc, char **argv)
{
// init GLUT and create window
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow ("OpenGL - Rotating Cubes");
Init ();
// register callbacks
glutDisplayFunc (Display);
glutReshapeFunc (Reshape);
glutIdleFunc (Display); // used in animation
// enter GLUT event processing cycle
glutMainLoop();
}
解决方案
Why are you using
?
It does not make sense for your use case (and perfectly explays the "inversion" of the color values). You probably want GL_REPLACE
or GL_MODULATE
.
这篇关于OpenGl纹理.......... ppm背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!