Summer Vacation HW about openGL(include DEVIL & GLew)

Just Code use openGL Jpeg Multitextiue & Keyboard Mouse Contrl
If you are interested or have any questions , you can leave a message to me .


#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

#include <gl/glew.h>
#include <gl/wglew.h>
#pragma comment(lib,"glew32.lib")

#include <il/il.h>
#include <il/ilu.h>
#include <il/ilut.h>
#pragma comment(lib,"DevIL.lib")
#pragma comment(lib,"ILU.lib")
#pragma comment(lib,"ILUT.lib")

#include <gl/glut.h>
#pragma comment(lib,"glut32.lib")

#include "bitmap.h"
using namespace std;  

GLfloat environmetnt_light[] = {0.5f , 0.5f , 0.5f , 0.5f};
GLfloat specular_r[] = {0.6f , 0.6f , 0.6f , 0.6f};
GLfloat ambient[] = {1.0f , 1.0f , 1.0f , 1.0f};
GLfloat ambient_ear[] = {0.9f , 0.45f , 0.0f , 1.0f};
GLfloat diffuse[] = {1.0f , 1.0f , 1.0f , 1.0f};
GLfloat diffuse_ear[] = {0.9f , 0.45f , 0.0f , 1.0f};
GLfloat specular[] = {1.0f , 1.0f , 1.0f , 1.0f};
GLfloat specular_ear[] = {0.9f , 0.65f , 0.2f , 1.0f};
GLfloat lightpos_0[] = {0.0f , 40.0f , 300.0f , 1.0f};
GLfloat lightpos_1[] = {0.0f , 40.0f , -300.0f , 1.0f};
GLfloat fogcolor[] = {0.8f, 0.8f, 1.0f, 0.0f};

GLuint texture[2];

GLfloat teapotRotate = 0.0f;

GLfloat keyboard_Scale = 1.0f,
        x_position = 0.0f,
        y_position = 0.0f,
        z_position = 0.0f;

int old_rot_x=0;   //剛按下滑鼠時的視窗座標
int old_rot_y=0;
int rot_x=0;      //拖曳後的相對座標,用這決定要旋轉幾度
int rot_y=0;
int record_x=0;      //紀錄上一次旋轉的角度
int record_y=0;

int Mscale = 4;

void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
        case ‘=’ :
            keyboard_Scale += 0.1;
            break;
        case ‘-‘ :
            keyboard_Scale -= 0.1;
            break;
        case ‘a’ :
            x_position += 10;
            break;
        case ‘z’ :
            x_position -= 10;
            break;
        case ‘s’ :
            y_position += 10;
            break;
        case ‘x’ :
            y_position -= 10;
            break;
        case ‘d’ :
            z_position += 10;
            break;
        case ‘c’ :
            z_position -= 10;
            break;
    }
    glutPostRedisplay();
}

void Mouse(int button, int state, int x, int y)
{
   if(state)
   {
      record_x += x – old_rot_x;
      record_y += y – old_rot_y;
      rot_x = 0;   //沒有歸零會有不理想的結果
      rot_y = 0;
   }
   else
   {
      old_rot_x = x;
      old_rot_y = y;
   }
}

void MotionMouse(int x, int y)
{
   rot_x = x – old_rot_x;
   rot_y = y – old_rot_y;
   glutPostRedisplay();
}
void fog_creat()
{
    glFogfv(GL_FOG_COLOR , fogcolor);
    glFogf(GL_FOG_START , 500.0f);
    glFogf(GL_FOG_END , 600.0f);
    glFogi(GL_FOG_MODE, GL_LINEAR);
    glFogf(GL_FOG_DENSITY , 0.025);
    glHint(GL_FOG_HINT, GL_NICEST);
    glEnable(GL_FOG);
}

void CreateMAP()
{
    //tex
    ILuint ImgId;
    ilGenImages(1, &ImgId);
    ilBindImage(ImgId);

    //About MAP
    // Load image
    if(!ilLoadImage("Tex01.jpg")) {
        cerr << "Could not load " <<endl;
        return;
    }
   
    unsigned int width = ilGetInteger(IL_IMAGE_WIDTH);
    unsigned int height = ilGetInteger(IL_IMAGE_HEIGHT);
    unsigned int bpp = ilGetInteger(IL_IMAGE_FORMAT);

    ILubyte *imageData = ilGetData();

    // Generate a texture id
    glGenTextures(1,&texture[0]);
       
    // Bind the texture
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Set texture data
    if(bpp == IL_RGB) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
    } else if(bpp == IL_RGBA) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
    }

    //About Bump
    // Load image
    ilGenImages(1, &ImgId);
    ilBindImage(ImgId);

    if(!ilLoadImage("Bump01.jpg")) {
        cerr << "Could not load " <<endl;
        return;
    }
   
    width = ilGetInteger(IL_IMAGE_WIDTH);
    height = ilGetInteger(IL_IMAGE_HEIGHT);
    bpp = ilGetInteger(IL_IMAGE_FORMAT);

    imageData = ilGetData();

    // Generate a texture id
    glGenTextures(1,&texture[1]);
       
    // Bind the texture
    glBindTexture(GL_TEXTURE_2D, texture[1]);
   
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Set texture data
    if(bpp == IL_RGB) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
    } else if(bpp == IL_RGBA) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
    }
   
    glEnable(GL_TEXTURE_2D);
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

}

void changesize(GLsizei w,GLsizei h)
{
    GLfloat faspect;
    if(h == 0)
        h=1;
    glViewport(0,0,w,h);
    faspect = (GLfloat)w/(GLfloat)h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective (30.0f, faspect, 1.0, 2000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

   
}

void SetupRC(void)
{
    ilInit();
    iluInit();
    ilutRenderer(ILUT_OPENGL);
    ilEnable(IL_ORIGIN_SET);

    glewInit();

    glShadeModel(GL_SMOOTH);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glEnable(GL_DEPTH_TEST);
   
    //fog
    fog_creat();
   
    //envoronment light
    glEnable(GL_LIGHTING);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT , environmetnt_light);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
    glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
    glLightfv(GL_LIGHT0 , GL_POSITION , lightpos_0);

    glEnable(GL_LIGHT1);
    glLightfv(GL_LIGHT1,GL_AMBIENT,ambient);
    glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuse);
    glLightfv(GL_LIGHT1,GL_SPECULAR,specular);
    glLightfv(GL_LIGHT1 , GL_POSITION , lightpos_1);

    //material state
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT , GL_AMBIENT_AND_DIFFUSE);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular_ear);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient_ear);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_ear);
   
    CreateMAP();
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);

    glClearColor(0.9f, 0.9f, 0.9f, 0.5f);
}

void Scene(void)
{
   
   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    gluLookAt(0 + x_position, 60 + y_position, 500 + z_position, 0, 0, 320, 0, 1, 0);   
   
    glScalef(1 * keyboard_Scale, 0.03 * keyboard_Scale , 1 * keyboard_Scale);
    glColor3f(0.4f , 0.5f , 0.7f);
    glRotatef(teapotRotate , 0.0f , 1.0f , 0.0f);
    glRotatef((float)rot_y+(float)record_y, 1.0, 0.0, 0.0);//以x軸當旋轉軸
    glRotatef((float)rot_x+(float)record_x, 0.0, 1.0, 0.0);//以y軸當旋轉軸
    glTranslatef(-512.0f , 0.0f , -512.0f);
   
    //Module
    //Load Map
    BITMAPINFO *Info; // Texture bitmap information
    GLubyte *Bits; // Texture bitmap pixel bits
    Bits = LoadDIBitmap("Map01.bmp", &Info);

    int i, j, k, l, m, n, o;
    bool bswitchsides = false;

    float x, y, a, b, c, d;
   
    //描繪地形
   
    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,  texture[0]);

    glActiveTexture(GL_TEXTURE2);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,  texture[1]);
   

    /*glBindTexture(GL_TEXTURE_2D, texture[0]);*/

    glEnable(GL_RESCALE_NORMAL);
   
    glBegin(GL_TRIANGLE_STRIP);
       
    for(i = 0 ; i < 255 ; i++)
    {
        if(bswitchsides)
        {
            for(j = 255; j >= 0; j–)
            {
                x = i;
                y = j;
               
                k = Bits[i * Info->bmiHeader.biWidth * 3 + j * 3 ] ;
                m = Bits[(i + 1 ) * Info->bmiHeader.biWidth * 3  + j * 3  ];
               
                b = x / 256;
                a = y / 256;
                d = (x + 1) / 256;
                c = y / 256;
               
                glMultiTexCoord2f(GL_TEXTURE0, a, b);
                glMultiTexCoord2f(GL_TEXTURE1, a, b);
                glVertex3f(i * Mscale, k, j * Mscale);

                glMultiTexCoord2f(GL_TEXTURE0, c, d);
                glMultiTexCoord2f(GL_TEXTURE1, c, d);
                glVertex3f((i + 1) * Mscale, m, j * Mscale);
            }
        }
        else
        {
            for(j = 0 ; j <= 255 ; j++)
            {
                x = i;
                y = j;
               
                k = Bits[i * Info->bmiHeader.biWidth * 3 + j * 3 ] ;
                m = Bits[(i + 1 ) * Info->bmiHeader.biWidth * 3  + j * 3  ];
               
                b = x / 256;
                a = y / 256;
                d = (x + 1) / 256;
                c = y / 256;

                glMultiTexCoord2f(GL_TEXTURE0, c, d);
                glMultiTexCoord2f(GL_TEXTURE1, c, d);
                glVertex3f((i + 1) * Mscale, m, j * Mscale);

                glMultiTexCoord2f(GL_TEXTURE0, a, b);
                glMultiTexCoord2f(GL_TEXTURE1, a, b);
                glVertex3f(i * Mscale, k, j * Mscale);
            }
        }

        bswitchsides = !bswitchsides;
    }
   
    glEnd();
   
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
}

void rotatefunc(int value)
{
    teapotRotate += 0.5f;
    if(teapotRotate>360.0f)
        teapotRotate=0.0f;
    glutPostRedisplay();
    glutTimerFunc(50 , rotatefunc , 1);
}

void main(void)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN)/2-300) , (GetSystemMetrics(SM_CYSCREEN)/2-300));
    glutInitWindowSize(1000,800);
    glutCreateWindow("test_3D");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(Mouse);
    glutMotionFunc(MotionMouse);
    glutDisplayFunc(Scene);
    glutReshapeFunc(changesize);
    glutTimerFunc(33 , rotatefunc , 1);
    SetupRC();
    glutMainLoop();
}


*分享有助於成長*

Advertisements

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s