Nebula Device - Урок 2
Автор: Daddy
WEB-сайт: http://daddy.mirgames.ru
Отрисовка двигающегося треугольника.
Привет! В этом уроке я объясню как нарисовать треугольник в OpenGL. Здесь нам понадобиться материал из первого урока(кроме Main). Функция WinMain остаётся неизменной так как она находится в Init Main и End. Давайте начнём с Init:
#include // include matrix lib to handle matrix stuff
void Init()
{
KernelServer = new nKernelServer;
GfxServer = (nGfxServer *) ks->New("nglserver","/sys/servers/gfx");
InputServer = (nInputServer *) ks->New("ninputserver","/sys/servers/input");
// these remain unchanged
KernelServer->ts->EnableFrameTime();
//enable the frametime counter
//the Kernel server class has a member
//of nTimeServer class that handles time ,
//profiling etc called 'ts' (bad name)
InputServer->BeginMap();
InputServer->Map("keyb0:left.pressed", "decreasex");
InputServer->Map("keyb0:right.pressed", "increasex");
InputServer->Map("keyb0:up.pressed", "increasey");
InputServer->Map("keyb0:down.pressed", "decreasey");
InputServer->Map("keyb0:esc.up", "end");
InputServer->EndMap();
GfxServer->SetDisplayMode("-type(win)-w(640)-h(480)");
GfxServer->SetClearColor(0.7f,0.7f,0.7f,1.0f);
GfxServer->SetViewVolume(-100, 100, -100, 100, 0, 100);
// NEW. We have to set the view volume so that polys dont get culled
GfxServer->OpenDisplay();
}
Теперь Main Здесь мы рисуем треугольник void Main()
{
static float rangle = 0.0f, // rotation angle for the triangle
xpos=0.0f, // xpos for the tri
ypos=0.0f, // ypos for the tri
LastFrameTime=0.0f; // var to track time
InputServer->Trigger();
if(!GfxServer->Trigger())
EndProgram();
KernelServer->ts->Trigger();
// Do Internal work, quit if needed, update times , etc;
if (InputServer->GetButton("end"))
{
EndProgram();
return;
}
if (InputServer->GetButton("decreasex")) xpos-=0.01f;
if (InputServer->GetButton("increasex")) xpos+=0.01f;
if (InputServer->GetButton("decreasey")) ypos-=0.01f;
if (InputServer->GetButton("increasey")) ypos+=0.01f;
// if flags were fired, execute
// correspondant action
GfxServer->BeginScene();
// tell the GfxServer and correspondant API(GL or DX)
// that we are going to draw
nRState RenderState;
RenderState.Set(N_RS_CULLMODE,N_CULL_NONE);
GfxServer->SetState(RenderState);
// Init a renderstate block and disable Culling so that
// when the poligon turns it doesnt get culled
static matrix44 Transform;
TransformMatrix.ident();
// create a matrix to handle the
// transformation operations and set it to
// the identity
rangle+=0.01f;
// increase polygon rotation angle
Transform.rotate_y(rangle);
Transform.rotate_x(rangle*0.5f);
Transform.rotate_z(rangle*0.75f);
Transform.translate(vector3(xpos,ypos,0));
// create a matrix which will rotate the points then
// translate 'em by xpos and ypos
GfxServer->SetMatrix(N_MXM_MODELVIEW,Transform);
// set the ModelView matrix to our matrix
GfxServer->Begin(N_PTYPE_TRIANGLE_LIST);
GfxServer->Rgba(1.0f,0.0f,0.0f,1.0f);
GfxServer->Coord(0.25, 0.0f, -0.25f);
GfxServer->Rgba(0.0f,1.0f,0.0f,1.0f);
GfxServer->Coord(0.25, 0.0f, +0.25f);
GfxServer->Rgba(0.0f,0.0f,1.0f,1.0f);
GfxServer->Coord(-0.25f, 0.0f, 0.25);
GfxServer->End();
char str[80];
double fps = 1/(KernelServer->ts->GetFrameTime()-LastFrameTime);
sprintf(str,"FPS:%f",fps);
// calculate the fps through GetFrameTime() and LastFrameTime
// and print it to str
GfxServer->BeginText(); // Tell him that we're gonna draw text
GfxServer->TextPos(-1,-1); // Set the Text output position
GfxServer->Text(str); // send the string and draw it
GfxServer->EndText(); // end the draw text block
GfxServer->EndScene(); // End drawing (update screen,clear,etc)
LastFrameTime = KernelServer->ts->GetFrameTime();
// set last frame time
// NOTE: GetFrameTime() here will return the same as it has returned when
// we have calculated the fps cause it returns a var which is only updated
// in InputServer->Trigger()
}
Конец остаётся тем же самым так как мы не добавили никакого нового интерфейса. Попробуйте это, нажмите кнопку мыши и треугольник будет двигаться. FPS просто потрясает!
Исходник можно скачать тут: tutor2.rar
|