OpenGL纹理............ ppm背景

Posted

技术标签:

【中文标题】OpenGL纹理............ ppm背景【英文标题】:OpenGl texturing ........ ppm background 【发布时间】:2013-12-28 20:35:57 【问题描述】:

我正在使用 ppm 加载程序将图像设置为背景,但出现了问题 这里的颜色是我使用的代码和图像。

http://imgur.com/w732d6j

http://imgur.com/mJr26Ik

这里是代码.....

纹理.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

纹理.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();

【问题讨论】:

【参考方案1】:

你为什么要使用

glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

?

这对您的用例没有意义(并且完美地展示了颜色值的“反转”)。您可能想要GL_REPLACEGL_MODULATE

【讨论】:

以上是关于OpenGL纹理............ ppm背景的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL ES纹理

OpenGL-纹理(下)

opengl使用bmp纹理映射画不出东西

opengl 把纹理映射到立方体的六个面。

Windows 上的 OpenCL/OpenGL 纹理互操作:调整 OpenGL 纹理的大小

如何将 OpenGL 纹理转换为 CUDA 纹理?