I am trying to draw a 12 bytes long array of rgb values. Since rgb is 3 bytes this should total 4 pixels held within the array.All the pixels are red. Thus I am trying to draw a 2*2 square of red pixels. The problem is that although the second row of rgb values is the desired colour, the top row is green, which is not the desired colour. The code works when I change the array to work with RGBA instead of rgb: GLubyte texDat[16] = {255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255}; and I change to RGBA mode in glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA , GL_UNSIGNED_BYTE, texDat);, however I do not want to use RGBA, I need RGB for the project I am working on.
code:
#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <Windows.h>
#define width 4
#define height 0
int main(int argc, char** argv)
{
//create GL context
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(480, 240);
glutCreateWindow("Demo");
GLubyte texDat[12] = {255,0,0,255,0,0,255,0,0,255,0,0};
//upload to GPU texture
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB , GL_UNSIGNED_BYTE, texDat);
glBindTexture(GL_TEXTURE_2D, 0);
//match projection to window resolution (could be in reshape callback)
glMatrixMode(GL_PROJECTION);
glOrtho(0, 800, 0, 600, -1, 1);
glMatrixMode(GL_MODELVIEW);
//clear and draw quad with texture (could be in display callback)
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(100, 100);
glTexCoord2i(0, 1); glVertex2i(100, 500);
glTexCoord2i(1, 1); glVertex2i(500, 500);
glTexCoord2i(1, 0); glVertex2i(500, 100);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers(); //don't need this with GLUT_DOUBLE and glutSwapBuffers
system("pause"); //I think this works on windows
return 0;
}
output:
expected output:

