Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- #include <iostream>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h>
- #include "GLee.h" //copy GLee.h and GLee.c into your project
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include "camera.h" //this doesn't changed at all
- #include <vector>
- camera cam;
- /* //this is for mip-mapping, it's not important here
- 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);
- 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);
- SDL_FreeSurface(img);
- SDL_FreeSurface(img2);
- return num;
- }*/
- unsigned int vbo,ind; //indicis
- float f[]={0,2,-4,1,0,0,-2,-2,-4,0,1,0,2,-2,-4,0,0,1}; //vertex array (3 component vertex, 3 component color)
- unsigned int indarr[]={0,1,2}; //index array
- 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);
- glGenBuffers(1,&vbo); //generate an index for the vertexbuffer
- glGenBuffers(1,&ind); //and the indices
- glBindBuffer(GL_ARRAY_BUFFER,vbo); //use vbo as ARRAY_BUFFER
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ind); //use ind as ELEMENT_ARRAY_BUFFER (index-array)
- glBufferData(GL_ARRAY_BUFFER,sizeof(f),f,GL_STATIC_DRAW);//fill up the array with vertex and color-data
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indarr),indarr,GL_STATIC_DRAW);//this one with indicis
- }
- void display()
- {
- glLoadIdentity();
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- cam.Control();
- cam.UpdateCamera();
- glBindBuffer(GL_ARRAY_BUFFER,vbo); //use this VBO
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ind); //and index
- glEnableClientState(GL_VERTEX_ARRAY); //we use vertex and
- glEnableClientState(GL_COLOR_ARRAY); //color data
- //the vertex has 3 component (x,y,z),it has the type float, 3 float is the vertex + 3 float is the color/6*sizeof(float), and the first
- //coordinate start at the begginning of the VBO
- glVertexPointer(3,GL_FLOAT,6*sizeof(float),0);
- //same thing
- glColorPointer(3,GL_FLOAT,6*sizeof(float),(void*)(3*sizeof(float)));
- glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0); //draw it
- glDisableClientState(GL_COLOR_ARRAY); //disable everything
- glDisableClientState(GL_VERTEX_ARRAY);
- glBindBuffer(GL_ARRAY_BUFFER,0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
- }
- 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;
- }
- 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));
- }
- glDeleteBuffers(1,&ind); //delete it
- glDeleteBuffers(1,&vbo);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement