opengl纹理 [英] opengl texturing

查看:135
本文介绍了opengl纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

#include windows.h
#include gl\gl.h
#include gl\glut.h
#include stdlib.h
#include iostream>


#define ROAD 0


struct Image
{
unsigned long size_x;
unsigned long size_y;
char * data;
};

typedef struct Image Image;

const int textureCount = 1;

图片myTextureData [textureCount];
GLuint theTexture [textureCount];


char * textureFilenames [textureCount] = {road.bmp};



void init(void);
void display(void);
void keyboard(unsigned char,int,int);
void resize(int,int);
void drawcube(float,float,float,float,float,float,int);



int is_depth;




int main(int argc,char ** argv)
{
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(40,40);
glutCreateWindow(3D World);
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(resize);

glutMainLoop();
return 0;
}

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0)
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);

is_depth = 1;
}

void display(void)
{

if(is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(-75.0,0.0,-400.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-75.0,0.0,100.0);
glTexCoord2f(1.0,1.0);
glVertex3f(75.0,0.0,100.0);
glTexCoord2f(1.0,1.0);
glVertex3f(75.0,0.0,-400.0);

drawcube(-70,15,72,8,15,28,0);
drawcube(-70,10,10,8,10,28,0);
drawcube(-70,15,-45,8,15,18,0);
drawcube(-70,15,-85,8,15,18,0);
drawcube(-70,35,-125,8,35,12,0);
drawcube(-70,9,-170,8,9,28,0);
drawcube(-70,15,-220,8,15,18,0);
drawcube(-70,15,-265,8,15,28,0);
drawcube(-70,15,-330,8,15,28,0);
drawcube(67,15,72,8,15,28,0);
drawcube(67,10,10,8,10,28,0);
drawcube(67,15,-45,8,15,18,0);
drawcube(67,15,-85,8,15,18,0);
drawcube(67,35,-125,8,35,12,0);
drawcube(67,9,-170,8,9,28,0);
drawcube(67,15,-220,8,15,18,0);
drawcube(67,15,-265,8,15,28,0);
drawcube(67,15,-330,8,15,28,0);
drawingcube(-33,18,-364,25,18,10,0);
drawcube(25,28,-364,30,28,10,0);
drawcube(25,28,90,30,28,10,0);
drawcube(-33,18,90,25,18,10,0);
drawcube(0,60,-125,18,60,22,0);
drawcube(0,25,-225,8,25,28,0);
drawcube(0,25,0,8,25,28,0);
drawcube(-58,1,-135,4,0.5,215,0);
drawcube(58,1,-135,4,0.5,215,0);
drawcube(0,1,-345,60,0.5,4,0);
drawcube(0,1,75,60,0.5,4,0);



glEnd();


glutSwapBuffers();
}

void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case' a':
glTranslatef(5.0,0.0,0.0);
break;
case'd':
glTranslatef(-5.0,0.0,0.0);
break;
case'w':
glTranslatef(0.0,0.0,5.0);
break;
case's':
glTranslatef(0.0,0.0,-5.0);
break;
}
display();
}

void resize(int width,int height)
{
if(height == 0)height = 1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0,width / height,1.0,400.0);
glTranslatef(0.0,-5.0,-150.0);
glMatrixMode(GL_MODELVIEW);
}

void drawcube(float xc,float yc,float zc,float x_offset,float y_offset,float z_offset,int color)
{

switch(color)
{
case 1:
glColor3f(1.0,0.0,0.0);
break;
case 2:
glColor3f(0.0,1.0,0.0);
break;
case 3:
glColor3f(0.0,0.0,1.0);
break;
}
glBegin(GL_QUADS);
glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc-z_offset);
glVertex3f(xc + x_offset,yc + y_offset,zc-z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);

glVertex3f(xc + x_offset,yc + y_offset,zc-z_offset);
glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc-z_offset);

glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);

glVertex3f(xc + x_offset,yc + y_offset,zc-z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);

glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc-z_offset);
glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);

glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);
glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);
glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);


glEnd();
}

int imageLoader(const char * filename,Image * image)
{
FILE * file;

unsigned long size;
unsigned long i;
无符号短整型平面;
unsigned short int bpp;

char temp;
char finalName [80];

glTexCoord2f(1.0,0.0);

strcpy(finalName,);
strcat(finalName,filename);

if((file = fopen(finalName,rb))== NULL)
{
printf(File Not Found:%s\\\
,finalName );
return 0;
}

fseek(file,18,SEEK_CUR);

glTexCoord2f(1.0,0.0);

if((i = fread(& image-> size_x,4,1,file))!= 1)
{
printf %s.\\\
,finalName);
return 0;
}

if((i = fread(& image-> size_y,4,1,file))!= 1)
{
printf 从%s.\\\
读取高度错误,finalName);
return 0;
}

size = image-> size_x * image-> size_y * 3;

if((fread(& planes,2,1,file))!= 1)
{
printf(Error from planes from%s.\\\
,finalName);
return 0;
}

if(planes!= 1)
{
printf(%s不是1:%u\\\
,finalName,planes );
return 0;
}

if((i = fread(& bpp,2,1,file))!= 1)
{
printf from%s.\\\
,finalName);
return 0;
}

if(bpp!= 24)
{
printf(Bpp from%s is not 24:%u\\\
,finalName,bpp );
return 0;
}

fseek(file,24,SEEK_CUR);

image-> data =(char *)malloc(size);

if(image-> data == NULL)
{
printf(为颜色校正的图像数据分配内存错误);
return 0;
}

if((i = fread(image-> data,size,1,file))!= 1)
{
printf从%s.\\\
读取图像数据,finalName);
return 0;
}

for(i = 0; i {
temp = image-> data [i]
image-> data [i] = image-> data [i + 2];
image-> data [i + 2] = temp;
}
return 1;
}

void textureLoader()
{

glPixelStorei(GL_UNPACK_ALIGNMENT,1);

for(int k = 0; k {
if(!imageLoader(textureFilenames [k],& myTextureData [k]))
exit(1);


glGenTextures(1,& theTexture [k]);

glBindTexture(GL_TEXTURE_2D,theTexture [k]);


glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D,3,myTextureData [k] .size_x,myTextureData [k] .size_y,GL_RGB,GL_UNSIGNED_BYTE,myTextureData [k] .data);
}
}


解决方案

从不调用 textureLoader()



glBindTexture()



除了 textureLoader() glBegin() s。


hi im trying to texture the floor of my level but when i call it the program compiles ok but the texture doesnt appear any help would be great

#include windows.h
#include gl\gl.h
#include gl\glut.h
#include stdlib.h
#include iostream>


#define ROAD 0


struct Image 
{
    unsigned long size_x;
    unsigned long size_y;
    char *data;
};

typedef struct Image Image;

const int textureCount = 1;

Image myTextureData[textureCount];
GLuint theTexture[textureCount];


char* textureFilenames[textureCount] = {"road.bmp"};



void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);
void drawcube(float, float, float, float, float, float, int);



int is_depth;




int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(40, 40);
    glutCreateWindow("3D World");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutReshapeFunc(resize);  

    glutMainLoop();
    return 0;
}

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);

    is_depth = 1;
}

void display(void)
{

    if (is_depth)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    else
        glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
        glTexCoord2f(0.0,0.0);
        glVertex3f(-75.0, 0.0, -400.0);
        glTexCoord2f(0.0,1.0);
        glVertex3f(-75.0, 0.0, 100.0);
        glTexCoord2f(1.0,1.0);
        glVertex3f(75.0, 0.0, 100.0);
        glTexCoord2f(1.0,1.0);
        glVertex3f(75.0, 0.0, -400.0);

        drawcube(-70,15,72,8,15,28,0);
        drawcube(-70,10,10,8,10,28,0);
        drawcube(-70,15,-45,8,15,18,0);
        drawcube(-70,15,-85,8,15,18,0);
        drawcube(-70,35,-125,8,35,12,0);
        drawcube(-70,9,-170,8,9,28,0);
        drawcube(-70,15,-220,8,15,18,0);
        drawcube(-70,15,-265,8,15,28,0);
        drawcube(-70,15,-330,8,15,28,0);
        drawcube(67,15,72,8,15,28,0);
        drawcube(67,10,10,8,10,28,0);
        drawcube(67,15,-45,8,15,18,0);
        drawcube(67,15,-85,8,15,18,0);
        drawcube(67,35,-125,8,35,12,0);
        drawcube(67,9,-170,8,9,28,0);
        drawcube(67,15,-220,8,15,18,0);
        drawcube(67,15,-265,8,15,28,0);
        drawcube(67,15,-330,8,15,28,0);
        drawcube(-33,18,-364,25,18,10,0);
        drawcube(25,28,-364,30,28,10,0);
        drawcube(25,28,90,30,28,10,0);
        drawcube(-33,18,90,25,18,10,0);
        drawcube(0,60,-125,18,60,22,0);
        drawcube(0,25,-225,8,25,28,0);
        drawcube(0,25,0,8,25,28,0);
        drawcube(-58,1,-135,4,0.5,215,0);
        drawcube(58,1,-135,4,0.5,215,0);
        drawcube(0,1,-345,60,0.5,4,0);
        drawcube(0,1,75,60,0.5,4,0);        



    glEnd();


    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'a':
        glTranslatef(5.0, 0.0, 0.0);
        break;
    case 'd':
        glTranslatef(-5.0, 0.0, 0.0);
        break;
    case 'w':
        glTranslatef(0.0, 0.0, 5.0);
        break;
    case 's':
        glTranslatef(0.0, 0.0, -5.0);
        break;
    }
    display();
}

void resize(int width, int height)
{
    if (height == 0) height = 1;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0, width / height, 1.0, 400.0);
    glTranslatef(0.0, -5.0, -150.0);
    glMatrixMode(GL_MODELVIEW);
}

void drawcube(float xc, float yc, float zc, float x_offset, float y_offset, float z_offset, int color)
{

    switch(color)
    {
    case 1:
        glColor3f(1.0,0.0,0.0);
        break;
    case 2:
        glColor3f(0.0,1.0,0.0);
        break;
    case 3:
        glColor3f(0.0,0.0,1.0);
        break;
    }
    glBegin(GL_QUADS);
        glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc - z_offset);
        glVertex3f(xc + x_offset,yc + y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);

        glVertex3f(xc + x_offset,yc + y_offset,zc - z_offset);
        glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc - z_offset);

        glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
        glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);

        glVertex3f(xc + x_offset,yc + y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);

        glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc - z_offset);
        glVertex3f(xc - x_offset,yc - y_offset,zc - z_offset);

        glVertex3f(xc + x_offset,yc + y_offset,zc + z_offset);
        glVertex3f(xc - x_offset,yc + y_offset,zc + z_offset);
        glVertex3f(xc - x_offset,yc - y_offset,zc + z_offset);
        glVertex3f(xc + x_offset,yc - y_offset,zc + z_offset);


    glEnd();
}

int imageLoader(const char *filename, Image *image) 
{
    FILE *file;

    unsigned long size;
    unsigned long i;
    unsigned short int planes;
    unsigned short int bpp;

    char temp;
    char finalName[80];

    glTexCoord2f(1.0, 0.0);

    strcpy(finalName, "" );
    strcat(finalName, filename);

    if ((file = fopen(finalName, "rb"))==NULL) 
    {
        printf("File Not Found : %s\n",finalName);
        return 0;
    }

    fseek(file, 18, SEEK_CUR);

    glTexCoord2f(1.0, 0.0);

    if ((i = fread(&image->size_x, 4, 1, file)) != 1) 
    {
        printf("Error reading width from %s.\n", finalName);
        return 0;
    }

    if ((i = fread(&image->size_y, 4, 1, file)) != 1) 
    {
        printf("Error reading height from %s.\n", finalName);
        return 0;
    }

    size = image->size_x * image->size_y * 3;

    if ((fread(&planes, 2, 1, file)) != 1) 
    {
        printf("Error reading planes from %s.\n", finalName);
        return 0;
    }

    if (planes != 1) 
    {
        printf("Planes from %s is not 1: %u\n", finalName, planes);
        return 0;
    }

    if ((i = fread(&bpp, 2, 1, file)) != 1) 
    {
        printf("Error reading bpp from %s.\n", finalName);
        return 0;
    }

    if (bpp != 24) 
    {
        printf("Bpp from %s is not 24: %u\n", finalName, bpp);
        return 0;
    }

    fseek(file, 24, SEEK_CUR);

    image->data = (char *) malloc(size);

    if (image->data == NULL) 
    {
        printf("Error allocating memory for color-corrected image data");
        return 0;
    }

    if ((i = fread(image->data, size, 1, file)) != 1) 
    {
        printf("Error reading image data from %s.\n", finalName);
        return 0;
    }

    for (i=0;i<size;i+=3) 
    {
        temp = image->data[i];
        image->data[i] = image->data[i+2];
        image->data[i+2] = temp;
    }
    return 1;
}

void textureLoader() 
{

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    for(int k=0; k < textureCount; k++) 
    {
        if(!imageLoader(textureFilenames[k], &myTextureData[k])) 
            exit(1);


        glGenTextures(1, &theTexture[k]);

        glBindTexture(GL_TEXTURE_2D, theTexture[k]);


        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, myTextureData[k].size_x, myTextureData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, myTextureData[k].data);
    }
}

解决方案

You never call textureLoader().

Or glBindTexture(), other than in textureLoader(), which you don't call.

Also, don't nest glBegin()s.

这篇关于opengl纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆