1 //
2 // Copyright 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 #include <cstddef>
10
CreateSimpleTexture2D()11 GLuint CreateSimpleTexture2D()
12 {
13 // Use tightly packed data
14 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
15
16 // Generate a texture object
17 GLuint texture;
18 glGenTextures(1, &texture);
19
20 // Bind the texture object
21 glBindTexture(GL_TEXTURE_2D, texture);
22
23 // Load the texture: 2x2 Image, 3 bytes per pixel (R, G, B)
24 const size_t width = 2;
25 const size_t height = 2;
26 GLubyte pixels[width * height * 3] = {
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
CreateSimpleTextureCubemap()41 GLuint 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] = {// Face 0 - Red
52 {255, 0, 0},
53 // Face 1 - Green,
54 {0, 255, 0},
55 // Face 3 - Blue
56 {0, 0, 255},
57 // Face 4 - Yellow
58 {255, 255, 0},
59 // Face 5 - Purple
60 {255, 0, 255},
61 // Face 6 - White
62 {255, 255, 255}};
63
64 for (size_t i = 0; i < 6; i++)
65 {
66 glTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, GL_RGB, 1, 1, 0,
67 GL_RGB, GL_UNSIGNED_BYTE, &pixels[i]);
68 }
69
70 // Set the filtering mode
71 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
73
74 return texture;
75 }
76
CreateMipMappedTexture2D()77 GLuint CreateMipMappedTexture2D()
78 {
79 // Texture object handle
80 const size_t width = 256;
81 const size_t height = 256;
82 std::array<GLubyte, width * height * 3> pixels;
83
84 const size_t checkerSize = 8;
85 for (size_t y = 0; y < height; y++)
86 {
87 for (size_t x = 0; x < width; x++)
88 {
89 GLubyte rColor = 0;
90 GLubyte bColor = 0;
91
92 if ((x / checkerSize) % 2 == 0)
93 {
94 rColor = 255 * ((y / checkerSize) % 2);
95 bColor = 255 * (1 - ((y / checkerSize) % 2));
96 }
97 else
98 {
99 bColor = 255 * ((y / checkerSize) % 2);
100 rColor = 255 * (1 - ((y / checkerSize) % 2));
101 }
102
103 pixels[(y * height + x) * 3] = rColor;
104 pixels[(y * height + x) * 3 + 1] = 0;
105 pixels[(y * height + x) * 3 + 2] = bColor;
106 }
107 }
108
109 // Generate a texture object
110 GLuint texture;
111 glGenTextures(1, &texture);
112
113 // Bind the texture object
114 glBindTexture(GL_TEXTURE_2D, texture);
115
116 // Load mipmap level 0
117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
118 pixels.data());
119
120 // Generate mipmaps
121 glGenerateMipmap(GL_TEXTURE_2D);
122
123 // Set the filtering mode
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126
127 return texture;
128 }
129