~kicad-product-committers/kicad/kicad-gal

« back to all changes in this revision

Viewing changes to gal/test/shader/test_shader.cpp

  • Committer: torstenhtr at gmx
  • Date: 2012-08-04 14:55:55 UTC
  • Revision ID: torstenhtr@gmx.de-20120804145555-jbm9k193p1tth439
Cleaned up the files.
Repaired and improved the test examples.
Added the wxDC backend.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * This program source code file is part of KICAD, a free EDA CAD application.
3
3
 *
4
 
 * Copyright (C) 2010 Virtenio GmbH, Torsten Hueter, torsten.hueter <at> virtenio.de
5
 
 * Copyright (C) 2007 Kicad Developers, see change_log.txt for contributors.
 
4
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 
5
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6
6
 *
7
7
 * Test of the Graphics Abstraction Layer (GAL)
8
8
 *
49
49
 
50
50
#include <deque>
51
51
 
 
52
// FIXME Should be merged with test_gal.cpp
 
53
 
52
54
#define screenSizeX 640
53
55
#define screenSizeY 480
54
56
 
58
60
public:
59
61
    // @brief Constructor
60
62
    CANVAS_FRAME( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
61
 
            const wxSize& size );
 
63
                  const wxSize& size );
62
64
 
63
65
    // @brief Destructor
64
66
    virtual ~CANVAS_FRAME();
65
67
 
66
68
    void SetTestNumber( int testNumber );
 
69
    void DrawPrimitives();
 
70
    void DrawPrimitives2();
67
71
 
68
72
private:
69
73
    bool m_isReady;
97
101
    void DrawCoordinates();
98
102
 
99
103
    // Event handlers
100
 
    void OnTimerEvent( wxTimerEvent &event );
101
104
    void OnMotion( wxMouseEvent& event );
102
105
    void OnMouseWheel( wxMouseEvent& event );
103
106
    void OnRedraw( wxCommandEvent& event );
106
109
};
107
110
 
108
111
CANVAS_FRAME::CANVAS_FRAME( wxWindow* parent, wxWindowID id, const wxString& title,
109
 
        const wxPoint& pos, const wxSize& size ) :
 
112
                            const wxPoint& pos, const wxSize& size ) :
110
113
        wxFrame( parent, id, title, pos, size ), m_screenSize( size.x, size.y ), m_wxtimer( this )
111
114
{
112
115
    m_animationIndex = 0;
139
142
    m_offset = 0.333;
140
143
 
141
144
    // Load Font
142
 
    if ( !m_font.LoadNewStrokeFont( newstroke_font, newstroke_font_bufsize ) )
 
145
    if( !m_font.LoadNewStrokeFont( newstroke_font, newstroke_font_bufsize ) )
143
146
    {
144
147
        cout << "Loading of the font failed." << endl;
145
148
    }
155
158
{
156
159
    VECTOR2D mousePoint = VECTOR2D( event.GetX(), event.GetY() );
157
160
 
158
 
    if ( event.Dragging() )
 
161
    if( event.Dragging() )
159
162
    {
160
 
        if ( m_isPanning )
 
163
        if( m_isPanning )
161
164
        {
162
165
            MATRIX3x3D matrix = m_gal->GetWorldScreenMatrix().Inverse();
163
166
            VECTOR2D delta = matrix.GetScale().x * ( m_startMousePoint - mousePoint );
198
201
    PaintScene();
199
202
}
200
203
 
 
204
void CANVAS_FRAME::DrawPrimitives()
 
205
{
 
206
    for( int j = 0; j < 10; j++ )
 
207
    {
 
208
        m_gal->SetStrokeColor( COLOR4D( 1, 0.7, 0.6, 0.8 ) );
 
209
        m_gal->SetFillColor( COLOR4D( 0, 1, 1, 0.1 * j ) );
 
210
        for( int i = 0; i < 20; i++ )
 
211
        {
 
212
            m_gal->DrawCircle( VECTOR2D( 500 + 500 * i, 500 + 1000 * j ), 100 + 20 * i );
 
213
        }
 
214
    }
 
215
}
 
216
 
 
217
void CANVAS_FRAME::DrawPrimitives2()
 
218
{
 
219
    for( int j = 0; j < 10; j++ )
 
220
    {
 
221
        m_gal->SetFillColor( COLOR4D( 1, 1, 0, 0.1 * j ) );
 
222
        m_gal->SetStrokeColor( COLOR4D( 1, 0.7, 0.6, 0.8 ) );
 
223
 
 
224
        for( int i = 0; i < 20; i++ )
 
225
        {
 
226
            m_gal->DrawCircle( VECTOR2D( 500 + 1000 * j, 500 + 500 * i ), 100 + 20 * i );
 
227
        }
 
228
    }
 
229
}
 
230
 
201
231
void CANVAS_FRAME::TestShader()
202
232
{
203
233
    //m_gal->SetLineCap(LINE_CAP_ROUND);
204
234
    //m_gal->SetLineWidth(3);
205
235
 
206
 
    m_gal->SetLayerDepth( 0 );
207
 
 
208
 
    glEnable( GL_STENCIL_TEST );
209
 
    glStencilFunc( GL_ALWAYS, 0, 0 );
210
 
    glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
211
 
    glColorMask( false, false, false, false );
212
 
    m_gal->SetStrokeColor( COLOR4D( 1, 1, 1, 1 ) );
213
 
    for ( int j = 0; j < 10; j++ )
214
 
    {
215
 
        for ( int i = 0; i < 20; i++ )
216
 
        {
217
 
            m_gal->DrawCircle( VECTOR2D( 500 + 500 * i, 500 + 1000 * j ), 100 + 20 * i );
218
 
        }
219
 
    }
220
 
    glStencilFunc( GL_NOTEQUAL, 1, 1 );
221
 
    glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
222
 
    glColorMask( true, true, true, true );
223
 
 
224
 
    for ( int j = 0; j < 10; j++ )
225
 
    {
226
 
        m_gal->SetStrokeColor( COLOR4D( 0, 1, 1, 0.1 * j ) );
227
 
        for ( int i = 0; i < 20; i++ )
228
 
        {
229
 
            m_gal->DrawCircle( VECTOR2D( 500 + 500 * i, 500 + 1000 * j ), 100 + 20 * i );
230
 
        }
231
 
    }
232
 
    glDisable( GL_STENCIL_TEST );
233
 
 
234
236
    glClear( GL_DEPTH_BUFFER_BIT );
235
237
 
236
 
    glEnable( GL_STENCIL_TEST );
237
 
    glStencilFunc( GL_ALWAYS, 0, 0 );
238
 
    glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
239
 
    glColorMask( false, false, false, false );
240
 
    m_gal->SetStrokeColor( COLOR4D( 0, 1, 1, 1 ) );
241
 
    for ( int j = 0; j < 10; j++ )
242
 
    {
243
 
        for ( int i = 0; i < 20; i++ )
244
 
        {
245
 
            m_gal->DrawCircle( VECTOR2D( 500 + 1000 * j, 500 + 500 * i ), 100 + 20 * i );
246
 
        }
247
 
    }
248
 
    glStencilFunc( GL_NOTEQUAL, 1, 1 );
249
 
    glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
250
 
    glColorMask( true, true, true, true );
251
 
 
252
 
    for ( int j = 0; j < 10; j++ )
253
 
    {
254
 
        m_gal->SetStrokeColor( COLOR4D( 1, 1, 0, 0.1 * j ) );
255
 
        for ( int i = 0; i < 20; i++ )
256
 
        {
257
 
            m_gal->DrawCircle( VECTOR2D( 500 + 1000 * j, 500 + 500 * i ), 100 + 20 * i );
258
 
        }
259
 
    }
260
 
    glDisable( GL_STENCIL_TEST );
 
238
    m_gal->SetLayerDepth( 0 );
 
239
    m_gal->SetLayerDepth( 0 );
 
240
    m_gal->SetIsStroke( false );
 
241
    m_gal->SetIsFill( true );
 
242
    m_gal->SetLineWidth( 20 );
 
243
 
 
244
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
245
    DrawPrimitives();
 
246
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_PREPARE_EDGES );
 
247
    DrawPrimitives();
 
248
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_DRAW_EDGES );
 
249
    DrawPrimitives();
 
250
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
251
 
 
252
    m_gal->SetLayerDepth( -10 );
 
253
    m_gal->SetIsStroke( false );
 
254
 
 
255
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
256
    DrawPrimitives2();
 
257
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_PREPARE_EDGES );
 
258
    DrawPrimitives2();
 
259
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_DRAW_EDGES );
 
260
    DrawPrimitives2();
 
261
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
262
 
 
263
    m_gal->SetLayerDepth( -20 );
 
264
    m_gal->SetIsStroke( true );
 
265
 
 
266
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
267
    DrawPrimitives2();
 
268
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_PREPARE_EDGES );
 
269
    DrawPrimitives2();
 
270
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_DRAW_EDGES );
 
271
    DrawPrimitives2();
 
272
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
273
    m_gal->SetIsStroke( false );
261
274
}
262
275
 
263
276
void CANVAS_FRAME::DrawCoordinates()
264
277
{
265
 
    m_gal->SetLineWidth( 20 );
 
278
    m_gal->SetLineWidth( 10 );
266
279
    m_gal->SetStrokeColor( COLOR4D( 1, 1, 1, 1 ) );
267
280
    m_font.SetHorizontalJustify( HORIZONTAL_JUSTIFY_CENTER );
268
281
    m_font.SetVerticalJustify( VERTICAL_JUSTIFY_CENTER );
269
282
    m_font.SetGlyphSize( VECTOR2D( 100, 100 ) );
270
 
    for ( int i = 0; i < 10; i++ )
 
283
    for( int i = 0; i < 10; i++ )
271
284
    {
272
285
        std::stringstream ss;
273
286
        ss << i;
274
 
        m_font.Draw( ss.str(), POINT( 0, i * 1000 ), 0 );
275
 
        if ( i )
 
287
        m_font.Draw( ss.str(), POINT2D( 0, i * 1000 ), 0 );
 
288
        if( i )
276
289
        {
277
 
            m_font.Draw( ss.str(), POINT( i * 1000, 0 ), 0 );
 
290
            m_font.Draw( ss.str(), POINT2D( i * 1000, 0 ), 0 );
278
291
        }
279
292
    }
280
293
}
284
297
    m_gal->BeginDrawing();
285
298
    m_gal->SetBackgroundColor( COLOR4D( 0, 0, 0, 1.0 ) );
286
299
    m_gal->ClearScreen();
287
 
    m_gal->SetLayerDepth( -0.1 );
288
300
    m_gal->SetGridSize( VECTOR2D( 1000, 1000 ) );
289
301
    m_gal->DrawGrid();
290
 
    DrawCoordinates();
 
302
    m_gal->SetLayerDepth( 0 );
 
303
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
304
    DrawCoordinates();
 
305
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_PREPARE_EDGES );
 
306
    DrawCoordinates();
 
307
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_DRAW_EDGES );
 
308
    DrawCoordinates();
 
309
    ( (OPENGL_GAL*)m_gal )->SetDrawMode( OPENGL_GAL::DRAW_MODE_NORMAL );
 
310
 
291
311
    m_gal->SetLayerDepth( 0 );
292
312
 
293
313
    TestShader();
317
337
 
318
338
bool GAL_TEST_APPLICATION::OnInit()
319
339
{
320
 
    if ( !wxApp::OnInit() )
 
340
    if( !wxApp::OnInit() )
321
341
        return false;
322
342
 
323
 
    CANVAS_FRAME* frameOpenGL = new CANVAS_FRAME( (wxFrame *) NULL, -1, wxT( "OpenGL Shader Test" ),
324
 
            wxPoint( screenSizeX + 10, 0 ), wxSize( screenSizeX, screenSizeY ) );
 
343
    CANVAS_FRAME* frameOpenGL = new CANVAS_FRAME( (wxFrame *)NULL, -1, wxT( "OpenGL Shader Test" ),
 
344
                                                  wxPoint( screenSizeX + 10, 0 ),
 
345
                                                  wxSize( screenSizeX, screenSizeY ) );
325
346
 
326
347
    frameOpenGL->Show( TRUE );
327
348