---
title: "OpenGL SuperBible in Java: Your first triangle"
description: "Learn to draw a triangle in Java using OpenGL and LWJGL. This tutorial provides a concise code example demonstrating how to set up a basic OpenGL environment, initialize shaders, and render a red triangle to the screen.  The code covers clearing the color buffer, using an identity shader, and updating the display.  Improve your understanding of OpenGL fundamentals and begin creating your own 2D graphics.\n"
slug: "OpenGL-Superbible-in-Java-Your-First-Triangle"
created: 2011-11-11T00:00:00Z
updated: 2011-11-11T00:00:00Z
tags:
  - "java"
  - "opengl"
  - "opengl-superbible"
  - "lwjgl"
ai_assisted: false
---

On the last tutorials, there was a lot of code that we used to build a framework and encapsulate the complexity of the shaders. Today, it's time for that work to start paying off and getting something actually drawn on our screen!

Besides using the code on OpenGL Superbible, the code in this article was inspired by the tutorials on [LWJGL's wiki page][2] and a few other searches on Google, to figure out the LWJGL specific parts, like initalizing screen and etc.

Again, all the code is available on [http://code.google.com/p/opengl-superbible-java/][1]

First, i won't cover the details on LWJGL's implementation. The code is fairly simple and their wiki and docs should clear any doubts.

Taking out the LWJGL initialization code, the example turns out to be very short. There are two instance variables that are important to our example in the Triangle class. The triangleBatch and shader instances. The first is a `GLBatch` which has the responsability of drawing our triangle. The second is shader used to draw this triangle.

Those variables are initialized on the `initGL` method. Heres a transcription of the code:

```java
public void initGL() {
    glClearColor(0.0f,0.0f,0.0f,0.0f);

    shader = GLShaderFactory.getIdentityShader();
    triangleBatch = new SimpleGLBatch(GL11.GL_TRIANGLES,
            new float[]{ 0.0f, 0.5f, 0.0f, 1.0f, //vertex 1
                        -0.5f, -0.5f, 0.0f, 1.0f, //vertex 2
                          0.5f, -0.5f, 0.0f, 1.0f}, //vertex 3
            new short[]{0, 1, 2});
}
```

The `glClearColor` call specifies which color to using when clearing the color buffers. You can get more details here.

The shader initialization just uses the default Identity Shader from `GLShaderFactory`. This shader does not make any transformation on the vertex.

The next line initializes the GLBatch informing that it should use `GL_TRIANGLES` to draw the vertices, the vertices values.  Each vertex has 4 float values. `x`, `y`, `z` and `scale`, and, at last, the indexes of the vertices.

The next important line of code is:

```java
public void resizeGL() {
    glViewport(0,0,DISPLAY_WIDTH ,DISPLAY_HEIGHT);
}
```

This code simply resizes the OpenGL viewport when the window size is changed.

Now, we have to actually draw the triangle on the screen, and thats what the render method does:

```java
public void render() {
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    shader.useShader();
    shader.setUniform4("vColor", 1.0f, 0.0f, 0.0f, 1.0f);
    triangleBatch.draw(shader.getAttributeLocations());
    Display.update();
}
```
The first step is clearing the screen, with a call to [`glClear`][3]. Then, we tell the shader we want to use it, with `shader.useShader()`. The next step is telling the shader which color we want to paint our triangle. The identity shader user a uniform called vColor to do that.  The, we can draw the triangleBatch and ask LWJGL to swap the screen buffer with `Display.update()`. Thats it, we got a triangle on our screen.

![A red triangle](/img/2011/11/triangle.png "The resulting triangle")


You may notice that if you change the screen size, the triangle will change its shape. That's because we are using the identity shader, which maps the viewport to coordinates between `-1.0` and `1.0`. On the next tutorials, we will see how to draw our triangle without losing the proportion.

[1]: http://code.google.com/p/opengl-superbible-java/
[2]: http://lwjgl.org/wiki/index.php?title=Main_Page
[3]: http://www.opengl.org/sdk/docs/man/xhtml/glClear.xml