Path: blob/main_old/samples/sample_util/texture_utils.cpp
1695 views
//1// Copyright 2014 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "texture_utils.h"7#include <array>89GLuint CreateSimpleTexture2D()10{11// Use tightly packed data12glPixelStorei(GL_UNPACK_ALIGNMENT, 1);1314// Generate a texture object15GLuint texture;16glGenTextures(1, &texture);1718// Bind the texture object19glBindTexture(GL_TEXTURE_2D, texture);2021// Load the texture: 2x2 Image, 3 bytes per pixel (R, G, B)22const size_t width = 2;23const size_t height = 2;24GLubyte pixels[width * height * 3] = {25255, 0, 0, // Red260, 255, 0, // Green270, 0, 255, // Blue28255, 255, 0, // Yellow29};30glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);3132// Set the filtering mode33glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);34glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);3536return texture;37}3839GLuint CreateSimpleTextureCubemap()40{41// Generate a texture object42GLuint texture;43glGenTextures(1, &texture);4445// Bind the texture object46glBindTexture(GL_TEXTURE_CUBE_MAP, texture);4748// Load the texture faces49GLubyte pixels[6][3] = {// Face 0 - Red50{255, 0, 0},51// Face 1 - Green,52{0, 255, 0},53// Face 3 - Blue54{0, 0, 255},55// Face 4 - Yellow56{255, 255, 0},57// Face 5 - Purple58{255, 0, 255},59// Face 6 - White60{255, 255, 255}};6162for (size_t i = 0; i < 6; i++)63{64glTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, GL_RGB, 1, 1, 0,65GL_RGB, GL_UNSIGNED_BYTE, &pixels[i]);66}6768// Set the filtering mode69glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);70glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);7172return texture;73}7475GLuint CreateMipMappedTexture2D()76{77// Texture object handle78const size_t width = 256;79const size_t height = 256;80std::array<GLubyte, width * height * 3> pixels;8182const size_t checkerSize = 8;83for (size_t y = 0; y < height; y++)84{85for (size_t x = 0; x < width; x++)86{87GLubyte rColor = 0;88GLubyte bColor = 0;8990if ((x / checkerSize) % 2 == 0)91{92rColor = 255 * ((y / checkerSize) % 2);93bColor = 255 * (1 - ((y / checkerSize) % 2));94}95else96{97bColor = 255 * ((y / checkerSize) % 2);98rColor = 255 * (1 - ((y / checkerSize) % 2));99}100101pixels[(y * height + x) * 3] = rColor;102pixels[(y * height + x) * 3 + 1] = 0;103pixels[(y * height + x) * 3 + 2] = bColor;104}105}106107// Generate a texture object108GLuint texture;109glGenTextures(1, &texture);110111// Bind the texture object112glBindTexture(GL_TEXTURE_2D, texture);113114// Load mipmap level 0115glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,116pixels.data());117118// Generate mipmaps119glGenerateMipmap(GL_TEXTURE_2D);120121// Set the filtering mode122glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);123glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);124125return texture;126}127128129