#include <GL/glut.h>
#include "LSystem.h"
//L系统
GLint WindW = 800;
GLint WindH = 600;
GLfloat oldx, oldy;//鼠标移动因子
GLfloat scale = 1.0;//放大缩小
GLfloat xangle = 15.0f;
GLfloat yangle = -15.0f;//旋转因子
GLuint texout; //纹理
GLuint texin;
#define BMP_Header_Length 54 //图像数据在内存块中的偏移量
GLfloat Pi1 = 3.14159;
Tree tree;
LSystem lsrule(tree);
int LoadGLTextures(char *Filename, GLuint *ttexture, int i)//ttexture[i]为用来绑定纹理数据的整数,可更改
{
FILE *File;
BITMAPINFOHEADER header;
BYTE *texture;//texture长宽和像素RGBA texture[width][height][4]
int width, height, m, j;
unsigned char *image;
File = fopen(Filename, "rb");
//读取BMP信息头,跳过14字节文件头
if (File) {
fseek(File, 14, SEEK_SET);
fread(&header, sizeof(BITMAPINFOHEADER), 1, File);
}
else return FALSE;
//读取长宽
width = header.biWidth;
height = header.biHeight;
//为image分配像素空间,读取图片数据,为texture分配width*height*4的四位空间用来生成纹理
image = (unsigned char *)malloc(width*height * 3);
fread(image, sizeof(unsigned char), width*height * 3, File);//唯一的不足之处在于将3字节像素读为一维字符串
texture = (BYTE *)malloc(width*height * 4);// 唯一的不足之处在于将4字节像素读为一维字符串
//以下代码将对texture重排列,一般来说为Blue Green Red Alpha格式(24位图格式),生成纹理使用BGRA模式
/****************************************修改模块,通过像素任意修改图片,示例:*************************************************************/
//本段代码将纯黑色像素点透明度(Alpha)设为100%,其余颜色设为0%
for (m = 0; m<width; m++)
{
for (j = 0; j<height; j++)
{
//把颜色值写入
texture[m*width * 4 + j * 4] = image[m*width * 3 + j * 3];
texture[m*width * 4 + j * 4 + 1] = image[m*width * 3 + j * 3 + 1];
texture[m*width * 4 + j * 4 + 2] = image[m*width * 3 + j * 3 + 2];
//设置alpha值,假设黑色为透明色
if (texture[m*width * 4 + j * 4] >= 200 && texture[m*width * 4 + j * 4 + 1] >= 200 && texture[m*width * 4 + j * 4 + 2] >= 200) {
texture[m*width * 4 + j * 4 + 3] = 0; //透明,alpha=0
}
else
texture[m*width * 4 + j * 4 + 3] = 255; //不透明,alpha=255
}
}
//ttexture[i]为绑定纹理的整数
//下面生成纹理以及纹理处理
glGenTextures(1, &ttexture[i]);
glBindTexture(GL_TEXTURE_2D, ttexture[i]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glAlphaFunc(GL_GREATER, 0.5);//使Alpha值生效
//纹理生成完毕,下面释放空间
free(texture);
free(image); // 释放图像数据
return TRUE; // 返回 Status
}
int power_of_two(int n)
{
if (n <= 0)
return 0;
return (n & (n - 1)) == 0;
}
//读取纹理
GLuint load_texture(const char* file_name)
{
GLint width, height, total_bytes;
GLubyte* pixels = 0;
GLuint last_texture_ID = 0, texture_ID = 0;
// 打开文件,如果失败,返回
FILE* pFile = fopen(file_name, "rb");
if (pFile == 0)
return 0;
// 读取文件中图象的宽度和高度
fseek(pFile, 0x0012, SEEK_SET);
fread(&width, 4, 1, pFile);
fread(&height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
// 计算每行像素所占字节数,并根据此数据计算总像素字节数
{
GLint line_bytes = width * 3;
while (line_bytes % 4 != 0)
++line_bytes;
total_bytes = line_bytes * height;
}
// 根据总像素字节数分配内存
pixels = (GLubyte*)malloc(total_bytes);
if (pixels == 0)
{
fclose(pFile);
return 0;
}
// 读取像素数据
if (fread(pixels, total_bytes, 1, pFile) <= 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 对就旧版本的兼容,如果图象的宽度和高度不是的整数次方,则需要进行缩放
// 若图像宽高超过了OpenGL规定的最大值,也缩放
{
GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (!power_of_two(width)
|| !power_of_two(height)
|| width > max
|| height > max)
{
const GLint new_width = 256;
const GLint new_height = 256; // 规定缩放后新的大小为边长的正方形
GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0;
// 计算每行需要的字节数和总字节数
new_line_bytes = new_width * 3;
while (new_line_bytes % 4 != 0)
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height;
// 分配内存
new_pixels = (GLubyte*)malloc(new_total_bytes);
if (new_pixels == 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 进行像素缩放
gluScaleImage(GL_RGB,
width, height, GL_UNSIGNED_BYTE, pixels,
new_width, new_height, GL_UNSIGNED_BYTE, new_pixels);
// 释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height
free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height;
}
}
// 分配一个新的纹理编号
glGenTextures(1, &texture_ID);
if (texture_ID == 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 绑定新的纹理,载入纹理并设置纹理参数
// 在绑定前,先获得原来绑定的纹理编号,以便在最后进行恢复
GLint lastTextureID = last_texture_ID;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &lastTextureID);
glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
glBindTexture(GL_TEXTURE_2D, lastTextureID); //恢复之前的纹理绑定
free(pixels);
return texture_ID;
}
void init()
{
//glClearColor(0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-25.0, 25.0, -25.0, 25.0, -25.0, 25.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); // 启用纹理
//使alpha值生效
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
//
lsrule.initGrammar();//将文法迭代好
lsrule.generateFractal();//把树的树枝等先算好
//cout << lsrule.grammar.getRule() << endl;//成功迭代好
}
//以yuandian两端点,r为半径
void drawcone(double r,double h) {
glBindTexture(GL_TEXTURE_2D, texin);
for (int i = 0; i <360; i += 50) { //在原点画了一个圆柱
float temp = i * Pi1 / 180;
float temp1 = (i + 50)*Pi1 / 180;
glBegin(GL_QUADS);
glTexCoord2f((temp*r*tree.trunk.radius_shrink)/(2 * Pi1), 0.0f); glVertex3f(r*cos(temp)*tree.trunk.radius_shrink, 0, r*sin(temp)*tree.trunk.radius_shrink);
glTexCoord2f((temp*r)/(2*Pi1), 1.0f); glVertex3f(r*cos(temp), h, r*sin(temp)); //竖着2倍 横着1/2
glTexCoord2f((temp1*r)/(2 * Pi1), 1.0f); glVertex3f(r*cos(temp1), h, r*sin(temp1));
glTexCoord2f((temp1*r*tree.trunk.radius_shrink) / (2 * Pi1), 0.0f); glVertex3f(r*cos(temp1)*tree.trunk.radius_shrink, 0.0f, r*sin(temp1)*tree.trunk.radius_shrink);
glEnd();
}
}
void DrawChannel(Node A, Node B, double r)
{
// 起始线段:以(0,1,0)为起点,它的长度(distance)通过目标线段计算,
// 终点坐标为(0,1-distance,0)
// 目标线段:以(x1,y1,z1)为起点,以(x2,y2,z2)为终点
// 计算目标向量
GLfloat dx = B.x - A.x;
GLfloat dy = B.y - A.y;
GLfloat dz = B.z - A.z;
// 算出目标向量模(即AB长度)
GLfloat distance = sqrt(dx*dx + dy * dy + dz * dz);
// 计算平移量
GLfloat px = A.x;
GLfloa
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
画树.zip (91个子文件)
画树
resource.h 383B
tree.cpp 23B
画树.vcxproj 10KB
Debug
画树.Build.CppClean.log 2KB
vc141.pdb 572KB
glew32.dll 330KB
visualinfo.exe 218KB
画树.log 4KB
画树.exe 83KB
tree.obj 3KB
transformation.obj 34KB
vc141.idb 803KB
grammar.obj 120KB
main.obj 164KB
glfw3.dll 43KB
freeglut.dll 214KB
glewinfo.exe 343KB
LSystem.obj 251KB
画树.tlog
rc.command.1.tlog 626B
CL.read.1.tlog 76KB
link.read.1.tlog 6KB
rc.read.1.tlog 3KB
link.write.1.tlog 1KB
CL.write.1.tlog 3KB
link.command.1.tlog 3KB
画树.lastbuildstate 252B
CL.command.1.tlog 5KB
rc.write.1.tlog 262B
画树.res 32B
grammar.cpp 795B
LSystem.h 994B
packages.config 225B
画树.rc 2KB
画树
tree.cpp 23B
画树.vcxproj 10KB
Debug
画树.Build.CppClean.log 2KB
vc141.pdb 548KB
glew32.dll 330KB
visualinfo.exe 218KB
画树.log 2KB
画树.exe 83KB
tree.obj 3KB
transformation.obj 34KB
vc141.idb 811KB
grammar.obj 120KB
main.obj 111KB
glfw3.dll 43KB
freeglut.dll 214KB
glewinfo.exe 343KB
LSystem.obj 187KB
stdafx.obj 6KB
画树.pch 2.75MB
画树.tlog
CL.read.1.tlog 81KB
link.read.1.tlog 6KB
link.write.1.tlog 1KB
CL.write.1.tlog 5KB
link.command.1.tlog 3KB
画树.lastbuildstate 252B
CL.command.1.tlog 6KB
grammar.cpp 795B
LSystem.h 812B
packages.config 225B
Maple1.bmp 48KB
stdafx.h 366B
grammar.h 515B
main.cpp 3KB
stdafx.cpp 306B
transformation.cpp 2KB
LSystem.cpp 3KB
transformation.h 534B
tree.h 2KB
targetver.h 370B
画树.vcxproj.filters 2KB
stdafx.h 366B
画树.aps 1KB
grammar.h 507B
res
tree.bmp 2.65MB
Maple1.bmp 48KB
TREE1.bmp 2.75MB
银杏叶.bmp 192KB
tempLeaf.bmp 119KB
bark2.bmp 406KB
bark1.bmp 24KB
星状叶.bmp 192KB
main.cpp 15KB
transformation.cpp 2KB
LSystem.cpp 3KB
transformation.h 534B
tree.h 2KB
targetver.h 370B
画树.vcxproj.filters 3KB
共 91 条
- 1
资源评论
- qq_352016022018-08-05还可以吧……
MeatBall00
- 粉丝: 52
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功