f49e69a35bad9bf525f5ddc3bf10f923c9c45e69
[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
10 private:
11         void paint (wxPaintEvent& event);
12         void Render();
13 };
14
15 GLView::GLView(wxFrame *parent)
16         : wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"))
17 {
18         Bind (wxEVT_PAINT, boost::bind(&GLView::paint, this, _1));
19 }
20
21 void GLView::paint (wxPaintEvent &)
22 {
23         SetCurrent();
24         wxPaintDC(this);
25         glClearColor(0.0, 0.0, 0.0, 0.0);
26         glClear(GL_COLOR_BUFFER_BIT);
27         glViewport(0, 0, (GLint)GetSize().x, (GLint)GetSize().y);
28
29         //create test checker image
30         unsigned char texDat[64];
31         for (int i = 0; i < 64; ++i)
32                 texDat[i] = ((i + (i / 8)) % 2) * 128 + 127;
33
34         //upload to GPU texture
35         GLuint tex;
36         glGenTextures(1, &tex);
37         glBindTexture(GL_TEXTURE_2D, tex);
38         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
39         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
40         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texDat);
41         glBindTexture(GL_TEXTURE_2D, 0);
42
43         //match projection to window resolution (could be in reshape callback)
44 //    glMatrixMode(GL_PROJECTION);
45 //    glOrtho(0, 800, 0, 600, -1, 1);
46 //    glMatrixMode(GL_MODELVIEW);
47
48         glClear(GL_COLOR_BUFFER_BIT);
49         glColor3f(1, 1, 1);
50         glBindTexture(GL_TEXTURE_2D, tex);
51         glEnable(GL_TEXTURE_2D);
52         glBegin(GL_QUADS);
53         glTexCoord2i(0, 0); glVertex2f(-0.5, -0.5);
54         glTexCoord2i(0, 1); glVertex2f(-0.5, 0.5);
55         glTexCoord2i(1, 1); glVertex2f(0.5, 0.5);
56         glTexCoord2i(1, 0); glVertex2f(0.5, -0.5);
57         glEnd();
58         glDisable(GL_TEXTURE_2D);
59         glBindTexture(GL_TEXTURE_2D, 0);
60
61         glFlush();
62         SwapBuffers();
63 }
64
65 class MyApp : public wxApp
66 {
67         bool OnInit()
68         {
69                 wxFrame *frame = new wxFrame (0, -1, wxT("Hello GL World"), wxPoint(50,50), wxSize(200,200));
70                 new GLView(frame);
71
72                 frame->Show (true);
73                 return true;
74         }
75 };
76
77 IMPLEMENT_APP(MyApp)