Vulkan

纹理

最近用到纹理,以下是一个DEMO

#pragma once
#include <windows.h>
#include<fstream>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdlib.h>

#include <gl/glu.h>
#include <gl/glut.h>
using namespace std;

static int win;
GLuint pTexture;
int imgWid=64,imgHei=64;
static GLubyte texSourIma[64][64][4];

void init(void) 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor (1.0, 1.0, 1.0, 0.0);
	glShadeModel (GL_SMOOTH);


}

void initTexture()
{
	for (int i =0;i<64;i++)
	{
		for (int j=0;j<64;j++)
		{
			texSourIma[i][j][0]=(GLubyte)(i%2)*255;
			texSourIma[i][j][1]=(GLubyte)(i%2)*255;
			texSourIma[i][j][2]=(GLubyte)(i%2)*255;
			texSourIma[i][j][3]=(GLubyte)255;
		}
	}
	glGenTextures(1,&pTexture);
	glBindTexture(GL_TEXTURE_2D,pTexture);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imgWid,imgHei,0,GL_RGBA,GL_UNSIGNED_BYTE,texSourIma);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);//由于是2D,这里只有S,T方向,若再来个R方向,会提示找不到符号
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

}
void display(void)
{
	glClear (GL_COLOR_BUFFER_BIT);
	glPushMatrix();

	gluLookAt (25.0,25.0,25.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0);

	initTexture();//初始化纹理源 和 纹理设置
	glColor3f(1.0,0.0,0.0);
	glScaled(4,4,4);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,pTexture);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0);glVertex3d(0,0,0);//这里注意顶点环绕方向
	glTexCoord2f(0,1);glVertex3d(0,3,0);
	glTexCoord2f(1,1);glVertex3d(3,3,0);
	glTexCoord2f(1,0);glVertex3d(3,0,0);
	glEnd();
	glDisable(GL_TEXTURE_2D);

	glPopMatrix();
	glFlush();
}

void reshape (int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 0.1, 900.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}



int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH);
	glutInitWindowSize (500, 500); 
	glutInitWindowPosition (100, 100);
	win=glutCreateWindow (argv[0]);

	init ();

	glutDisplayFunc(display); 
	glutReshapeFunc(reshape);

	glutMainLoop();
	return 0;
}


posted on 2013-03-30 14:58  Vulkan  阅读(233)  评论(0编辑  收藏  举报

导航