Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>glTexImage2D</code> <em>is correct</em>, so that's not the problem.</p> <p>I do need (when loading TGA files) to swap the color components. Try this:</p> <pre><code>typedef struct { unsigned char imageTypeCode; short int imageWidth; short int imageHeight; unsigned char bitCount; unsigned char *imageData; } TGAFILE; bool LoadTGAFile(char *filename, TGAFILE *tgaFile) { FILE *filePtr; unsigned char ucharBad; short int sintBad; long imageSize; int colorMode; unsigned char colorSwap; // Open the TGA file. filePtr = fopen(filename, "rb"); if (filePtr == NULL) { return false; } // Read the two first bytes we don't need. fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr); fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr); // Which type of image gets stored in imageTypeCode. fread(&amp;tgaFile-&gt;imageTypeCode, sizeof(unsigned char), 1, filePtr); // For our purposes, the type code should be 2 (uncompressed RGB image) // or 3 (uncompressed black-and-white images). if (tgaFile-&gt;imageTypeCode != 2 &amp;&amp; tgaFile-&gt;imageTypeCode != 3) { fclose(filePtr); return false; } // Read 13 bytes of data we don't need. fread(&amp;sintBad, sizeof(short int), 1, filePtr); fread(&amp;sintBad, sizeof(short int), 1, filePtr); fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr); fread(&amp;sintBad, sizeof(short int), 1, filePtr); fread(&amp;sintBad, sizeof(short int), 1, filePtr); // Read the image's width and height. fread(&amp;tgaFile-&gt;imageWidth, sizeof(short int), 1, filePtr); fread(&amp;tgaFile-&gt;imageHeight, sizeof(short int), 1, filePtr); // Read the bit depth. fread(&amp;tgaFile-&gt;bitCount, sizeof(unsigned char), 1, filePtr); // Read one byte of data we don't need. fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr); // Color mode -&gt; 3 = BGR, 4 = BGRA. colorMode = tgaFile-&gt;bitCount / 8; imageSize = tgaFile-&gt;imageWidth * tgaFile-&gt;imageHeight * colorMode; // Allocate memory for the image data. tgaFile-&gt;imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize); // Read the image data. fread(tgaFile-&gt;imageData, sizeof(unsigned char), imageSize, filePtr); // Change from BGR to RGB so OpenGL can read the image data. for (int imageIdx = 0; imageIdx &lt; imageSize; imageIdx += colorMode) { colorSwap = tgaFile-&gt;imageData[imageIdx]; tgaFile-&gt;imageData[imageIdx] = tgaFile-&gt;imageData[imageIdx + 2]; tgaFile-&gt;imageData[imageIdx + 2] = colorSwap; } fclose(filePtr); return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload