0

我有枪的无背景图像。我正在尝试借助这把枪的 2D 图像来构建第一人称射击游戏。我已经在我的程序中加载了纹理,但我无法使枪的背景透明。此外,当我的图像已经没有背景时,我为什么还要这样做。我使用 clippingmagic.com 删除了我的图像的背景。代码是:

#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include<SOIL/SOIL.h>

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;
const float A = 1366.0; //width of the computer screen
const float B = 768.0;  //height of the computer screen

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
                    int x, int y) {    //The current mouse coordinates
    switch (key) {
        case 27: //Escape key
            exit(0); //Exit the program
    }
}

//Initializes 3D rendering
void initRendering() {
    //Makes 3D drawing work when something is in front of something else
    glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
    //Tell OpenGL how to convert from coordinates to pixel values
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

    //Set the camera perspective
    glLoadIdentity(); //Reset the camera
    gluPerspective(45.0,                  //The camera angle
                   (double)w / (double)h, //The width-to-height ratio
                   1.0,                   //The near z clipping coordinate
                   200.0);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
    //Clear information from last draw
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective
    glTranslatef(0.0,0.0,-10.0);
//DRAW WALL OF ROOM
    glBegin(GL_QUADS);      
        glColor3f(0.0, 1.0, 1.0);
        glVertex3f(-100.0, -100.0, -100.0);
        glVertex3f(-100.0,  100.0, -100.0);
        glVertex3f( 100.0,  100.0, -100.0);
        glVertex3f( 100.0, -100.0, -100.0);
    glEnd();

//DRAW GUN
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    GLuint gun_tex_id = SOIL_load_OGL_texture
    (
        "GUN.png",
        SOIL_LOAD_RGB,  //I even tried SOIL_LOAD_AUTO and SOIL_LOAD_RGBA but it didn't help
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y  | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_MULTIPLY_ALPHA
    );
    glBindTexture(GL_TEXTURE_2D, gun_tex_id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTranslatef(2.0,0.0,0.0);
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-4.1,-4.15, 0.0);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f( 3.0,-4.15, 0.0);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f( 3.0, 1.2, 0.0);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(-4.1, 1.2, 0.0);     
    glEnd();
    glDisable(GL_BLEND);
    glutSwapBuffers(); //Send the 3D scene to the screen
}

void myinit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0); /* gray background */

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho( -A/2, A/2, -B/2, B/2, -200, 200);
/* In World coordinates:position the "clipping rectangle" at -A/2, its right edge at +A/2, its bottom edge at -B/2 and its top edge at +B/2 */
  glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(A, B); //Set the window size

    //Create the window
    glutCreateWindow("My shooting game");
    initRendering(); //Initialize rendering

    //Set handler functions for drawing, keypresses, and window resizes
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);

    myinit();
    glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
    return 0; //This line is never reached
}
4

1 回答 1

0

您可以使用 glBlendFunc 函数来完成。在将纹理绑定到对象之前,请尝试添加以下代码片段。

glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

阅读内容以获取更多信息。

于 2014-04-08T13:24:06.927 回答