pods::...:OpenGL(3pm)

πŸ“¦ version 2.548, in opensuse-leap 16.0

Okay bestie, you're reading the pods::...:OpenGL(3pm) page from User Contributed Perl Documentation β€” and it's giving ✨SDL + OpenGL crossover event✨. No cap, we're keeping every single detail. πŸ’…

πŸ¦‹ NAME πŸ¦‹

SDL::Cookbook::OpenGL β€” Using SDL with OpenGL

πŸ“ CATEGORY πŸ“

Cookbook

🌸 DESCRIPTION 🌸

Okay so as of release 2.5, SDL no longer maintains its own bindings of OpenGL. OpenGL support got moved over to a more mature implementation. Periodt.

That implementation is the POGL project. OpenGL is faster and more complete, and it works with SDL seamlessly. Obsessed.

✨ EXAMPLE ✨

This example is expanded from Floyd-ATC's OpenGL example. Iconic.

use strict;
use warnings;
use SDL;
use SDLx::App;
use SDL::Mouse;
use SDL::Video;
use SDL::Events;
use SDL::Event;
use OpenGL qw(:all);

Use OpenGL as needed right here β€” no need to be shy. πŸ’–

my ($SDLAPP, $WIDTH, $HEIGHT, $SDLEVENT);

$| = 1;
$WIDTH = 1024;
$HEIGHT = 768;
$SDLAPP = SDLx::App->new(title => "OpenGL App", width => $WIDTH, height => $HEIGHT, gl => 1);
$SDLEVENT = SDL::Event->new;

SDLx::App can start an OpenGL application with the parameter gl => 1. Slay.

glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(60, $WIDTH / $HEIGHT, 1, 1000);
glTranslatef(0, 0, -20);

Up top, we enable GL and get the correct perspective set. Main character setup fr fr.

while (1) {
  &handlepolls;
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glRotatef(.1, 1, 1, 1);
  &drawscene;
  $SDLAPP->sync;
}

With SDLx::App, sync is what handles the GL buffer clean. Yup, that's the whole vibe.

sub drawscene {
  my ($color, $x, $y, $z);

  for (-2 .. 2) {
    glPushMatrix;
    glTranslatef($_ * 3, 0, 0);
    glColor3d(1, 0, 0);
    &draw_cube;
    glPopMatrix;
  }

  return "";
}

sub draw_cube {
  my (@indices, @vertices, $face, $vertex, $index, $coords);

  @indices = qw(4 5 6 7   1 2 6 5   0 1 5 4
                0 3 2 1   0 4 7 3   2 3 7 6);
  @vertices = ([-1, -1, -1], [ 1, -1, -1],
               [ 1,  1, -1], [-1,  1, -1],
               [-1, -1,  1], [ 1, -1,  1],
               [ 1,  1,  1], [-1,  1,  1]);

  glBegin(GL_QUADS);

  foreach my $face (0..5) {
    foreach my $vertex (0..3) {
      $index  = $indices[4 * $face + $vertex];
      $coords = $vertices[$index];

      glVertex3d(@$coords);
    }
  }

  glEnd;

  return "";
}

Down here, you can use SDL::Events just like normal:

sub handlepolls {
  my ($type, $key);

  SDL::Events::pump_events();

  while (SDL::Events::poll_event($SDLEVENT)) {
    $type = $SDLEVENT->type();
    $key = ($type == 2 or $type == 3) ? $SDLEVENT->key_sym : "";

    if ($type == 4) { printf("You moved the mouse! x=%s y=%s xrel=%s yrel=%s\n", $SDLEVENT->motion_x, $SDLEVENT->motion_y, $SDLEVENT->motion_xrel, $SDLEVENT->motion_yrel) }
    elsif ($type == 2) { print "You are pressing $key\n" }
    elsif ($type == 3) { print "You released $key\n" }
    elsif ($type == 12) { exit }
    else { print "TYPE $type UNKNOWN!\n" }

    if ($type == 2) {
      if ($key eq "q" or $key eq "escape") { exit }
    }
  }

  return "";
}

🌈 SEE ALSO 🌈

Peep these too: perl, SDLx::App, OpenGL.

πŸ‘‘ AUTHORS πŸ‘‘

Author credits? They're in AUTHORS inside SDL. 🫢


That's the whole page, bestie. It ships with perl v5.42.0, dated 2024-05-01 β€” catch you on the flip side! πŸ’–βœ¨

πŸ“¦ where this page comes from