1

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;
}

問題は、すべてのピクセルが赤であることですが、赤い四角ではなく白い四角が表示されます。

ここに画像の説明を入力

4

2 に答える 2

4

データをアップロードする前にテクスチャをバインドする必要があるため、glTexImage2D の前に glBindTexture を使用します。また、テクスチャにはミップマップがなく、縮小および拡大フィルタのデフォルト値で必要とされるため、現状ではテクスチャは不完全です。glTexParameter を使用してフィルターを線形に設定するか、そうでなければミップマップを作成します。

于 2012-11-29T13:16:09.583 に答える
1

これを試してみてください:

#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;
}
于 2012-11-29T15:31:19.700 に答える