~ubuntu-branches/ubuntu/trusty/libsdl2/trusty-proposed

« back to all changes in this revision

Viewing changes to test/testshader.c

  • Committer: Package Import Robot
  • Author(s): Manuel A. Fernandez Montecelo
  • Date: 2013-12-28 12:31:19 UTC
  • mto: (7.1.3 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20131228123119-wehupm72qsjvh6vz
Tags: upstream-2.0.1+dfsg1
ImportĀ upstreamĀ versionĀ 2.0.1+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
*/
12
12
/* This is a simple example of using GLSL shaders with SDL */
13
13
 
14
 
#include <stdio.h> /* for printf() */
15
14
#include "SDL.h"
16
15
 
17
16
#ifdef HAVE_OPENGL
139
138
        glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
140
139
        info = SDL_stack_alloc(char, length+1);
141
140
        glGetInfoLogARB(shader, length, NULL, info);
142
 
        fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
 
141
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile shader:\n%s\n%s", source, info);
143
142
        SDL_stack_free(info);
144
143
 
145
144
        return SDL_FALSE;
245
244
    /* Compile all the shaders */
246
245
    for (i = 0; i < NUM_SHADERS; ++i) {
247
246
        if (!CompileShaderProgram(&shaders[i])) {
248
 
            fprintf(stderr, "Unable to compile shader!\n");
 
247
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to compile shader!\n");
249
248
            return SDL_FALSE;
250
249
        }
251
250
    }
333
332
}
334
333
 
335
334
/* A general OpenGL initialization function.    Sets all of the initial parameters. */
336
 
void InitGL(int Width, int Height)                    // We call this right after our OpenGL window is created.
 
335
void InitGL(int Width, int Height)                    /* We call this right after our OpenGL window is created. */
337
336
{
338
337
    GLdouble aspect;
339
338
 
340
339
    glViewport(0, 0, Width, Height);
341
 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);        // This Will Clear The Background Color To Black
342
 
    glClearDepth(1.0);                // Enables Clearing Of The Depth Buffer
343
 
    glDepthFunc(GL_LESS);                // The Type Of Depth Test To Do
344
 
    glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
345
 
    glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
 
340
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);        /* This Will Clear The Background Color To Black */
 
341
    glClearDepth(1.0);                /* Enables Clearing Of The Depth Buffer */
 
342
    glDepthFunc(GL_LESS);                /* The Type Of Depth Test To Do */
 
343
    glEnable(GL_DEPTH_TEST);            /* Enables Depth Testing */
 
344
    glShadeModel(GL_SMOOTH);            /* Enables Smooth Color Shading */
346
345
 
347
346
    glMatrixMode(GL_PROJECTION);
348
 
    glLoadIdentity();                // Reset The Projection Matrix
 
347
    glLoadIdentity();                /* Reset The Projection Matrix */
349
348
 
350
349
    aspect = (GLdouble)Width / Height;
351
350
    glOrtho(-3.0, 3.0, -3.0 / aspect, 3.0 / aspect, 0.0, 1.0);
364
363
        MAXY
365
364
    };
366
365
 
367
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer
368
 
    glLoadIdentity();                // Reset The View
369
 
 
370
 
    glTranslatef(-1.5f,0.0f,0.0f);        // Move Left 1.5 Units
371
 
 
372
 
    // draw a triangle (in smooth coloring mode)
373
 
    glBegin(GL_POLYGON);                // start drawing a polygon
374
 
    glColor3f(1.0f,0.0f,0.0f);            // Set The Color To Red
375
 
    glVertex3f( 0.0f, 1.0f, 0.0f);        // Top
376
 
    glColor3f(0.0f,1.0f,0.0f);            // Set The Color To Green
377
 
    glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
378
 
    glColor3f(0.0f,0.0f,1.0f);            // Set The Color To Blue
379
 
    glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left
380
 
    glEnd();                    // we're done with the polygon (smooth color interpolation)
381
 
 
382
 
    glTranslatef(3.0f,0.0f,0.0f);         // Move Right 3 Units
383
 
 
384
 
    // Enable blending
 
366
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        /* Clear The Screen And The Depth Buffer */
 
367
    glLoadIdentity();                /* Reset The View */
 
368
 
 
369
    glTranslatef(-1.5f,0.0f,0.0f);        /* Move Left 1.5 Units */
 
370
 
 
371
    /* draw a triangle (in smooth coloring mode) */
 
372
    glBegin(GL_POLYGON);                /* start drawing a polygon */
 
373
    glColor3f(1.0f,0.0f,0.0f);            /* Set The Color To Red */
 
374
    glVertex3f( 0.0f, 1.0f, 0.0f);        /* Top */
 
375
    glColor3f(0.0f,1.0f,0.0f);            /* Set The Color To Green */
 
376
    glVertex3f( 1.0f,-1.0f, 0.0f);        /* Bottom Right */
 
377
    glColor3f(0.0f,0.0f,1.0f);            /* Set The Color To Blue */
 
378
    glVertex3f(-1.0f,-1.0f, 0.0f);        /* Bottom Left */
 
379
    glEnd();                    /* we're done with the polygon (smooth color interpolation) */
 
380
 
 
381
    glTranslatef(3.0f,0.0f,0.0f);         /* Move Right 3 Units */
 
382
 
 
383
    /* Enable blending */
385
384
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
386
385
    glEnable(GL_BLEND);
387
386
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
388
387
 
389
 
    // draw a textured square (quadrilateral)
 
388
    /* draw a textured square (quadrilateral) */
390
389
    glEnable(GL_TEXTURE_2D);
391
390
    glBindTexture(GL_TEXTURE_2D, texture);
392
391
    glColor3f(1.0f,1.0f,1.0f);
394
393
        glUseProgramObjectARB(shaders[current_shader].program);
395
394
    }
396
395
 
397
 
    glBegin(GL_QUADS);                // start drawing a polygon (4 sided)
 
396
    glBegin(GL_QUADS);                /* start drawing a polygon (4 sided) */
398
397
    glTexCoord2f(texcoord[MINX], texcoord[MINY]);
399
 
    glVertex3f(-1.0f, 1.0f, 0.0f);        // Top Left
 
398
    glVertex3f(-1.0f, 1.0f, 0.0f);        /* Top Left */
400
399
    glTexCoord2f(texcoord[MAXX], texcoord[MINY]);
401
 
    glVertex3f( 1.0f, 1.0f, 0.0f);        // Top Right
 
400
    glVertex3f( 1.0f, 1.0f, 0.0f);        /* Top Right */
402
401
    glTexCoord2f(texcoord[MAXX], texcoord[MAXY]);
403
 
    glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
 
402
    glVertex3f( 1.0f,-1.0f, 0.0f);        /* Bottom Right */
404
403
    glTexCoord2f(texcoord[MINX], texcoord[MAXY]);
405
 
    glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left
406
 
    glEnd();                    // done with the polygon
 
404
    glVertex3f(-1.0f,-1.0f, 0.0f);        /* Bottom Left */
 
405
    glEnd();                    /* done with the polygon */
407
406
 
408
407
    if (shaders_supported) {
409
408
        glUseProgramObjectARB(0);
410
409
    }
411
410
    glDisable(GL_TEXTURE_2D);
412
411
 
413
 
    // swap buffers to display, since we're double buffered.
 
412
    /* swap buffers to display, since we're double buffered. */
414
413
    SDL_GL_SwapWindow(window);
415
414
}
416
415
 
422
421
    GLuint texture;
423
422
    GLfloat texcoords[4];
424
423
 
 
424
        /* Enable standard application logging */
 
425
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
426
 
425
427
    /* Initialize SDL for video output */
426
428
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
427
 
        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
 
429
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to initialize SDL: %s\n", SDL_GetError());
428
430
        exit(1);
429
431
    }
430
432
 
431
433
    /* Create a 640x480 OpenGL screen */
432
434
    window = SDL_CreateWindow( "Shader Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL );
433
435
    if ( !window ) {
434
 
        fprintf(stderr, "Unable to create OpenGL window: %s\n", SDL_GetError());
 
436
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create OpenGL window: %s\n", SDL_GetError());
435
437
        SDL_Quit();
436
438
        exit(2);
437
439
    }
438
440
 
439
441
    if ( !SDL_GL_CreateContext(window)) {
440
 
        fprintf(stderr, "Unable to create OpenGL context: %s\n", SDL_GetError());
 
442
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create OpenGL context: %s\n", SDL_GetError());
441
443
        SDL_Quit();
442
444
        exit(2);
443
445
    }
444
446
 
445
447
    surface = SDL_LoadBMP("icon.bmp");
446
448
    if ( ! surface ) {
447
 
        fprintf(stderr, "Unable to load icon.bmp: %s\n", SDL_GetError());
 
449
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load icon.bmp: %s\n", SDL_GetError());
448
450
        SDL_Quit();
449
451
        exit(3);
450
452
    }
454
456
    /* Loop, drawing and checking events */
455
457
    InitGL(640, 480);
456
458
    if (InitShaders()) {
457
 
        printf("Shaders supported, press SPACE to cycle them.\n");
 
459
        SDL_Log("Shaders supported, press SPACE to cycle them.\n");
458
460
    } else {
459
 
        printf("Shaders not supported!\n");
 
461
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Shaders not supported!\n");
460
462
    }
461
463
    done = 0;
462
464
    while ( ! done ) {
489
491
int
490
492
main(int argc, char *argv[])
491
493
{
492
 
    printf("No OpenGL support on this system\n");
 
494
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
493
495
    return 1;
494
496
}
495
497