OpenGL 练习12 texture

嗯嗯

一幅黑白素描加上颜色之后···

#include<gl/glut.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>

//窗口大小
int width = 600;
int height = 600;

//typedef unsigned char BYTE;
typedef void BYTE;
typedef GLfloat point4[4];
typedef GLfloat color4[4];

point4 vertices[8] = {{-0.5, -0.5, 0.5, 1.0}, {-0.5, 0.5, 0.5, 1.0}, {0.5, 0.5, 0.5, 1.0}, {0.5, -0.5, 0.5, 1.0},
					  {-0.5, -0.5, -0.5, 1.0},  {-0.5, 0.5, -0.5, 1.0},  {0.5, 0.5, -0.5, 1.0},  {0.5, -0.5, -0.5, 1.0}};

color4 colors[8] = {{1.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0},
					{0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0}, {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}};

color4 quad_color[36];
point4 points[36];


GLfloat th, x, y, z;
#define M_PI 3.14159;

GLuint texture;

#define N 36;

GLfloat tex_coord[36][2];

void quad(int a, int b, int c, int d)
{
	static int i = 0;
	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[a][j]; 
		points[i][j] = vertices[a][j];
	}
	tex_coord[i][0] = 0.0;
	tex_coord[i][1] = 0.0;
	glTexCoord2d(0.0, 0.0);
	glVertex4fv(points[i]);
	i++;

	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[b][j]; 
		points[i][j] = vertices[b][j];
	}
	tex_coord[i][0] = 0.0;
	tex_coord[i][1] = 1.0;
	glTexCoord2d(0.0, 1.0);
	glVertex4fv(points[i]);
	i++;
	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[c][j]; 
		points[i][j] = vertices[c][j];
	}
	tex_coord[i][0] = 1.0;
	tex_coord[i][1] = 1.0;
	glTexCoord2d(1.0, 1.0);
	glVertex4fv(points[i]);
	i++;
	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[a][j]; 
		points[i][j] = vertices[a][j];
	}
	tex_coord[i][0] = 0.0;
	tex_coord[i][1] = 0.0;
	glTexCoord2d(0.0, 0.0);
	glVertex4fv(points[i]);
	i++;
	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[c][j]; 
		points[i][j] = vertices[c][j];
	}
	tex_coord[i][0] = 1.0;
	tex_coord[i][1] = 1.0;
	glTexCoord2d(1.0, 1.0);
	glVertex4fv(points[i]);
	i++;
	for (int j=0; j<4; j++) {
		quad_color[i][j] = colors[d][j]; 
		points[i][j] = vertices[d][j];
	}
	tex_coord[i][0] = 1.0;
	tex_coord[i][1] = 0.0;
	glTexCoord2d(1.0, 0.0);
	glVertex4fv(points[i]);
	i++;
}

void colorcube()
{
	quad(0, 3, 2, 1);
	quad(2, 3, 7, 6);
	quad(3, 0, 4, 7);
	quad(1, 2, 6, 5);
	quad(4, 5, 6, 7);
	quad(5, 4, 0, 1);
}

// load a 256x256 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap )
{
    GLuint texture;
    int width, height;
    BYTE * data;
    FILE * file;

    // open texture data
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    // allocate buffer
    width = 256;
    height = 256;
    data = malloc( width * height * 3 );

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

    // allocate a texture name
    glGenTextures( 1, &texture );

    // select our current texture
    glBindTexture( GL_TEXTURE_2D, texture );

    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    // if wrap is true, the texture wraps over at the edges (repeat)
    //       ... false, the texture ends at the edges (clamp)
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                     wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                     wrap ? GL_REPEAT : GL_CLAMP );

    // build our texture mipmaps
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                       GL_RGB, GL_UNSIGNED_BYTE, data );

    // free buffer
    free( data );

    return texture;
}

void display(void)
{	
	texture = LoadTextureRAW("texture.bmp", 1);
	
	glEnable(GL_TEXTURE_2D);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	glEnableClientState(GL_VERTEX_ARRAY);
	colorcube();
	glVertexPointer(4, GL_FLOAT, 0, points);
	glEnableClientState(GL_COLOR_ARRAY);//启用颜色数组
	glColorPointer(4, GL_FLOAT, 0, quad_color);
	glDrawArrays(GL_TRIANGLES, 0, 36);

	glFlush();
}

void init()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glViewport(0,0,(GLsizei)width,(GLsizei)height);

	glMatrixMode(GL_PROJECTION);//投影矩阵
		glLoadIdentity();
		gluPerspective(45, width/height, 1, 1000);

	glMatrixMode(GL_MODELVIEW);//模型矩阵
		glLoadIdentity();
		gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);

		glRotatef(35.26, 1, 0, 0);//绕x轴旋转
		glRotatef(30, 0, 1, 0);
		glRotatef(90, 0, 0, 1);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);//初始化GLUT
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(100, 100);//设置窗口显示位置
	glutInitWindowSize(600,600);//设置窗口大小
	glutCreateWindow("Texture object");//创建带标题的窗口
	init();
	glutDisplayFunc(display);
	glutMainLoop();//进入GLUT事件处理循环
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值