简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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