Jump to content

Let's create WebGL examples for practice


8Observer8
 Share

Recommended Posts

To create a desktop application from WebGL, you can rewrite it to OpenGL. Python is one of the easiest languages to build desktop applications.

Drawing Box2D physics engine colliders with b2Draw, OpenGL1, PyQt6 and Python

The example shows how to draw colliders using b2Draw. Uses OpenGL version 1 for simplicity.

pyBox2D works with Python 3.8. Download and install Python 3.8 from here: https://www.python.org/downloads/

Install the required packages with this command from CMD:

pip install Box2D PyQt6 PyOpenGL

Download the source code: https://github.com/8Observer8/edit-gravity-debug-drawer-opengl1-pyqt6

Go to your project folder and type this command to run the application: python main.py (or double click on main.py)

Install the very useful isort module (pip install isort), which allows you to sort plugins alphabetically with the command: isort . (dot means to sort in all files in the current directory)

The Notepad++ code editor has syntax highlighting for Python, JavaScript, HTML, C++, and more by default. There is a plugin (https://github.com/danielscherzer/NotepaddPP-glsl-integration) for syntax highlighting GLSL (OpenGL Shading Language). You can make the working directory the directory of the current file, which will open in the left pane. To do this, right-click on the file in the editor tab and select "Open into" -> "Open Containing Folder as Workspace". You can invoke the CMD (console) on Windows from Notapad++ by opening a code file in the editor, right-clicking on the file in the editor tab and selecting "Open into" -> "Open Containing Folder in cmd".

fb996c81-97b7-4638-bf04-ecbc7e6a32c7.gif.c29f73dc82e7ede8f27b541c69194060.gif

edit-gravity-debug-drawer-opengl3-pyqt6.gif.5a140bca68cd5dd0b1eebdd5c8b5fe9a.gif

Edited by 8Observer8
Link to comment
Share on other sites

  • 3 weeks later...

Here are two examples of using WebGL to create simple graphics in a web browser:

  1. Drawing a Triangle:
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>
    <script>
      // Set up the scene
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(
        75,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      // Create the triangle
      const geometry = new THREE.Geometry();
      geometry.vertices.push(
        new THREE.Vector3(-1, -1, 0),
        new THREE.Vector3(0, 1, 0),
        new THREE.Vector3(1, -1, 0)
      );
      geometry.faces.push(new THREE.Face3(0, 1, 2));
      const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
      const triangle = new THREE.Mesh(geometry, material);
      scene.add(triangle);

      // Render the scene
      camera.position.z = 5;
      renderer.render(scene, camera);
    </script>
  </head>
  <body>
  </body>
</html>
  1. Rotating a Cube:
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>
    <script>
      // Set up the scene
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(
        75,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      // Create the cube
      const geometry = new THREE.BoxGeometry(1, 1, 1);
      const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
      const cube = new THREE.Mesh(geometry, material);
      scene.add(cube);

      // Render the scene
      camera.position.z = 5;
      renderer.render(scene, camera);
      const render = function () {
        requestAnimationFrame(render);
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        renderer.render(scene, camera);
      };
      render();
    </script>
  </head>
  <body>
  </body>
</html>

These examples show the basics of setting up a scene, creating objects, and rendering them in a web browser using Three.js.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...