简体   繁体   English

OpenGL纹理映射坐标

[英]OpenGL texture mapping coordinates

I need to be able to stretch the image texture i'm importing over the entire 2d or 3d (face) shape. 我需要能够在整个2d或3d(面部)形状上拉伸要导入的图像纹理。 It will only render in the top right of the shape, and either repeat - if I enable GL_REPEAT, or the image will stretch from the sides projecting to the edge horribly if i enable GL_CLAMP. 它只会在形状的右上角渲染,然后重复-如果启用GL_REPEAT,或者如果启用GL_CLAMP,则图像将从投影到边缘的那边严重拉伸。

Here's my code: 这是我的代码:

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

Please decide: Implicit texture coordinate generation: 请决定:隐式纹理坐标生成:

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

or explicit texture coordinate supplications: 或明确的纹理坐标请求:

glTexCoord(...)

Implicit will override explicit. 隐式将覆盖显式。


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

    drawScene1();

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

Don't load and delete the texture for each rendered frame. 不要为每个渲染的框架加载和删除纹理。 Load the textures at startup and only bind them when rendering. 在启动时加载纹理,仅在渲染时绑定它们。

Remove the following lines: 删除以下行:

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

You already have texture coordinates entered via glTexCoord2f(), no need to generate them. 您已经通过glTexCoord2f()输入了纹理坐标,无需生成它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM