• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CreateSimpleTexture2D()10 GLuint 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         255, 0,   0,    // Red
27         0,   255, 0,    // Green
28         0,   0,   255,  // Blue
29         255, 255, 0,    // Yellow
30     };
31     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
32 
33     // Set the filtering mode
34     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
35     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
36 
37     return texture;
38 }
39 
CreateSimpleTextureCubemap()40 GLuint CreateSimpleTextureCubemap()
41 {
42     // Generate a texture object
43     GLuint texture;
44     glGenTextures(1, &texture);
45 
46     // Bind the texture object
47     glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
48 
49     // Load the texture faces
50     GLubyte pixels[6][3] = {// Face 0 - Red
51                             {255, 0, 0},
52                             // Face 1 - Green,
53                             {0, 255, 0},
54                             // Face 3 - Blue
55                             {0, 0, 255},
56                             // Face 4 - Yellow
57                             {255, 255, 0},
58                             // Face 5 - Purple
59                             {255, 0, 255},
60                             // Face 6 - White
61                             {255, 255, 255}};
62 
63     for (size_t i = 0; i < 6; i++)
64     {
65         glTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, GL_RGB, 1, 1, 0,
66                      GL_RGB, GL_UNSIGNED_BYTE, &pixels[i]);
67     }
68 
69     // Set the filtering mode
70     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
71     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
72 
73     return texture;
74 }
75 
CreateMipMappedTexture2D()76 GLuint CreateMipMappedTexture2D()
77 {
78     // Texture object handle
79     const size_t width  = 256;
80     const size_t height = 256;
81     std::array<GLubyte, width * height * 3> pixels;
82 
83     const size_t checkerSize = 8;
84     for (size_t y = 0; y < height; y++)
85     {
86         for (size_t x = 0; x < width; x++)
87         {
88             GLubyte rColor = 0;
89             GLubyte bColor = 0;
90 
91             if ((x / checkerSize) % 2 == 0)
92             {
93                 rColor = 255 * ((y / checkerSize) % 2);
94                 bColor = 255 * (1 - ((y / checkerSize) % 2));
95             }
96             else
97             {
98                 bColor = 255 * ((y / checkerSize) % 2);
99                 rColor = 255 * (1 - ((y / checkerSize) % 2));
100             }
101 
102             pixels[(y * height + x) * 3]     = rColor;
103             pixels[(y * height + x) * 3 + 1] = 0;
104             pixels[(y * height + x) * 3 + 2] = bColor;
105         }
106     }
107 
108     // Generate a texture object
109     GLuint texture;
110     glGenTextures(1, &texture);
111 
112     // Bind the texture object
113     glBindTexture(GL_TEXTURE_2D, texture);
114 
115     // Load mipmap level 0
116     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
117                  pixels.data());
118 
119     // Generate mipmaps
120     glGenerateMipmap(GL_TEXTURE_2D);
121 
122     // Set the filtering mode
123     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
124     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
125 
126     return texture;
127 }
128