这是我的确切代码的简化版本,
我希望看到所有红色(因为我只将红色设置为 255,其他设置为 0),但实际上得到了这个:
#include <GLUT/glut.h>
static GLuint texture;
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -2, 2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void callback(unsigned char *data, long width, long height) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
int i,j;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
data[i * 3 * width + 3 * j] = 255;
data[i * 3 * width + 3 * j + 1] = 0;
data[i * 3 * width + 3 * j + 2] = 0;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
}
void init() {
glClearColor(0, 0, 0, 0);//RGBA
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
unsigned char data[3 * 350 * 168];
callback(data, 350, 168);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);glVertex2f(-2, -2);
glTexCoord2f(0, 1);glVertex2f(-2, 2);
glTexCoord2f(1, 1);glVertex2f(2, 2);
glTexCoord2f(1, 0);glVertex2f(2, -2);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("hello world texture window");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
init();
glutMainLoop();
return 0;
}
我在这里做错了什么?