?? drawglscene.cpp
字號:
//////////////////////////////////////////////////////////////////////
// scene.cpp: implementation of the scene class.
//
//////////////////////////////////////////////////////////////////////
#include "scene.h"
//////////////////////////////////////////////////////////////////////
// Tu je vsetko co sa kresli
int size=256;
int size_old=1;
float t;
float delta_rot_x=0,delta_rot_y=0.f,delta_rot_z=20.f,rot_y=0,rot_x=0,rot_z;
float blend=0.8f;
int sposob=0,pocet_krokov=10;
void scene::RenderMotionBlur() // vykresli akumulacnu texturu na celu obrazovku o mensom jase ( jas - podla premennej blend)
{
glPushMatrix();
glDisable(GL_LIGHTING); // vypne svetlo
glDisable(GL_DEPTH_TEST); // vypneme z-buffer test
glDepthMask(0); // vypneme zapis do z-buffra
glBlendFunc(GL_SRC_ALPHA,GL_ONE); // kreslime na obrazovku texturu podla nastavenej alpha
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, tex[0]);
glColor4f(1, 1, 1, blend); // natavime alpha
glMatrixMode(GL_PROJECTION); // projekcia
glPushMatrix();
glLoadIdentity(); // Reset pohladu - nacita jednotkovu maticu
glOrtho(0, size, 0, size, -1, 1);
glMatrixMode(GL_MODELVIEW); // Modelview
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2i(0,0);
glTexCoord2f(1.0f, 0.0f); glVertex2i(size,0);
glTexCoord2f(1.0f, 1.0f); glVertex2i(size,size);
glTexCoord2f(0.0f, 1.0f); glVertex2i(0,size);
glEnd();
glMatrixMode(GL_PROJECTION); // projekcia
glPopMatrix();
glMatrixMode(GL_MODELVIEW); // Modelview
glEnable(GL_DEPTH_TEST);
glDepthMask(1);
glDisable(GL_BLEND);
glPopMatrix();
if(svetlo)glEnable(GL_LIGHTING); // zapne svetlo
}
void scene::DrawGLScene()
{
if(keys['L'] && keys[VK_CONTROL]){ english = !english; keys['L']=0; }
{
float _fps = fps;
if(keys[VK_SHIFT])_fps *= 2.0f;
if(keys['W'])delta_rot_x -= 30.0f/_fps;
if(keys['S'])delta_rot_x += 30.0f/_fps;
if(keys['A'])delta_rot_y -= 30.0f/_fps;
if(keys['D'])delta_rot_y += 30.0f/_fps;
if(keys['X'])delta_rot_z -= 30.0f/_fps;
if(keys['E'])delta_rot_z += 30.0f/_fps;
if(keys['W'] && keys['S']){delta_rot_x=0;}
if(keys['A'] && keys['D']){delta_rot_y=0;}
if(keys['E'] && keys['X']){delta_rot_z=0;}
if(keys['R'])
{
rot_x = 0;
rot_y = 0;
rot_z = 0;
}
rot_x += delta_rot_x/fps;
if(rot_x>360.f)rot_x-=360.f;
else if(rot_x<0.f)rot_x+=360.f;
rot_y += delta_rot_y/fps;
if(rot_y>360.f)rot_y-=360.f;
else if(rot_y<0.f)rot_y+=360.f;
rot_z += delta_rot_z/fps;
if(rot_y>360.f)rot_z-=360.f;
else if(rot_z<0.f)rot_z+=360.f;
}
if(keys['P']){ ciary = !ciary; keys['P']=0; }
if(ciary)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
if (keys['1'])size=2;
else if (keys['2'])size=4;
else if (keys['3'])size=8;
else if (keys['4'])size=16;
else if (keys['5'])size=32;
else if (keys['6'])size=64;
else if (keys['7'])size=128;
else if (keys['8'])size=256;
else if (keys['9'])size=512;
else if (keys['0'])size=1024;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //cisti buffer v aktualnom viewporte a to iba color a hlbkovy bit
glLoadIdentity();
Prepni_do_3D();
c->MoveWorld(fps);
if(keys['L']){ svetlo = !svetlo; keys['L']=0; }
if(svetlo)glEnable(GL_LIGHTING); // zapne svetlo
if(keys[VK_ADD])
{
if(motion_fps<10.f)motion_fps+=0.5f;
else motion_fps+=1.0f;
keys[VK_ADD]=0;
}
if(keys[VK_SUBTRACT])
{
if(motion_fps<11.f)
{
if(motion_fps>0.0f)motion_fps -=0.5f;
}
else motion_fps-=1.0f;
keys[VK_SUBTRACT]=0;
}
if(keys[VK_SPACE])
{
sposob++;
if(sposob>2)sposob=0;
keys[VK_SPACE]=0;
}
if(sposob==0 || sposob==1)
{
if(keys['M'])
{
blend+= 0.05f/fps;
if(blend>1.0f)blend=1.0f;
}
if(keys['N'])
{
blend-= 0.05f/fps;
if(blend<0.0f)blend=0.0f;
}
glRotatef(rot_x,1,0,0);
glRotatef(rot_y,0,1,0);
glRotatef(rot_z,0,0,1);
if( t_motion.GetTime() > (1.0f / motion_fps) ) // ak cas od posledneho pridania do akumulacnej textury je > ako perioda kreslenia do akumulacnej textury
{
t_motion.Reset();
glViewport(0, 0, size, size==1024?512:size);
RenderMotionBlur();
if(sposob==0)
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE); // kreslime na obrazovku texturu podla nastavenej alpha
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
GLfloat LightAmbient[] = { 0.2f*(1.f-blend), 0.2f*(1.f-blend), 0.2f*(1.f-blend), 0 };
GLfloat LightDiffuse[] = { 1.0f*(1.f-blend), 1.0f*(1.f-blend), 1.0f*(1.f-blend), 0 };
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, LightAmbient );
glLightfv ( GL_LIGHT0, GL_DIFFUSE, LightDiffuse );
glColor3f(1.f-blend, 1.f-blend, 1.f-blend);
glColorMask(0,0,0,0);
_6kriz->Render();
glColorMask(1,1,1,1);
_6kriz->Render();
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
else
{
GLfloat LightAmbient[] = { 0.2f, 0.2f, 0.2f, 0 };
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 0 };
glColor3f(1.f, 1.f, 1.f);
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, LightAmbient );
glLightfv ( GL_LIGHT0, GL_DIFFUSE, LightDiffuse );
_6kriz->Render();
}
timer1.Reset();
if(size!=size_old || !tex[0])
{
if (tex[0])
glDeleteTextures( 1, &tex[0]);
glGenTextures(1, &tex[0]);
glBindTexture(GL_TEXTURE_2D, tex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP ); // nastavuje za textury sa v smere u (vodorovnom) neopakuju
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP ); // nastavuje za textury sa v smere v (zvislom) neopakuju
// Copies the contents of the frame buffer into the texture
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, size, size==1024?512:size, 0);
size_old=size;
}
else
{
glBindTexture(GL_TEXTURE_2D, tex[0]);
// Copies the contents of the frame buffer into the texture
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size, size==1024?512:size);
}
t = timer1.GetTime();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, screen_x, screen_y);
}
RenderMotionBlur();
if(sposob==1 && !keys['T'])
{
_6kriz->Render();
}
}
else if(sposob==2)
{
GLfloat LightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; //svetlo ktore osvetluje zo vsetkych stran rovnako (okolite svetlo)
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; //smerove, alebo bodove svetlo - svetlo svieti z pozicie zadanej pomocou premennej LightPosition
GLfloat mat_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_specular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
float alpha;
if(keys['M'])
{
pocet_krokov++;
keys['M']=0;
}
if(keys['N'])
{
pocet_krokov--;
if(pocet_krokov<1)pocet_krokov=1;
keys['N']=0;
}
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glEnable(GL_BLEND);
for(int i=0; i<pocet_krokov; i++)
{
glPushMatrix();
alpha = (1.0f-i/(float)pocet_krokov)/(pocet_krokov*0.5f*(1.f+1.f/(float)pocet_krokov));
glColor4f(1,1,1,alpha);
LightAmbient[3] = alpha;
LightDiffuse[3] = alpha;
mat_diffuse[3] = alpha;
mat_specular[3] = alpha;
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, LightAmbient ); // nastavenie okoliteho (Ambient) svetla
glLightfv ( GL_LIGHT0, GL_DIFFUSE, LightDiffuse ); // nastavenie difuzneho svetla
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE , mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glRotatef(rot_x-i*delta_rot_x/motion_fps,1,0,0);
glRotatef(rot_y-i*delta_rot_y/motion_fps,0,1,0);
glRotatef(rot_z-i*delta_rot_z/motion_fps,0,0,1);
glBlendFunc(GL_ZERO,GL_ONE);// nekreslime nic na obrazovku, - iba do z-buffera
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glColorMask(0,0,0,0);
_6kriz->Render(0);
glColorMask(1,1,1,1);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glDepthFunc(GL_EQUAL ); // vykreslime iba tam kde sa uz kreslilo
glEnable(GL_TEXTURE_2D);
if(svetlo)glEnable(GL_LIGHTING); // zapne svetlo
_6kriz->Render(1);
glDepthFunc(GL_LEQUAL );
glClear(GL_DEPTH_BUFFER_BIT);
glPopMatrix();
}
LightAmbient[3] = 1.0f;
LightDiffuse[3] = 1.0f;
mat_diffuse[3] = 1.f;
mat_specular[3] = 1.f;
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, LightAmbient ); // nastavenie okoliteho (Ambient) svetla
glLightfv ( GL_LIGHT0, GL_DIFFUSE, LightDiffuse ); // nastavenie difuzneho svetla
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE , mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glDisable(GL_BLEND);
}
Prepni_do_2D();
glColor4f(1,1,1,1);
font0->Begin();
fps = 1.0f / timer0.GetTime();
timer0.Reset();
fps_counter.GetAvgFps(1.f/fps,0.2f);
gcvt( (double)fps_counter.avg_fps, 4, text);
font0->Print(0,0,text,0);
gcvt( (double)motion_fps, 4, text);
font0->Print(10,0,text,0);
if(sposob==0)
{
gcvt( (double)blend, 4, text);
}
else if(sposob==1)
{
itoa(pocet_krokov,text,10);
}
font0->Print(20,0,text,0);
if(sposob==0 || sposob==1)
{
gcvt( (double)t*1000.f, 4, text);
strcat(text," ms");
font0->Print(0,1,text,0);
itoa(size,text,10);
if(size!=1024)
font0->Print(0,2,text,0);
else
font0->Print(0,2,"1024 x 512",0);
if(sposob==0)
font0->Print(40,0,english ? "rendering to texture" : "kreslenie do textury",1);
else
font0->Print(40,0,english ? "rendering to texture 2" : "kreslenie do textury 2",1);
}
else font0->Print(40,0,english ? "more-times rendering":"viacnasobne kreslenie",1);
if(keys['H']) // zobrazenie helpu
{
int i=3;
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0,0,0,0.6f);
glLoadIdentity();
glBegin(GL_QUADS);
glVertex2i(80,80);
glVertex2i(800,100);
glVertex2i(800,555);
glVertex2i(80,555);
glEnd();
glColor4f(1,1,1,1);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_COLOR);
glEnable(GL_TEXTURE_2D);
if(english)
{
font0->Print(15,i++,"Program: Motion blur",0);
glColor3f(0.9f,0.9f,1.0f);
font0->Print(8,i++,"Programmer: Marek Mizanin - www.mizanin.szm.sk",0);
glColor3f(1.0f,1.0f,1.0f);
font0->Print(10,i++,"Controls:",0);
font0->Print(15,i++,"space - change way to create blur",1);
i++;
font0->Print(12,i++,"Motion blur with rendering to texture:",1);
font0->Print(15,i++,"+ on numeric. - increase fps motion blur",1);
font0->Print(15,i++,"- on numeric. - decrease fps motion blur",1);
font0->Print(15,i++,"0,1,..,9 - set size of texture on 2^key",1);
font0->Print(15,i++,"M - increase alpha",1);
font0->Print(15,i++,"N - decrease alpha",1);
i++;
font0->Print(12,i++,"Motion blur with more-times rendering:",1);
font0->Print(15,i++,"+ on numeric. - increase fps motion blur",1);
font0->Print(15,i++,"- on numeric. - decrease fps motion blur",1);
font0->Print(15,i++,"M - increase count of drawing",1);
font0->Print(15,i++,"N - decrease count of drawing",1);
i++;
font0->Print(15,i++,"P - on/off wire frame",1);
font0->Print(15,i++,"L - on/off lighting",1);
font0->Print(15,i++,"T - show texture",1);
i++;
font0->Print(15,i++,"AD, WS, EX - rotate objecst in axis x, y, z",1);
font0->Print_scale(15,i++,"A and simultaneously D - stop rotation x, similarly also for y,z axis",1,0.8f,1.f);
i++;
font0->Print(15,i++,"1. row - FPS, fps motion blur, alpha/count of drawing",1);
font0->Print(15,i++,"2. row - time for copying screen to texture",1);
font0->Print(15,i++,"3. row - size of texture",1);
}
else
{
font0->Print(15,i++,"Program: Motion blur",0);
glColor3f(0.9f,0.9f,1.0f);
font0->Print(8,i++,"Naprogramoval: Marek Mizanin - www.mizanin.szm.sk",0);
glColor3f(1.0f,1.0f,1.0f);
font0->Print(10,i++,"Pouzivane klavesy:",0);
font0->Print(15,i++,"medzernik - meni sposob vytvaranie rozmazania",1);
i++;
font0->Print(12,i++,"Motion blur pomocou kreslenia do textury:",1);
font0->Print(15,i++,"+ na numer. - zvecsuje fps motion blur",1);
font0->Print(15,i++,"- na numer. - zmensuje fps motion blur",1);
font0->Print(15,i++,"0,1,..,9 - nastavuje velkost textury na 2^klavesu",1);
font0->Print(15,i++,"M - zvecsuje alpha",1);
font0->Print(15,i++,"N - zmensuje alpha",1);
i++;
font0->Print(12,i++,"Motion blur pomocou viacnasobneho kreslenia:",1);
font0->Print(15,i++,"+ na numer. - zvecsuje fps motion blur",1);
font0->Print(15,i++,"- na numer. - zmensuje fps motion blur",1);
font0->Print(15,i++,"M - zvecsuje pocet kresleni",1);
font0->Print(15,i++,"N - zmensuje pocet kresleni",1);
i++;
font0->Print(15,i++,"P - zapne/vypne zobrazovanie ciar",1);
font0->Print(15,i++,"L - zapne/vypne svetlo",1);
font0->Print(15,i++,"T - zobrazy vytvorenu texturu",1);
i++;
font0->Print(15,i++,"AD, WS, EX - rotuje objekt podla smerov x, y, z",1);
font0->Print(15,i++,"A a zaroven D - stop rotacia x, podobne aj pre osi y,z",1);
i++;
font0->Print(15,i++,"1. riadok - FPS, fps motion blur, alpha/pocet kresleni",1);
font0->Print(15,i++,"2. riadok - cas potrebny na skopirovanie do textury",1);
font0->Print(15,i++,"3. riadok - velkost textury",1);
}
font0->Print(15,i++,"H - help",1);
}
font0->End();
if(keys['F']){ SaveScreen(); keys['F']=0;} // ulozenie obrazovky do raw subora
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -