MCPcopy Create free account
hub / github.com/BohemiaInteractive/CWR / InitFromRGBA

Method InitFromRGBA

engine/PoseidonGL33/TextureGL33_Init.cpp:513–583  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

511}
512
513bool TextureGL33::InitFromRGBA(int w, int h, const void* rgba, uint32_t size, bool mipmap)
514{
515 if (!rgba)
516 return false;
517
518 _initialized = true;
519 _dynamicMipmapped = mipmap;
520
521 int maxLevel = 0;
522 if (mipmap)
523 {
524 int dim = w > h ? w : h;
525 while (dim > 1)
526 {
527 dim >>= 1;
528 maxLevel++;
529 }
530 }
531 _nMipmaps = maxLevel + 1;
532 _mipmaps[0]._w = static_cast<short>(w);
533 _mipmaps[0]._h = static_cast<short>(h);
534 _mipmaps[0]._pitch = static_cast<short>(w * 4);
535 _maxSize = w > h ? w : h;
536
537 unsigned int tex = 0;
538 glGenTextures(1, &tex);
539 if (!tex)
540 return false;
541
542 _surface.SetTexture(tex);
543 GL33Bind::Tex2D(EngineGL33::kUploadUnit - GL_TEXTURE0, tex);
544 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
545
546 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
547 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevel);
548 if (mipmap)
549 {
550 glGenerateMipmap(GL_TEXTURE_2D);
551 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
552 // Anisotropic filter keeps tilted-surface sampling crisp — isotropic LOD
553 // over-blurs because the per-fragment UV footprint is elongated.
554 float maxAniso = 1.0f;
555 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
556 if (maxAniso > 1.0f)
557 {
558 float aniso = maxAniso < 8.0f ? maxAniso : 8.0f;
559 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
560 }
561 }
562 else
563 {
564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
565 }
566 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
567 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
568 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
569 GL33Bind::ActiveUnit(0);
570

Callers 1

CreateDynamicMethod · 0.80

Calls 3

Tex2DFunction · 0.85
ActiveUnitFunction · 0.85
SetTextureMethod · 0.45

Tested by

no test coverage detected