OpenGL纹理贴图坐标

时间:2012-01-23 22:27:20

标签: opengl textures

我需要能够拉伸我在整个2d或3d(脸部)形状上导入的图像纹理。它只会在形状的右上角渲染,并且重复 - 如果我启用GL_REPEAT,或者如果我启用GL_CLAMP,图像将从突出到边缘的边伸展。

这是我的代码:

#include "stdafx.h"
#include "glut.h"
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>



GLuint texture;
float xRotation = 0.0f;




void drawScene1 (void) {
    //glRotatef(xRotation,0.0f,1.0f,0.0f);
    glBindTexture(GL_TEXTURE_2D, texture);
    //glutSolidCube(1.0f);

    glBegin(GL_POLYGON);
        glTexCoord2d(0,1);
        glVertex2d(-1.5,-1.5);

        glTexCoord2d(1,1);
        glVertex2d(1.0,-2.0);

        glTexCoord2d(1,0);
        glVertex2d(+1.5,+1.5);

        glTexCoord2d(0,0);
        glVertex2d(-1.5,+1.5);
    glEnd();
}




void FreeTexture(GLuint texture) {
  glDeleteTextures(1, &texture);
}



GLuint LoadTexture(const char * filename, int width, int height) {

    GLuint texture;
    unsigned char * data;
    FILE * file;

    file = fopen(filename, "rb");
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc(width * height * 3);

    fread(data, width * height * 3, 1, file); 
    fclose(file);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);


    free(data);
    return texture;
}




void init (void) {
    glShadeModel(GL_SMOOTH);  
    glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //gluOrtho2D(0,500,0,500);
    //glClearDepth(1);
    //glEnable (GL_DEPTH_TEST);
    //glEnable (GL_LIGHTING);
    //glEnable (GL_LIGHT0);
    //glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_NORMALIZE);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);

}




void display (void) {
    glClearColor(0.05,0.05,0.1,1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    texture = LoadTexture("img.raw", 256, 256);

    drawScene1();

    FreeTexture(texture);
    glutSwapBuffers();
    xRotation++;
}



int main (int argc, char **argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Virgin");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0;
}

2 个答案:

答案 0 :(得分:5)

请决定:隐式纹理坐标生成:

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

或显式纹理坐标恳求:

glTexCoord(...)

隐式将覆盖显式。


void display (void) {
/*... */
    texture = LoadTexture("img.raw", 256, 256);

    drawScene1();

    FreeTexture(texture);
/* ... */
}

不要为每个渲染帧加载和删除纹理。在启动时加载纹理,仅在渲染时绑定它们。

答案 1 :(得分:3)

删除以下行:

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

您已经通过glTexCoord2f()输入了纹理坐标,无需生成它们。