1.md 1.16 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1
# Hello GL
2

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
This minimal example shows the classical "Hello World" of OpenGL 2D drawing, showing a nice 2D gradient where:

- The RED component increases with the X position of the pixel.
- The GREEN component increases with the Y position of the pixel.

> N.B. a GLSL fragment uses a ["functional rendering"](http://greweb.me/2013/11/functional-rendering/)
paradigm, which means you have to implement a `Position => Color` function.

![](1.jpg)

```html
<HelloGL width={256} height={171} />
```

## Implementation

```js
const React = require("react-native");
const GL = require("gl-react-native");

const shaders = GL.Shaders.create({
  helloGL: {
    frag: `
precision highp float;
varying vec2 uv; // This variable vary in all pixel position (normalized from vec2(0.0,0.0) to vec2(1.0,1.0))

void main () { // This function is called FOR EACH PIXEL
  gl_FragColor = vec4(uv.x, uv.y, 0.5, 1.0); // red vary over X, green vary over Y, blue is 50%, alpha is 100%.
}
    `
  }
});

class HelloGL extends React.Component {
  render () {
    const { width, height } = this.props;
    return <GL.View
      shader={shaders.helloGL}
      width={width}
      height={height}
    />;
  }
}
```