1. 程式人生 > >opengl利用SOIL載入圖片dds紋理

opengl利用SOIL載入圖片dds紋理

本來網上有簡單的載入圖片 dds紋理的例子,不過不支援minimap?找了一個簡單的c語言庫。。。沒辦法,看c++的類我就暈,不然ogl sample pack也不錯的

https://github.com/kbranigan/Simple-OpenGL-Image-Library

自行下載編譯,codeblocks+mingw visual c++的專案檔案都有的,自己編譯成庫,然後複製SOIL.h到你用便的include目錄,現在就簡單多了,載入紋理圖片dds紋理

#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL.h>

GLFWwindow* window;
GLint width,height;
GLfloat ratio = 1.0f;
static char msg[128] = {0};
GLfloat xpos,ypos;
GLuint tex_ID;
GLfloat xrot=0.0f;
GLfloat yrot=0.0f;
GLfloat zrot=0.0f;

static void error_callback(int error, const char* description)
{
    return;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (action != GLFW_PRESS)
        return;
    switch (key)
    {
    case GLFW_KEY_ESCAPE:
        glfwSetWindowShouldClose(window, GL_TRUE);
        break;
    case GLFW_KEY_D:
        xrot+=0.5f;
        break;
    case GLFW_KEY_A:
        xrot-=0.5f;
        break;
    case GLFW_KEY_W:
        yrot+=0.5f;
        break;
    case GLFW_KEY_S:
        yrot-=0.5f;
        break;
    default:
        break;
    }
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (action == GLFW_PRESS) switch(button)
        {
        case GLFW_MOUSE_BUTTON_LEFT:
            strcpy(msg,"Mosue left button clicked!");
            break;
        case GLFW_MOUSE_BUTTON_MIDDLE:
            strcpy(msg,"Mosue middle button clicked!");
            break;
        case GLFW_MOUSE_BUTTON_RIGHT:
            strcpy(msg,"Mosue right button clicked!");
            break;
        default:
            return;
        }
    return;
}
void cursor_position_callback(GLFWwindow* window, double x, double y)
{
    sprintf(msg,"Mouse position move to [%d:%d]",int(x),int(y));
    xpos=float((x-width/2)/width)*2;
    ypos=float(0-(y-height/2)/height)*2;
    return;
}
void scroll_callback(GLFWwindow* window, double x, double y)
{
    return;
}
void framebuffer_size_callback(GLFWwindow* window, int w, int h)
{
    if (height==0)                                      // Prevent A Divide By Zero By
    {
        height=1;                                        // Making Height Equal One
    }
    if (h > 0)
        ratio = (float) w / (float) h;
    glViewport(0, 0, w, h); // Setup viewport
    width = w;
    height = h;
    glViewport(0,0,width,height);                        // Reset The Current Viewport
    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                    // Reset The Projection Matrix
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,ratio,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
    glLoadIdentity();
}
void render_scene(GLFWwindow* window)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-0.0f,-0.0f,-4.0f);
    glEnable(GL_TEXTURE_2D);
    glRotatef(xrot,1.0f,0.0f,0.0f);
    glRotatef(yrot,0.0f,1.0f,0.0f);
    glRotatef(zrot,0.0f,0.0f,1.0f);
    glBindTexture(GL_TEXTURE_2D, tex_ID);
    glBegin(GL_QUADS);
    // Front Face
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);      // Bottom Left Of The Texture and Quad
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f,  1.0f);      // Bottom Right Of The Texture and Quad
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f,  1.0f);      // Top Right Of The Texture and Quad
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f,  1.0f);     // Top Left Of The Texture and Quad
    // Back Face
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);       // Bottom Right Of The Texture and Quad
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);      // Top Right Of The Texture and Quad
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);      // Top Left Of The Texture and Quad
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);       // Bottom Left Of The Texture and Quad
    // Top Face
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);      // Top Left Of The Texture and Quad
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f,  1.0f,  1.0f);     // Bottom Left Of The Texture and Quad
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f,  1.0f,  1.0f);      // Bottom Right Of The Texture and Quad
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);      // Top Right Of The Texture and Quad
    // Bottom Face
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);       // Top Right Of The Texture and Quad
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);       // Top Left Of The Texture and Quad
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f,  1.0f);      // Bottom Left Of The Texture and Quad
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);      // Bottom Right Of The Texture and Quad
    // Right face
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);       // Bottom Right Of The Texture and Quad
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);      // Top Right Of The Texture and Quad
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f,  1.0f);      // Top Left Of The Texture and Quad
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f,  1.0f);      // Bottom Left Of The Texture and Quad
    // Left Face
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);       // Bottom Left Of The Texture and Quad
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);      // Bottom Right Of The Texture and Quad
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f,  1.0f);     // Top Right Of The Texture and Quad
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);      // Top Left Of The Texture and Quad
    glEnd();
    glDisable(GL_TEXTURE_2D);
    xrot+=0.3f;
    yrot+=0.3f;
    zrot+=0.3f;
}
void init_opengl(void)
{
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                // Black Background
    glClearDepth(1.0f);                                    // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
}
int main( int argc,char *argv[] )
{
    glfwSetErrorCallback(error_callback);
    if( !glfwInit() )
    {
        return 0;
    }
    window = glfwCreateWindow( 640, 480, "opengl tutorial 003-texture box", NULL, NULL);
    if( window == NULL )
    {
        glfwTerminate();
        return 0;
    }
    glfwSetKeyCallback(window, key_callback);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetMouseButtonCallback(window, mouse_button_callback);
    glfwSetCursorPosCallback(window, cursor_position_callback);
    glfwSetScrollCallback(window, scroll_callback);
    glfwMakeContextCurrent(window);
    glfwGetFramebufferSize(window, &width, &height);
    framebuffer_size_callback(window, width, height);
    glewExperimental = true; // Needed for core propbmpfile
    if (glewInit() != GLEW_OK)
    {
        return 0;
    }
    //initialize opengl
    init_opengl();

    tex_ID = SOIL_load_OGL_texture("demo.dds",SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS
                                   //| SOIL_FLAG_MULTIPLY_ALPHA
                                   //| SOIL_FLAG_COMPRESS_TO_DXT
                                   | SOIL_FLAG_DDS_LOAD_DIRECT
                                   //| SOIL_FLAG_NTSC_SAFE_RGB
                                   //| SOIL_FLAG_CoCg_Y
                                   //| SOIL_FLAG_TEXTURE_RECTANGLE
                                  );
    while(!glfwWindowShouldClose(window))
    {
        render_scene(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glDeleteTextures(1,&tex_ID);
    glfwTerminate();
    return 0;
}