More tidying of example.
[dcpomatic.git] / hacks / gl.cc
1 #include <wx/wx.h>
2 #include <wx/glcanvas.h>
3 #include <boost/bind.hpp>
4
5 class GLView : public wxGLCanvas
6 {
7 public:
8         GLView (wxFrame* parent);
9         ~GLView ();
10
11 private:
12         void paint (wxPaintEvent& event);
13
14         wxGLContext* _context;
15 };
16
17 GLView::GLView(wxFrame *parent)
18         : wxGLCanvas (parent, wxID_ANY, 0)
19 {
20         _context = new wxGLContext (this);
21         Bind (wxEVT_PAINT, boost::bind(&GLView::paint, this, _1));
22 }
23
24 GLView::~GLView ()
25 {
26         delete _context;
27 }
28
29 void GLView::paint (wxPaintEvent &)
30 {
31         SetCurrent (*_context);
32         wxPaintDC (this);
33         glClear(GL_COLOR_BUFFER_BIT);
34
35         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
36         glEnable(GL_TEXTURE_2D);
37         glDisable(GL_DEPTH_TEST);
38         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
39
40         glViewport (0, 0, GetSize().x, GetSize().y);
41         glMatrixMode(GL_PROJECTION);
42         glLoadIdentity();
43
44         glOrtho (0, GetSize().x, GetSize().y, 0, -1, 1);
45         glMatrixMode(GL_MODELVIEW);
46         glLoadIdentity();
47
48         //create test checker image
49         unsigned char texDat[64];
50         for (int i = 0; i < 64; ++i)
51                 texDat[i] = ((i + (i / 8)) % 2) * 128 + 127;
52
53         //upload to GPU texture
54         GLuint tex;
55         glGenTextures(1, &tex);
56         glBindTexture(GL_TEXTURE_2D, tex);
57         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
58         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texDat);
60         glBindTexture(GL_TEXTURE_2D, 0);
61
62         //match projection to window resolution (could be in reshape callback)
63 //    glMatrixMode(GL_PROJECTION);
64 //    glOrtho(0, 800, 0, 600, -1, 1);
65 //    glMatrixMode(GL_MODELVIEW);
66
67         glClear(GL_COLOR_BUFFER_BIT);
68         glColor3f(1, 1, 1);
69         glBindTexture(GL_TEXTURE_2D, tex);
70         glEnable(GL_TEXTURE_2D);
71         glBegin(GL_QUADS);
72         glTexCoord2i(0, 0); glVertex2f(0, 0);
73         glTexCoord2i(0, 1); glVertex2f(0, 100);
74         glTexCoord2i(1, 1); glVertex2f(100, 100);
75         glTexCoord2i(1, 0); glVertex2f(100, 0);
76         glEnd();
77         glDisable(GL_TEXTURE_2D);
78         glBindTexture(GL_TEXTURE_2D, 0);
79
80         glFlush();
81         SwapBuffers();
82 }
83
84 class MyApp : public wxApp
85 {
86         bool OnInit()
87         {
88                 wxFrame *frame = new wxFrame (0, -1, wxT("Hello GL World"), wxPoint(50,50), wxSize(200,200));
89                 new GLView(frame);
90
91                 frame->Show (true);
92                 return true;
93         }
94 };
95
96 IMPLEMENT_APP(MyApp)