1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include "main.h"
myFrameMain::myFrameMain() : wxFrame(NULL, -1, "Hello GL World", wxPoint(-1, -1), wxSize(1024,790), wxDEFAULT_FRAME_STYLE)
{
glCanvas = new myGLCanvas(this);
wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(glCanvas, 1, wxALL|wxEXPAND, 0);
this->SetSizer(sizer);
this->SetAutoLayout(true);
this->Layout();
this->ShowFullScreen(false);
}
myGLCanvas::myGLCanvas(wxWindow *parent) : wxGLCanvas(parent, -1, wxPoint(0,0))
{
this->parent=(wxFrame*)parent;
fullScreen=false;
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
this->SetCurrent();
}
void myGLCanvas::onIdle(wxIdleEvent &event)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
GLubyte indices[]={
4, 5, 6, 7, //front
1, 2, 6, 5, //right
0, 1, 5, 4, //bottom
0, 3, 2, 1, //back
0, 4, 7, 3, //left
2, 3, 7, 6, //top
};
const GLfloat sommets[]={
-1.0, -1.0, -1.0, //0
1.0, -1.0, -1.0, //1
1.0, 1.0, -1.0, //2
-1.0, 1.0, -1.0, //3
-1.0, -1.0, 1.0, //4
1.0, -1.0, 1.0, //5
1.0, 1.0, 1.0, //6
-1.0, 1.0, 1.0, //7
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, sommets);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glColor3f(0.0f,1.0f,0.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[0]);
glColor3f(1.0f,0.5f,0.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[4]);
glColor3f(1.0f,0.0f,0.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[8]);
glColor3f(1.0f,1.0f,0.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[12]);
glColor3f(0.0f,0.0f,1.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[16]);
glColor3f(1.0f,0.0f,1.0f);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &indices[20]);
glFlush();
this->SwapBuffers();
event.RequestMore();
}
void myGLCanvas::onKey(wxKeyEvent &event)
{
int key = event.GetKeyCode();
if(key==WXK_ESCAPE)
{
parent->Destroy();
}else if (key==WXK_F1)
{
fullScreen=!fullScreen;
parent->ShowFullScreen(fullScreen);
}
}
void myGLCanvas::onSize(wxSizeEvent &event)
{
this->Layout();
this->GetSize(&width, &height);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
BEGIN_EVENT_TABLE(myGLCanvas, wxGLCanvas)
EVT_KEY_DOWN(myGLCanvas::onKey)
EVT_IDLE(myGLCanvas::onIdle)
EVT_SIZE(myGLCanvas::onSize)
END_EVENT_TABLE()
IMPLEMENT_APP(Ini);
bool Ini::OnInit()
{
myFrameMain *frame = new myFrameMain();
frame->Show(TRUE);
return true;
} |
Partager