C 带有不想要的颜色的纹理

C 带有不想要的颜色的纹理,c,opengl,textures,glut,C,Opengl,Textures,Glut,我正在尝试学习如何在OpenGL中使用纹理。我没有安装freeimage,所以我必须使用for循环创建位图。 我想做的是简单地获取一个包含所有红点的纹理,并将其映射到一个正方形中: #import <OpenGL/OpenGL.h> #import <GLUT/GLUT.h> #import <stdlib.h> #import <string.h> #import <math.h> GLuint texture; GLfloat

我正在尝试学习如何在OpenGL中使用纹理。我没有安装freeimage,所以我必须使用for循环创建位图。 我想做的是简单地获取一个包含所有红点的纹理,并将其映射到一个正方形中:

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import <stdlib.h>
#import <string.h>
#import <math.h>


GLuint texture;
GLfloat (*pixels) [3];

void init()
{

    // Inizializzazione

    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 1, 1, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    // Inizializzazione della texture

    glGenTextures(1, &texture);

    pixels= malloc(256*256*sizeof(GLfloat[3]));

    for(GLuint i=0; i<256*256;i++)
    {
        pixels[i][0]=1.0;
        pixels[i][1]= pixels[i][2]= 0.0;
    }

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_FLOAT, pixels);

}

void display()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex3f(0, 0, 0);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(10, 0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(10, 10, 0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0, 10, 0);
    glTexCoord2f(0.0, 1.0);
    glEnd();
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);
    init();
    glutMainLoop();
    free(pixels);
    return 0;
}
问题是所有像素都是红色的,但我得到的是白色正方形而不是红色正方形:


在将数据上载到纹理之前,需要先绑定纹理,因此请在glTexImage2D之前使用glBindTexture。此外,由于纹理没有mipmap,并且缩小和放大过滤器的默认值需要它们,因此它将是不完整的。使用glTexParameter将过滤器设置为线性,或以其他方式创建mipmap。

在将数据上载到纹理之前,需要绑定纹理,因此在glTexImage2D之前使用glBindTexture。此外,由于纹理没有mipmap,并且缩小和放大过滤器的默认值需要它们,因此它将是不完整的。使用glTexParameter将过滤器设置为线性,或创建mipmap。

试一试:

#include <GL/glut.h>

GLuint texture;
void init()
{
    // Inizializzazione della texture
    unsigned int i;
    GLfloat* pixels = (GLfloat*)malloc( 256 * 256 * sizeof( GLfloat ) * 3 );
    for( i = 0; i < 256 * 256; i++ )
    {
        GLuint base = i * 3;
        pixels[ base + 0 ] = 1.0f;
        pixels[ base + 1 ] = 0.0f;
        pixels[ base + 2 ] = 0.0f;
    }

    glGenTextures(1, &texture);
    glBindTexture( GL_TEXTURE_2D, texture );

    // disable mipmap filtering since we aren't uploading mipmaps
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_FLOAT, pixels );

    free( pixels);
}

void display( void )
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 1, 1, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    // important because GL_TEXTURE_ENV defaults to GL_MODULATE
    glColor3ub(255,255,255);

    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex3f(0, 0, 0);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(10, 0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(10, 10, 0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0, 10, 0);
    glTexCoord2f(0.0, 1.0);
    glEnd();
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow(argv[0]);

    init();

    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
试一试:

#include <GL/glut.h>

GLuint texture;
void init()
{
    // Inizializzazione della texture
    unsigned int i;
    GLfloat* pixels = (GLfloat*)malloc( 256 * 256 * sizeof( GLfloat ) * 3 );
    for( i = 0; i < 256 * 256; i++ )
    {
        GLuint base = i * 3;
        pixels[ base + 0 ] = 1.0f;
        pixels[ base + 1 ] = 0.0f;
        pixels[ base + 2 ] = 0.0f;
    }

    glGenTextures(1, &texture);
    glBindTexture( GL_TEXTURE_2D, texture );

    // disable mipmap filtering since we aren't uploading mipmaps
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_FLOAT, pixels );

    free( pixels);
}

void display( void )
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 1, 1, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    // important because GL_TEXTURE_ENV defaults to GL_MODULATE
    glColor3ub(255,255,255);

    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex3f(0, 0, 0);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(10, 0, 0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(10, 10, 0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0, 10, 0);
    glTexCoord2f(0.0, 1.0);
    glEnd();
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow(argv[0]);

    init();

    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

您可以在glTexImage2D之后调用freepixel。而您正在泄漏纹理:最好调用glDeleteTextures1,&texture;在GLUTEMAIN循环之后。您可以在GLTEXAGE2D之后调用freepixel。而您正在泄漏纹理:最好调用glDeleteTextures1,&texture;在你的GLUTmain循环之后。我很少需要代码,但这让我明白了怎么做。我很少需要代码,但这让我明白了怎么做。