custom opengl lighting class
Main.cpp
/* *Author from JP.Lee *This is first OpenGL Shader preview framwork via SDL api. *5/19/2013 first update. */ #includeTexture.h#include "Light.h" #include "Texture.h" #include "SDL.h" #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include #endif //Platform Defined #if defined(__APPLE__) && defined(__MACH__) #include #include #else #include #include #endif const GLsizei windowWidth = 1280; const GLsizei windowHeight = 720; GLfloat viewFOVY = 45.0f; GLfloat cubePositionZ = -5.0f; GLfloat cubeRotateX = 45.0f; GLfloat cubeRotateY = 45.0f; Uint8 *keys = NULL; Light *mainLight = NULL; Texture *texture = NULL; GLvoid establisheProjectionMatrix(GLsizei width, GLsizei height) { glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //first setting up make perspective view. //*************FOV gluPerspective((GLfloat)viewFOVY , (GLfloat)width / (GLfloat)height, 0.1f , 200.0f); } GLvoid initGL(GLsizei width , GLsizei height) { Light::Initialize(); establisheProjectionMatrix(width , height); glShadeModel(GL_SMOOTH); glClearColor(0.0f , 0.0f , 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST); glEnable(GL_PERSPECTIVE_CORRECTION_HINT); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); mainLight = new Light(LIGHT_SPOT); mainLight -> setDiffuse(1.0 , 0.5 , 0.5 , 1.0); mainLight -> setPosition(0 , 5 , 0); texture = new Texture("sample.tga" , "Surface Texture"); } GLvoid displayFPS(GLvoid) { static long lastTime = SDL_GetTicks(); static long loops = 0; static GLfloat fps = 0.0f; int newTime = SDL_GetTicks(); if (newTime - lastTime > 100) { float newFPS = (float)loops / float(newTime-lastTime)* 1000.0f; fps = (fps+ newFPS) / 2.0f; char title[80]; sprintf_s(title, "JP OpenGL VIEWER - %f" , fps); SDL_WM_SetCaption(title , NULL); lastTime = newTime; loops = 0; } loops++; } GLvoid drawScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glTranslatef(0 , 0 , -5.0f); //offset to -z 5.0 glTranslatef(0,0,cubePositionZ); glRotatef(cubeRotateX , 1 , 0 , 0); //45 degree rotation X glRotatef(cubeRotateY , 0 , 1 , 0); //45 degree rotation +Y glColor3f(1.0f , 1.0f , 1.0f); //ÅؽºÃĸ¦ ¿¬°á ½ÃŲ´Ù. glBindTexture(GL_TEXTURE_2D , texture->texID); //À°¸éü¸¦ ±×¸³´Ï´Ù glBegin(GL_QUADS); //top face //glColor3f(1.0f , 0.5f , 0.0f); glNormal3f(0,1,0); glTexCoord2f(1.0f , 1.0f); glVertex3f( 1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , 1.0f , 1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f( 1.0f , 1.0f , 1.0f); //bottom face //glColor3f(0.0f , 1.0f , 0.0f); glNormal3f(0,-1,0); glTexCoord2f(1.0f , 1.0f); glVertex3f( 1.0f ,-1.0f ,-1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f ,-1.0f ,-1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f ,-1.0f , 1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f( 1.0f ,-1.0f , 1.0f); //front face //glColor3f(1.0f , 0.0f , 0.0f); glNormal3f(0,0,1); glTexCoord2f(1.0f , 1.0f); glVertex3f( 1.0f , 1.0f , 1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f ,-1.0f , 1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f( 1.0f ,-1.0f , 1.0f); //back face //glColor3f(1.0f , 1.0f , 0.0f);// glNormal3f(0,0,-1); glTexCoord2f(1.0f , 1.0f); glVertex3f( 1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f ,-1.0f ,-1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f( 1.0f ,-1.0f ,-1.0f); //left face //glColor3f(0.0f , 0.0f , 1.0f);//red glNormal3f(1,0,0); glTexCoord2f(1.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f ,-1.0f ,-1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f(-1.0f ,-1.0f , 1.0f); //right face //glColor3f(1.0f , 0.0f , 1.0f); glNormal3f(-1,0,0); glTexCoord2f(1.0f , 1.0f); glVertex3f( 1.0f , 1.0f , 1.0f); glTexCoord2f(0.0f , 1.0f); glVertex3f( 1.0f , 1.0f ,-1.0f); glTexCoord2f(0.0f , 0.0f); glVertex3f( 1.0f ,-1.0f ,-1.0f); glTexCoord2f(1.0f , 0.0f); glVertex3f( 1.0f ,-1.0f , 1.0f); glEnd(); glFlush(); SDL_GL_SwapBuffers(); displayFPS(); } GLboolean checkKeys(GLvoid) { const GLfloat speed = 1.0f; const GLfloat zoom = 1.0f; const GLfloat sensitive = 5.0f; if ( keys[ SDLK_ESCAPE ]) return true; if ( keys[SDLK_LEFT]) cubeRotateY -= speed; if(keys[SDLK_RIGHT]) cubeRotateY += speed; if(keys[SDLK_UP]) cubeRotateX -= speed; if(keys[SDLK_DOWN]) cubeRotateX += speed; if(keys[SDLK_HOME]) cubePositionZ -= zoom/sensitive; if(keys[SDLK_END]) cubePositionZ += zoom/sensitive; return false; } int main (int argc , char *argv[]) { if ( SDL_Init(SDL_INIT_VIDEO) < 0) { } if (SDL_SetVideoMode( windowWidth , windowHeight , 0 , SDL_OPENGL) == NULL) { } initGL(windowWidth , windowHeight); int done = 0; while ( !done ) { drawScene(); SDL_Event event; while (SDL_PollEvent(&event)) { if ( event.type == SDL_QUIT ) { done = 1; } keys = SDL_GetKeyState(NULL); } if ( checkKeys()) done = 1; } SDL_Quit(); return 1; }
#ifndef TEXTURE_H #define TEXTURE_H #includeTexture.cpp#ifndef WIN32 #define WIN32_LEAN_AND_MEAN #include #endif //Platform Defined #if defined(__APPLE__) && defined(__MACH__) #include #include #else #include #include #endif #include #include #include using std::string; using std::vector; using std::ifstream; struct TGA_Header { GLubyte ID_Length; GLubyte ColorMapType; GLubyte ImageType; GLubyte ColorMapSpecification[5]; GLshort xOrigin; GLshort yOrigin; GLshort ImageWidth; GLshort ImageHeight; GLubyte pixelDepth; }; class Texture { //Functions public: Texture(string filename , string name = ""); ~Texture(); //Variables public: unsigned char *imageData; unsigned int bpp; unsigned int width; unsigned int height; unsigned int texID; string name; static vector textures; private: bool loadTGA(string filename); bool creatTexture(unsigned char *imageData , int width , int height, int type); }; #endif
/* *Author from JP.Lee *This is first OpenGL Shader preview framwork via SDL api. *5/19/2013 first update. */ #include "Texture.h" vectorLight.hTexture::textures; Texture::Texture(string in_filename , string in_name) { imageData = NULL; loadTGA(in_filename); name = in_name; textures.push_back(this); } Texture::~Texture() { for( vector ::iterator it = textures.begin(); it != textures.end(); it++) if((*it) == this) textures.erase(it); if(imageData) delete imageData; } bool Texture::loadTGA(string filename) { TGA_Header TGAheader; ifstream file( filename.data(), std::ios_base::binary); if( !file.is_open()) { return false; } if(!file.read( (char *)&TGAheader , sizeof(TGAheader))) { return false; } if ( TGAheader.ImageType !=2) { return false; } width = TGAheader.ImageWidth; height = TGAheader.ImageHeight; bpp = TGAheader.pixelDepth; if ( width <= 0 || height <= 0 || (bpp !=24 && bpp != 32)) { return false; } GLuint type = GL_RGBA; if ( bpp == 24) { type = GL_RGB; } GLuint bytesPerPixel = bpp / 8; GLuint imageSize = width * height * bytesPerPixel; imageData = new GLubyte[imageSize]; //À̹ÌÁöµ¥ÀÌÅÍ¿¡ ¾Æ¹«°Íµµ ¾øÀ¸¸é ¾Æ¹«°Íµµ ¹Ýȯ ÇÏÁö ¾ÊÀ½ if (imageData == NULL ) { return false; } // if (!file.read (( char *)imageData, imageSize)) { delete imageData; return false; } //BGR ü³ÎÀ» ½º¿Ò Çؼ RGB ·Î º¯°æ for ( GLuint i = 0; i < (int)imageSize; i += bytesPerPixel ) { GLuint temp = imageData[i]; imageData[i] = imageData[i + 2]; //B to R imageData[i + 2] = temp; //R to B } creatTexture(imageData , width , height , type); return true; } bool Texture::creatTexture(unsigned char *imageData , int width , int height, int type) { glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0 , type , width , height, 0 , type , GL_UNSIGNED_BYTE , imageData); return true; }
/* *Author from JP.Lee *This is first OpenGL Shader preview framwork via SDL api. *5/19/2013 first update. */ #ifndef LIGHT_H #define LIGHT_H #includeLight.cpp#ifndef WIN32 #define WIN32_LEAN_AND_MEAN #include #endif //Platform Defined #if defined(__APPLE__) && defined(__MACH__) #include #include #else #include #include #endif enum LIGHT_TYPE { LIGHT_SPOT, LIGHT_POINT, LIGHT_DIRECTIONAL }; #include using std::vector; class Light { public: static void Initialize(void); Light(LIGHT_TYPE lighttype); ~Light(); void Visible(bool value = true); void setDiffuse(float r,float g,float b,float a); void setAmbient(float r,float g,float b,float a); void setSpecular(float r,float g,float b,float a); void setLightType(LIGHT_TYPE lightType); void setPosition(float x , float y , float z); void setSpotDirection(float x , float y , float z); void setCutoff(float value); void setExponent(float value); void setAttenuation(float constant , float linear , float quadratic); int getLightNum(void); void updateLight(void); static int numLights; static vector availableLights; static vector lights; private: GLfloat position[4]; GLfloat diffuse[4]; GLfloat ambient[4]; GLfloat specular[4]; GLfloat spotDirection[4]; float cutoff; float exponent; bool visible; int lightType; int lightNum; }; #endif
/* /* *Author from JP.Lee *This is first OpenGL Shader preview framwork via SDL api. *5/19/2013 first update. */ #include "Light.h" #includeGame Developer Leegoon copyright all right reserved since 2010.using std::vector; int Light::numLights = 0; vector Light::availableLights; vector Light::lights; void Light::Initialize(void) { glGetIntegerv(GL_MAX_LIGHTS, &numLights); for ( int i = 0; i < numLights; i++) { availableLights.push_back(GL_LIGHT0 + i); } } Light::Light(LIGHT_TYPE type) { lights.push_back(this); if ( (int)availableLights.size() > 0 ) { lightNum = availableLights[0]; availableLights.erase( availableLights.begin()); Visible(true); setLightType(type); setPosition(0,0,0); setSpotDirection(0,-1,0); setCutoff(45); setExponent(12); setAmbient(0,0,0,1); setDiffuse(0,0,0,1); setSpecular(0,0,0,1); updateLight(); } else { lightNum = 0; Visible(false); } } Light::~Light() { if( lightNum !=0) availableLights.push_back(lightNum); for (vector ::iterator it = lights.begin(); it != lights.end(); it++) { if ( (*it) == this ) { lights.erase(it); } } } void Light::Visible(bool value) { visible = value; if(visible == true) { glEnable(lightNum); } else { glDisable(lightNum); } } void Light::setDiffuse(float r,float g,float b,float a) { diffuse[0] = r; diffuse[1] = g; diffuse[2] = b; diffuse[3] = a; glLightfv(lightNum , GL_DIFFUSE , diffuse); } void Light:: setAmbient(float r,float g,float b,float a) { ambient[0] = r; ambient[1] = g; ambient[2] = b; ambient[3] = a; glLightfv(lightNum , GL_AMBIENT , ambient); } void Light::setSpecular(float r,float g,float b,float a) { specular[0] = r; specular[1] = g; specular[2] = b; specular[3] = a; glLightfv(lightNum , GL_SPECULAR , specular); } void Light::setLightType(LIGHT_TYPE type) { lightType = type; if( lightType == LIGHT_SPOT) { position[3] = 1.0f; } else if (lightType == LIGHT_POINT) { position[3] = 1.0f; setCutoff(180.0f); } else if (lightType == LIGHT_DIRECTIONAL) { position[3] = 0.0f; } updateLight(); } void Light::setPosition(float x , float y , float z) { position[0] = x; position[1] = y; position[2] = z; glLightfv(lightNum , GL_POSITION , position); } void Light::setSpotDirection(float x , float y , float z) { spotDirection[0] = x; spotDirection[1] = y; spotDirection[2] = z; glLightfv(lightNum , GL_SPOT_DIRECTION , spotDirection); } void Light::setCutoff(float value) { cutoff = value; glLightf(lightNum , GL_SPOT_CUTOFF , cutoff); } void Light::setExponent(float value) { exponent = value; glLightf(lightNum , GL_SPOT_EXPONENT , exponent); } void Light::setAttenuation(float constant , float linear , float quadratic) { glLightf(lightNum , GL_CONSTANT_ATTENUATION , constant); glLightf(lightNum , GL_LINEAR_ATTENUATION , linear); glLightf(lightNum , GL_QUADRATIC_ATTENUATION , quadratic); } int Light::getLightNum(void) { return lightNum; } void Light::updateLight(void) { glLightfv(lightNum , GL_POSITION , position); glLightfv(lightNum , GL_SPOT_DIRECTION , spotDirection); }
Comments
Post a Comment
덧글쓰기 기능 있는거 아시죠? ㅋㅋ