⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chgl.cpp

📁 此文档是用vistual studio 2005 开发的用来描述3D-tree 的生长过程
💻 CPP
字号:
#include "stdafx.h"
#include <cmath>
#include <iostream>
#include <GL/glut.h>
#include "ChGL.h"

ChGL::ChGL() {
}

ChGL::~ChGL() {
}

void ChGL::drawCylinder( float baseRadius, float topRadius, float length, int slices ) {
	float base_x = 0.0f, base_y = 0.0f, base_z = 0.0f;
	float top_x = 0.0f, top_y = length, top_z = 0.0f;
	float angle = 0.0f;
	int i = 0;

	//glEnable( GL_CULL_FACE );
	glBegin( GL_QUAD_STRIP );
	for ( i = 0; i <= slices; i++ ) {
		base_x = baseRadius * cos( angle );
		base_z = baseRadius * sin( angle );
		top_x = topRadius * cos( angle );
		top_z = topRadius * sin( angle );

		glNormal3f( base_x, base_y, base_z );

		glTexCoord2f( i * 1.0 / slices, 0.0f );
		glVertex3f( base_x, base_y, base_z );

		glTexCoord2f( i * 1.0 / slices, 1.0f );
		glVertex3f( top_x, top_y, top_z );

		angle = angle + ( 360.0f / slices ) * PI / 180.0f;
	}
	glEnd();
	//glDisable( GL_CULL_FACE );
	glTranslatef( 0.0f, length, 0.0f );
}

bool ChGL::loadImage( char* filename, ChImage* image ) {
	FILE* file;
	unsigned long size; /* size of the image in bytes. */
	unsigned long i; /* standard counter. */
	unsigned short int planes; /* number of planes in image (must be 1) */
	unsigned short int bpp; /* number of bits per pixel (must be 24) */
	unsigned char temp; /* used to convert bgr to rgb color. */

	/* Check if the file exists */
	if ( ( file = fopen( filename, "rb" ) ) == NULL ) {
		printf( "File Not Found : %s\n", filename );
		return 0;
	}

	/* Skip to bmp header */
	fseek( file, 18, SEEK_CUR );

	/* read width */
	if ( ( i = fread( &image->sizeX, 4, 1, file ) ) != 1 ) {
		printf( "Error reading width from %s.\n", filename );
		return false;
	}

	/* read the height */
	if ( ( i = fread( &image->sizeY, 4, 1, file ) ) != 1 ) {
		printf( "Error reading height from %s.\n", filename );
		return false;
	}

	/* calculate the size */
	size = image->sizeX * image->sizeY * 3 * sizeof( unsigned char );

	/* read the planes */
	if ( ( fread( &planes, 2, 1, file ) ) != 1 ) {
		printf( "Error reading planes from %s. \n", filename );
		return false;
	}
	if ( planes != 1 ) {
		printf( "Planes from %s is not 1: %u\n", filename, planes );
		return false;
	}

	/* read the bpp */
	if ( ( i = fread( &bpp, 2, 1, file ) ) != 1 ) {
		printf( "Error reading bpp from %s. \n", filename );
		return false;
	}
	if ( bpp != 24 ) {
		printf( "Bpp from %s is not 24: %u\n", filename, bpp );
		return false;
	}

	/* Seek past the rest of the bitmap header */
	fseek( file, 24, SEEK_CUR );

	/* Read the data */
	image->data = new unsigned char[size]; //( unsigned char* )malloc( size );
	if ( image->data == NULL ) {
		printf( "Error allocating memory for colour-corrected image data" );
		return false;
	}
	if ( ( i = fread( image->data, size, 1, file ) ) != 1 ) {
		printf( "Error reading image data from %s.\n", filename );
		return false;
	}

	/* reverse all of the colours BGR to RGB */
	for ( i = 0; i < size; i += 3 ) {
		temp = image->data[ i ];
		image->data[ i ] = image->data[ i + 2 ];
		image->data[ i + 2 ] = temp;
	}

	fclose( file );
	return true;
}

bool ChGL::delBlackColor( ChImage* image ) {
	int i;
	unsigned char* convertedImage = new unsigned char[4 * image->sizeX * image->sizeY];//* )malloc( 4 * image->sizeX * image->sizeY * sizeof( unsigned char ) );

	/* Create memory for RGBA8 texture */
	for ( i = 0; i < image->sizeX * image->sizeY; i++ )
	{
		/* Pick black value as alpha! */
		if ( image->data[ i * 3 ] < 0.5 && image->data[ i * 3 + 1 ] < 0.5 && image->data[ i * 3 + 2 ] < 0.5 ) {
			convertedImage[ i * 4 + 3 ] = 0;
		} else {
			convertedImage[ i * 4 + 3 ] = 0xFF;
		}

		/* Red channel */
		convertedImage[ i * 4 ] = image->data[ i * 3 ];
		/* Green channel */
		convertedImage[ i * 4 + 1 ] = image->data[ i * 3 + 1 ];
		/* Blue channel */
		convertedImage[ i * 4 + 2 ] = image->data[ i * 3 + 2 ];
	}
	if ( image->data != NULL ) {
		delete image->data ;
	}
	image->data = convertedImage;

	return true;
}

/* Load texture into memory */
void ChGL::loadTexture( char* imageName, unsigned int* tex, bool delBlack ) {
	ChImage* image;
	image = new ChImage();//( ChImage* )malloc( sizeof( ChImage ) );
	if ( image == NULL ) {
		printf( "Error allocating space for image!" );
		//exit( 0 );
	}
	if ( !loadImage( imageName, image ) ) {
		//exit( 1 );
	}

	glGenTextures( 1, tex );

	glBindTexture( GL_TEXTURE_2D, *tex ); /* 2d texture (x and y size) */
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); /* scale linearly when image bigger than texture */
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); /* scale linearly when image smalled than texture */
	
	if ( delBlack == false ) {
		/* 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, 
		border 0 (normal), rgb color data, unsigned byte data, and finally the data itself. */
		glTexImage2D( GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data );
		/* 2d texture, 3 colors, width, height, RGB in that order, byte data, and the data. */
		gluBuild2DMipmaps( GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->data ); 
	} else {
		delBlackColor( image );
		glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, image->sizeX, image->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->data );
		/* gluBuild2DMipmaps( GL_TEXTURE_2D, 4, image->sizeX, image->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, image->data ); */
	}
	if ( image->data != NULL ) {
		delete image;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -