C++ tan 3d ekran koruyucu
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdio.h>
#include <math.h>
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;
GLint base,loop;
GLuint texture[14];
GLUquadricObj *Obiekt1;
GLfloat roll,roll2;
GLfloat step=0.01f;
GLfloat step2=0.3f;
GLfloat rotate, rotateobiekt1,rotateobiekt2;
GLfloat angle,rotsat;
GLfloat RotateWenus;
GLfloat x,y;
GLfloat LightAmbient[]= { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat LightDiffuse[]= { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat LightPosition[]= { 10.0f, -10.0f, 10.0f, 1.0f };
GLfloat LightAmbient2[]= { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat roo;
GLfloat ro;
GLfloat ro2;
GLfloat alpha;
GLfloat alphaplus=0.0003f;
char *text_display[7] =
{
" Ekran koruyucu
[email protected]",
" Hepiniz hos geldiniz...",
" Bu proje benim ilk ekran koruyucu calismamdir",
" umarim begenir dostlarinizada hediye edersiniz",
" SAYGILARIMLA !!!",
"
[email protected]",
""
};
int Tekst = 0;
int TextSync = 0;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void Build_font(void)
{
int i;
float cx;
float cy;
base = glGenLists(256);
glBindTexture(GL_TEXTURE_2D, texture[4]);
for (i=0 ; i<256 ; i++) {
cx = float(i%16)/16.0f;
cy = float(i/16)/16.0f;
glNewList(base+i,GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(cx,1-cy-0.0625f);
glVertex2i(0,0);
glTexCoord2f(cx+0.0625f,1-cy-0.0625f);
glVertex2i(16,0);
glTexCoord2f(cx+0.0625f,1-cy);
glVertex2i(16,16);
glTexCoord2f(cx,1-cy);
glVertex2i(0,16);
glEnd();
glTranslated(10,0,0);
glEndList();
}
}
void KillFont(void)
{
glDeleteLists(base, 256);
}
void glPrint(GLint x, GLint y, char* string, int set)
{
if (set > 1) set = 1;
glBindTexture(GL_TEXTURE_2D, texture[4]);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,640,0,480,-100,100);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslated(x,y,0);
glListBase(base-32+(128*set));
glCallLists(strlen(string),GL_BYTE,string);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
}
void Swiatlo(void)
{
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient2);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if (!Filename)
{
return NULL;
}
File=fopen(Filename,"r"
;
if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
int LoadTextures()
{
int Status = FALSE;
AUX_RGBImageRec *TextureImage[14];
memset(TextureImage, 0, sizeof(void *)*14);
if ( (TextureImage[0]=LoadBMP("kosmos.bmp"
) &&
(TextureImage[1]=LoadBMP("belka.bmp"
) &&
(TextureImage[2]=LoadBMP("ziemia.bmp"
)&&
(TextureImage[3]=LoadBMP("ksiezyc.bmp"
)&&
(TextureImage[4]=LoadBMP("fonty.bmp"
) &&
(TextureImage[5]=LoadBMP("mars.bmp"
) &&
(TextureImage[6]=LoadBMP("wenus.bmp"
) &&
(TextureImage[7]=LoadBMP("merkury.bmp"
) &&
(TextureImage[8]=LoadBMP("satelitemaska.bmp"
) &&
(TextureImage[9]=LoadBMP("satelite.bmp"
) &&
(TextureImage[10]=LoadBMP("saturnmaska.bmp"
) &&
(TextureImage[11]=LoadBMP("saturn.bmp"
) &&
(TextureImage[12]=LoadBMP("space.bmp"
) &&
(TextureImage[13]=LoadBMP("sun.bmp"
))
{
Status=TRUE;
glGenTextures(14, &texture[0]);
for(loop=0;loop<14;loop++)
{
glBindTexture(GL_TEXTURE_2D, texture[loop]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}
for(loop=0;loop<14;loop++)
{
if (TextureImage[loop])
{
if (TextureImage[loop]->data)
free(TextureImage[loop]->data);
free(TextureImage[loop]);
}
}
}
return Status;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
if(!LoadTextures())
return FALSE;
Swiatlo();
Build_font();
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
Obiekt1=gluNewQuadric();
gluQuadricTexture(Obiekt1, GLU_TRUE);
gluQuadricDrawStyle(Obiekt1, GLU_FILL);
return TRUE;
}
GLint DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,0,10, 0,0,0, 0,1,0);
glDisable(GL_LIGHTING);
glTranslatef(0,0,-1);
glRotatef(20,-1,0,0);
glRotatef(rotate,0,0,1);
glRotatef(rotate, 0,1,0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f , roll + 0.0f); glVertex3f(-10.1f, -10.1f, 0.0);
glTexCoord2f(6.0f , roll + 0.0f); glVertex3f(10.1f, -10.1f, 0.0);
glTexCoord2f(6.0f, roll + 6.0f); glVertex3f(10.1f, 10.1f, 0.0);
glTexCoord2f(0.0f, roll + 6.0f); glVertex3f(-10.1f, 10.1f, 0.0);
glEnd();
glEnable(GL_LIGHTING);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTranslatef(-2.6f,-1.8f,-5);
glRotatef(90,1,0,0);
glRotatef(rotateobiekt2,0,0,1);
gluSphere(Obiekt1, 0.9, 20,20);
glLoadIdentity();
glTranslatef(4.5,3.5,-10);
glBindTexture(GL_TEXTURE_2D, texture[7]);
glRotatef(rotateobiekt2,1,1,0);
gluSphere(Obiekt1, 0.1,20,20);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTranslatef(0,0,-4);
glTranslatef(0,-1.5,0);
glDisable(GL_LIGHT1);
glEnable(GL_LIGHT0);
glBegin(GL_QUADS);
glTexCoord2f(-roll2 +0.0f , 0.0); glVertex3f(-3.1f, -0.2f, 0.0);
glTexCoord2f(-roll2 +1.0f , 0.0); glVertex3f(3.1f, -0.2f, 0.0);
glTexCoord2f(-roll2 +1.0f, 1.0); glVertex3f(3.1f, 0.2f, 0.0);
glTexCoord2f(-roll2 +0.0f, 1.0); glVertex3f(-3.1f, 0.2f, 0.0);
glEnd();
glPrint(100,20,text_display[Tekst],1);
glDisable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTranslatef(0.0f,1.3f, -4.0f);
glRotatef(rotateobiekt1,0,1,0);
glRotatef(-75,1,0,0);
gluSphere(Obiekt1,0.7,20,20);
glTranslatef(1,0,0.5);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glRotatef(rotateobiekt1,1,1,1);
gluSphere(Obiekt1,0.1,20,20);
glLoadIdentity();
glDisable(GL_LIGHTING);
glTranslatef(0.60f, 0.35f,-2.8f);
glRotatef(20,0,0,1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
glBindTexture(GL_TEXTURE_2D, texture[10]);
glScalef(0.1f,0.1f,0.1f);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glBlendFunc(GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, texture[11]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glLoadIdentity();
glDisable(GL_LIGHTING);
glTranslatef(-0.7f,0.5f,-3.0f);
glRotatef(rotsat,0,0,1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
glBindTexture(GL_TEXTURE_2D, texture[8]);
glScalef(0.1f,0.1f,0.1f);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glBlendFunc(GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, texture[9]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glLoadIdentity();
glTranslatef(1.8f, 1.0f, -8);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glColor4f(1,1,1, alpha);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glRotatef(ro2,0,0,1);
glBindTexture(GL_TEXTURE_2D, texture[12]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glRotatef(ro,0,0,1);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glDisable(GL_BLEND);
glColor4f(1,1,1,1);
alpha+=alphaplus;
if(alpha>0.5)
alphaplus=-alphaplus;
if(alpha<0.0)
alphaplus=-alphaplus;
ro2+=0.1f;
ro+=0.05f;
glLoadIdentity();
glTranslatef(-0.7f, 0.7f, -1 );
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(0.9f, 0.1f, 0.9f, 1);
glBindTexture(GL_TEXTURE_2D, texture[13]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glColor4f(1,1,1,1);
glDisable(GL_BLEND);
glLoadIdentity();
glTranslatef(0.4f, -0.2f, -1);
glScalef(0.2f, 0.2f, 0.2f);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(0.1f, 0.99f, 0.4f, 0.5f);
glBindTexture(GL_TEXTURE_2D, texture[13]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glRotatef(roo,0,0,1);
glBindTexture(GL_TEXTURE_2D, texture[13]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0); glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0 , 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glColor4f(1,1,1,1);
glDisable(GL_BLEND);
roo+=0.1f;
if(roo>360)
roo=0.0f;
rotsat+=0.1f;
if(rotsat>360)
rotsat=0;
RotateWenus+=0.2f;
if(RotateWenus>360)
RotateWenus=0;
TextSync++;
if (TextSync > 600)
{
TextSync = 0;
Tekst++;
}
if (Tekst > 6)
Tekst = 0;
rotateobiekt1+=step2;
rotateobiekt2+=0.05f;
rotate+=step;
if(rotate>12)
step=-step;
if(rotate<-12)
step=-step;
roll+=0.002f;
if(roll>1.0f)
roll-=1.0f;
roll2+=0.0005f;
if(roll2>1.0f)
roll2-=1.0f;
return TRUE;
}
GLvoid KillGLWindow(GLvoid)
{
gluDeleteQuadric(Obiekt1);
KillFont();
if (fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(TRUE);
}
if (hRC)
{
if (!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL,"Release Of DC And RC Failed.","Kapatma Hatası",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC))
{
MessageBox(NULL,"Release Rendering Context Failed.","Kapatma Hatası",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL;
}
if (hDC && !ReleaseDC(hWnd,hDC))
{
MessageBox(NULL,"Release Device Context Failed.","Kapatma Hatası",MB_OK | MB_ICONINFORMATION);
hDC=NULL;
}
if (hWnd && !DestroyWindow(hWnd))
{
MessageBox(NULL,"Could Not Release hWnd.","Kapatma Hatası",MB_OK | MB_ICONINFORMATION);
hWnd=NULL;
}
if (!UnregisterClass("OpenGL",hInstance))
{
MessageBox(NULL,"Could Not Unregister Class.","Kapatma Hatası",MB_OK | MB_ICONINFORMATION);
hInstance=NULL;
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;
fullscreen=fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";
if (!RegisterClass(&wc))
{
MessageBox(NULL,"Failed To Register The Window Class.","Hata",MB_OK|MB_ICONEXCLAMA TION);
return FALSE;
}
if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSett ings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dmScreenSettings,CDS_F ULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)