Missing library from test link list.
[dcpomatic.git] / hacks / gl / basic_gl_pane.cc
1 #include "wx/wx.h"
2 #include "wx/sizer.h"
3 #include "wx/glcanvas.h"
4
5 #include "basic_gl_pane.h"
6 #include "image.h"
7 #include "drawable.h"
8
9 // include OpenGL
10 #ifdef __WXMAC__
11 #include "OpenGL/glu.h"
12 #include "OpenGL/gl.h"
13 #else
14 #include <GL/glu.h>
15 #include <GL/gl.h>
16 #endif
17
18 class MyApp: public wxApp
19 {
20     virtual bool OnInit();
21
22     wxFrame *frame;
23     BasicGLPane * glPane;
24 public:
25
26 };
27
28 IMPLEMENT_APP(MyApp)
29
30 Image* image = NULL;
31 DrawableThing* sprite;
32
33 bool MyApp::OnInit()
34 {
35     wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
36     frame = new wxFrame((wxFrame *)NULL, -1,  wxT("Hello GL World"), wxPoint(50,50), wxSize(400,200));
37
38     int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0};
39
40     glPane = new BasicGLPane( (wxFrame*) frame, args);
41     sizer->Add(glPane, 1, wxEXPAND);
42
43     frame->SetSizer(sizer);
44     frame->SetAutoLayout(true);
45
46     frame->Show();
47     return true;
48 }
49
50 BEGIN_EVENT_TABLE(BasicGLPane, wxGLCanvas)
51 EVT_MOTION(BasicGLPane::mouseMoved)
52 EVT_LEFT_DOWN(BasicGLPane::mouseDown)
53 EVT_LEFT_UP(BasicGLPane::mouseReleased)
54 EVT_RIGHT_DOWN(BasicGLPane::rightClick)
55 EVT_LEAVE_WINDOW(BasicGLPane::mouseLeftWindow)
56 EVT_SIZE(BasicGLPane::resized)
57 EVT_KEY_DOWN(BasicGLPane::keyPressed)
58 EVT_KEY_UP(BasicGLPane::keyReleased)
59 EVT_MOUSEWHEEL(BasicGLPane::mouseWheelMoved)
60 EVT_PAINT(BasicGLPane::render)
61 END_EVENT_TABLE()
62
63
64 // some useful events to use
65 void BasicGLPane::mouseMoved(wxMouseEvent& event) {}
66 void BasicGLPane::mouseDown(wxMouseEvent& event) {}
67 void BasicGLPane::mouseWheelMoved(wxMouseEvent& event) {}
68 void BasicGLPane::mouseReleased(wxMouseEvent& event) {}
69 void BasicGLPane::rightClick(wxMouseEvent& event) {}
70 void BasicGLPane::mouseLeftWindow(wxMouseEvent& event) {}
71 void BasicGLPane::keyPressed(wxKeyEvent& event) {}
72 void BasicGLPane::keyReleased(wxKeyEvent& event) {}
73
74 BasicGLPane::BasicGLPane(wxFrame* parent, int* args) :
75 wxGLCanvas(parent, wxID_ANY,  wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"),  args)
76 {
77 }
78
79 void BasicGLPane::resized(wxSizeEvent& evt)
80 {
81     wxGLCanvas::OnSize(evt);
82
83     Refresh();
84 }
85
86 void BasicGLPane::prepare3DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
87 {
88     /*
89      *  Inits the OpenGL viewport for drawing in 3D.
90      */
91
92     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
93     glClearDepth(1.0f); // Depth Buffer Setup
94     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
95     glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
96     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
97
98     glEnable(GL_COLOR_MATERIAL);
99
100     glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
101     glMatrixMode(GL_PROJECTION);
102     glLoadIdentity();
103
104     float ratio_w_h = (float)(bottomrigth_x-topleft_x)/(float)(bottomrigth_y-topleft_y);
105     gluPerspective(45 /*view angle*/, ratio_w_h, 0.1 /*clip close*/, 200 /*clip far*/);
106     glMatrixMode(GL_MODELVIEW);
107     glLoadIdentity();
108
109 }
110
111 void BasicGLPane::prepare2DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
112 {
113
114     /*
115      *  Inits the OpenGL viewport for drawing in 2D
116      */
117
118     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
119     glEnable(GL_TEXTURE_2D);   // textures
120     glEnable(GL_COLOR_MATERIAL);
121     glEnable(GL_BLEND);
122     glDisable(GL_DEPTH_TEST);
123     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
124
125     glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
126     glMatrixMode(GL_PROJECTION);
127     glLoadIdentity();
128
129     gluOrtho2D(topleft_x, bottomrigth_x, bottomrigth_y, topleft_y);
130     glMatrixMode(GL_MODELVIEW);
131     glLoadIdentity();
132 }
133
134 int BasicGLPane::getWidth()
135 {
136     return GetSize().x;
137 }
138
139 int BasicGLPane::getHeight()
140 {
141     return GetSize().y;
142 }
143
144 void BasicGLPane::render( wxPaintEvent& evt )
145 {
146     if(!IsShown()) return;
147
148     wxGLCanvas::SetCurrent();
149
150
151     if(image == NULL)
152     {
153         image = new Image( wxT("myfile.png") );
154         sprite = new DrawableThing(image);
155     }
156
157     wxPaintDC(this); // only to be used in paint events. use wxClientDC to paint outside the paint event
158
159     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160
161     // render loaded image
162     prepare2DViewport(0,0,getWidth(), getHeight());
163     sprite->render();
164
165     glFlush();
166     SwapBuffers();
167 }