Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //mip-map demonstration, press enter, to change the mipmapping
- //create an image called chess.jpg (with gimp: create a new image with size 512x512, go to filter/render/pattern/checkerboard, and set the size to 128,then save it)
- #include <iostream>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h> //you need SDL_image to compile this
- #include "GLee.h" //you may also need GLee
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include "camera.h" //the camera class from earlier
- #include <vector>
- camera cam;
- unsigned int loadTexture(const char* filename,bool generate=false)
- {
- unsigned int num;
- glGenTextures(1,&num);
- SDL_Surface* img=IMG_Load(filename);
- if(img==NULL)
- {
- std::cout << "img was not loaded" << std::endl;
- return -1;
- }
- SDL_PixelFormat form={NULL,32,4,0,0,0,0,0,0,0,0,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
- SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
- if(img2==NULL)
- {
- std::cout << "img2 was not loaded" << std::endl;
- return -1;
- }
- glBindTexture(GL_TEXTURE_2D,num);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- //opengl 1.4 way:
- //if(generate)
- // glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,GL_TRUE);
- //openGL 1.1 way:
- if(generate)
- gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,img2->w,img2->h,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
- else
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
- if(generate)
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- else
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- // if(generate)
- // glGenerateMipmap(GL_TEXTURE_2D); //OpenGL 3.0+ way
- SDL_FreeSurface(img);
- SDL_FreeSurface(img2);
- return num;
- }
- unsigned int myImg,myImg2,current;
- void init()
- {
- glClearColor(0,0,0,1);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(50,640.0/480.0,1,1000);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_DEPTH_TEST);
- current=myImg=loadTexture("chess.jpg",true);
- myImg2=loadTexture("chess.jpg",false);
- }
- void display()
- {
- glLoadIdentity();
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- cam.Control();
- cam.UpdateCamera();
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,current);
- glBegin(GL_QUADS);
- glTexCoord2f(100,0);
- glVertex3f(-200,-3,200);
- glTexCoord2f(0,0);
- glVertex3f(-200,-3,-200);
- glTexCoord2f(0,100);
- glVertex3f(200,-3,-200);
- glTexCoord2f(100,100);
- glVertex3f(200,-3,200);
- glEnd();
- }
- int main()
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_SetVideoMode(640,480,32,SDL_OPENGL);
- Uint32 start;
- SDL_Event event;
- bool running=true;
- init();
- bool b=false;
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- case SDLK_RETURN:
- if(current==myImg)
- current=myImg2;
- else
- current=myImg;
- break;
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- cam.mouseIn(true);
- break;
- }
- }
- display();
- SDL_GL_SwapBuffers();
- if(1000.0/30>SDL_GetTicks()-start)
- SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement