1//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "texture_utils.h"
8#include <array>
9
10GLuint CreateSimpleTexture2D()
11{
12    // Use tightly packed data
13    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
14
15    // Generate a texture object
16    GLuint texture;
17    glGenTextures(1, &texture);
18
19    // Bind the texture object
20    glBindTexture(GL_TEXTURE_2D, texture);
21
22    // Load the texture: 2x2 Image, 3 bytes per pixel (R, G, B)
23    const size_t width = 2;
24    const size_t height = 2;
25    GLubyte pixels[width * height * 3] =
26    {
27        255,   0,   0, // Red
28          0, 255,   0, // Green
29          0,   0, 255, // Blue
30        255, 255,   0, // Yellow
31    };
32    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
33
34    // Set the filtering mode
35    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
36    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
37
38    return texture;
39}
40
41GLuint CreateSimpleTextureCubemap()
42{
43    // Generate a texture object
44    GLuint texture;
45    glGenTextures(1, &texture);
46
47    // Bind the texture object
48    glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
49
50    // Load the texture faces
51    GLubyte pixels[6][3] =
52    {
53        // Face 0 - Red
54        255, 0, 0,
55        // Face 1 - Green,
56        0, 255, 0,
57        // Face 3 - Blue
58        0, 0, 255,
59        // Face 4 - Yellow
60        255, 255, 0,
61        // Face 5 - Purple
62        255, 0, 255,
63        // Face 6 - White
64        255, 255, 255
65    };
66
67    for (size_t i = 0; i < 6; i++)
68    {
69        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixels[i]);
70    }
71
72    // Set the filtering mode
73    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
74    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75
76    return texture;
77}
78
79GLuint CreateMipMappedTexture2D()
80{
81    // Texture object handle
82    const size_t width = 256;
83    const size_t height = 256;
84    std::array<GLubyte, width * height * 3> pixels;
85
86    const size_t checkerSize = 8;
87    for (GLsizei y = 0; y < height; y++)
88    {
89        for (GLsizei x = 0; x < width; x++)
90        {
91            GLubyte rColor = 0;
92            GLubyte bColor = 0;
93
94            if ((x / checkerSize) % 2 == 0)
95            {
96                rColor = 255 * ((y / checkerSize) % 2);
97                bColor = 255 * (1 - ((y / checkerSize) % 2));
98            }
99            else
100            {
101                bColor = 255 * ((y / checkerSize) % 2);
102                rColor = 255 * (1 - ((y / checkerSize) % 2));
103            }
104
105            pixels[(y * height + x) * 3] = rColor;
106            pixels[(y * height + x) * 3 + 1] = 0;
107            pixels[(y * height + x) * 3 + 2] = bColor;
108        }
109    }
110
111    // Generate a texture object
112    GLuint texture;
113    glGenTextures(1, &texture);
114
115    // Bind the texture object
116    glBindTexture(GL_TEXTURE_2D, texture);
117
118    // Load mipmap level 0
119    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
120
121    // Generate mipmaps
122    glGenerateMipmap(GL_TEXTURE_2D);
123
124    // Set the filtering mode
125    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
126    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127
128    return texture;
129}
130