Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjglgamedev committed Dec 26, 2019
2 parents d345838 + 0486c51 commit 7891d01
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion chapter06/chapter6.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public void render(Window window, GameItem[] gameItems) {
gameItem.getRotation(),
gameItem.getScale());
shaderProgram.setUniform("worldMatrix", worldMatrix);
// Render the mes for this game item
// Render the mesh for this game item
gameItem.getMesh().render();
}

Expand Down
14 changes: 9 additions & 5 deletions chapter07/chapter7.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
```

Now we need to use the texture in our shader. In the vertex shader we have changed the second uniform parameter because now it’s a `vec2` \(we also changed the uniform name, so remember to change it in the `Renderer` class\). The vertex shader, as in the colour case, just passes the texture coordinates to be used by the fragment shader.
Now we need to use the texture in our shader. In the vertex shader we have changed the second input parameter because now it’s a `vec2` \(we also changed the parameter name). The vertex shader, as in the colour case, just passes the texture coordinates to be used by the fragment shader.

```glsl
#version 330
Expand Down Expand Up @@ -277,7 +277,7 @@ void main()

Before analyzing the code let’s clarify some concepts. A graphics card has several spaces or slots to store textures. Each of these spaces is called a texture unit. When we are working with textures we must set the texture unit that we want to work with. As you can see we have a new uniform named `texture_sampler`. That uniform has a `sampler2D` type and will hold the value of the texture unit that we want to work with.

In the main function we use the texture lookup function named `texture`. This function takes two arguments: a sampler and a texture coordinate and will return the correct colour. The sampler uniform allows us to do multi-texturing. We will not cover that topic right now but we will try to prepare the code to add it easily later on.
In the `main` function we use the texture lookup function named `texture`. This function takes two arguments: a sampler and a texture coordinate and will return the correct colour. The sampler uniform allows us to do multi-texturing. We will not cover that topic right now but we will try to prepare the code to add it easily later on.

Thus, in our `ShaderProgram` class we will create a new method that allows us to set an integer value for a uniform:

Expand All @@ -299,7 +299,7 @@ Also, in the `render` method of our `Renderer` class we will set the uniform val
shaderProgram.setUniform("texture_sampler", 0);
```

Finally we just need to change the render method of the `Mesh` class to use the texture. At the beginning of that method we put the following lines:
Finally we just need to change the `render` method of the `Mesh` class to use the texture. At the beginning of that method we put the following lines:

```java
// Activate first texture unit
Expand Down Expand Up @@ -348,11 +348,15 @@ In the next chapters we will learn how to load models generated by 3D modeling t

As you have seen, when we load an image we retrieve the four RGBA components including the transparency level. But if you load a texture with transparencies you may not see anything. In order to support transparency we need to enable blending. This is done by this fragment of code:

`glEnable(GL_BLEND);`
```java
glEnable(GL_BLEND);
```

But just by enabling blending, transparencies still will not show up. We need also to instruct OpenGL about how the blending will be applied. This is done through the `glBlendFunc` method:

`glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);`
```java
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
```

You can check an excellent explanation about the details of the different functions that can be applied [here]( https://learnopengl.com/Advanced-OpenGL/Blending).

Expand Down
2 changes: 1 addition & 1 deletion chapter08/chapter8.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Matrix4f getViewMatrix(Camera camera) {
}
```

As you can see, we first need to do the rotation and then the translation. If we did the opposite we would not be rotating along the camera position but along the coordinates origin. Please also note that in the `movePosition` method of the `Camera` class we just not simply increase the camera position by an offset. We also take into consideration the rotation along the y axis, the yaw, in order to calculate the final position. If we would just increase the camera position by the offset the camera will not move in the direction its facing.
As you can see, we first need to do the rotation and then the translation. If we did the opposite we would not be rotating along the camera position but along the coordinates origin. Please also note that in the `movePosition` method of the `Camera` class we just not simply increase the camera position by an offset. We also take into consideration the rotation along the y axis, the yaw, in order to calculate the final position. If we would just increase the camera position by the offset the camera will not move in the direction it's facing.

Besides what is mentioned above, we do not have here a full free fly camera \(for instance, if we rotate along the x axis the camera does not move up or down in the space when we move it forward\). This will be done in later chapters since is a little bit more complex.

Expand Down
2 changes: 1 addition & 1 deletion chapter09/chapter9.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private static void processFaceVertex(IdxGroup indices, List<Vector2f> textCoord
texCoordArr[posIndex * 2 + 1] = 1 - textCoord.y;
}
if (indices.idxVecNormal >= 0) {
// Reorder vectornormals
// Reorder normal vectors
Vector3f vecNorm = normList.get(indices.idxVecNormal);
normArr[posIndex * 3] = vecNorm.x;
normArr[posIndex * 3 + 1] = vecNorm.y;
Expand Down
2 changes: 1 addition & 1 deletion chapter12/chapter12.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ Before reviewing other topics let’s go back to the text rendering approach we
* If you want to use several fonts you need to create a separate texture file for each font. Also, the only way to change the text size is either to scale it, which may result in a poor quality rendered text, or to generate another texture file.
* The most important one, characters in most of the fonts do not occupy the same size but we are dividing the font texture in equally sized elements. We have cleverly used “Consolas” font which is [monospaced](https://en.wikipedia.org/wiki/Monospaced_font) \(that is, all the characters occupy the same amount of horizontal space\), but if you use a non-monospaced font you will see annoying variable white spaces between the characters.

We need to change our approach an provide a more flexible way to render text. If you think about it, the overall mechanism is OK, that is, the way of rendering text by texturing quads for each character. The issue here is how we are generating the textures. We need to be able to generate those texture dynamically by using the fonts available in the System.
We need to change our approach and provide a more flexible way to render text. If you think about it, the overall mechanism is OK, that is, the way of rendering text by texturing quads for each character. The issue here is how we are generating the textures. We need to be able to generate those texture dynamically by using the fonts available in the System.

This is where `java.awt.Font` comes to the rescue: with it, we will generate the textures by drawing each letter for a specified font family and size dynamically. That texture will be used in the same way as described previously, but it will solve perfectly all the issues mentioned above. We will create a new class named `FontTexture` that will receive a Font instance and a charset name and will dynamically create a texture that contains all the available characters. This is the constructor.

Expand Down
2 changes: 1 addition & 1 deletion chapter13/chapter13.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The process of creating a sky box can be summarized in the following steps:

Let’s start with the texture. You will find that there are lots of pre-generated textures for you to use in the internet. The one used in the sample for this chapter has been downloaded from here: [http://www.custommapmakers.org/skyboxes.php](http://www.custommapmakers.org/skyboxes.php). The concrete sample that we have used is this one: [http://www.custommapmakers.org/skyboxes/zips/ely\_hills.zip](http://www.custommapmakers.org/skyboxes/zips/ely_hills.zip) and has been created by Colin Lowndes.

The textures from that site are composed by separate TGA files, one for each side of the cube. The texture loader that we have created expects a single file in PNG format so we need to compose a single PNG image with the images of each face. We could apply other techniques, such us cube mapping, in order to apply the textures automatically. But, in order to keep this chapter as simple as possible, you will have to manually arrange them into a single file. The result image will look like this.
The textures from that site are composed by separate TGA files, one for each side of the cube. The texture loader that we have created expects a single file in PNG format so we need to compose a single PNG image with the images of each face. We could apply other techniques, such as cube mapping, in order to apply the textures automatically. But, in order to keep this chapter as simple as possible, you will have to manually arrange them into a single file. The result image will look like this.

![Sky Box Texture](skybox_texture.png)

Expand Down

0 comments on commit 7891d01

Please sign in to comment.