OpenGl texturing … ppm background

旧时模样 提交于 2019-12-12 02:35:22

问题


i am using a ppm loader to set image as a background , but there is a problem in colors here is the code and the image that i am use .

http://imgur.com/w732d6j

http://imgur.com/mJr26Ik

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();
}

回答1:


Why are you using

glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

?

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.



来源:https://stackoverflow.com/questions/20819094/opengl-texturing-ppm-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!