Răsfoiți Sursa

Fixed vertex order in plane primitive, added floating point texture support

Ivan Safrin 13 ani în urmă
părinte
comite
e352f2e803

+ 4 - 1
Core/Contents/Include/PolyGLTexture.h

@@ -60,7 +60,10 @@ namespace Polycode {
 		private:
 			
 			bool glTextureLoaded;
-			GLuint glTextureType;
+			GLenum glTextureType;
+			GLuint glTextureFormat;
+			GLenum pixelType;
+			
 			int filteringMode;
 			GLuint textureID;
 			GLuint frameBufferID;

+ 2 - 0
Core/Contents/Include/PolyGlobals.h

@@ -70,6 +70,8 @@ THE SOFTWARE.
 
 typedef double Number;
 
+#define RANDOM_NUMBER ((Number)rand()/(Number)RAND_MAX)
+
 inline Number clampf(Number x, Number a, Number b)
 {
     return x < a ? a : (x > b ? b : x);

+ 10 - 0
Core/Contents/Include/PolyImage.h

@@ -188,6 +188,15 @@ namespace Polycode {
 			void darken(Number amt, bool color, bool alpha);
 			void lighten(Number amt, bool color, bool alpha);
 			void multiply(Number amt, bool color, bool alpha);
+						
+			/**
+			* Returns an area of the image buffer. The area can go outside of image bounds, in which case the pixels not within the image are zeroed out. This method allocates new memory for the returned buffer and you must free it manually.
+			* @param x X position of the area to return.
+			* @param y Y position of the area to return.
+			* @param width Width of the area to return.
+			* @param height Height of the area to return.					
+			*/			
+			char *getPixelsInRect(unsigned int x, unsigned int y, unsigned int width, unsigned int height);
 			
 			/**
 			* Returns the x position of the brush.
@@ -223,6 +232,7 @@ namespace Polycode {
 		
 			static const int IMAGE_RGB = 0;
 			static const int IMAGE_RGBA = 1;
+			static const int IMAGE_FP16 = 2;
 		
 		protected:
 		

+ 6 - 0
Core/Contents/Include/PolyScreenMesh.h

@@ -36,6 +36,12 @@ namespace Polycode {
 	*/
 	class _PolyExport ScreenMesh : public ScreenEntity {
 		public:
+		
+			/**
+			* Creates the screen mesh from existing Mesh.
+			*/
+			ScreenMesh(Mesh *mesh);
+			
 			/**
 			* Creates the screen mesh and loads a mesh from a file name.
 			*/

+ 2 - 2
Core/Contents/Source/PolyGLRenderer.cpp

@@ -400,7 +400,7 @@ void OpenGLRenderer::drawVertexBuffer(VertexBuffer *buffer, bool enableColorBuff
 			}
 			break;
 		case Mesh::LINE_MESH:
-			mode = GL_LINES;
+			mode = GL_LINE_STRIP;
 			break;	
 		case Mesh::POINT_MESH:
 			mode = GL_POINTS;
@@ -1025,7 +1025,7 @@ void OpenGLRenderer::drawArrays(int drawType) {
 			}
 			break;
 		case Mesh::LINE_MESH:
-			mode = GL_LINES;
+			mode = GL_LINE_STRIP;
 			break;	
 		case Mesh::POINT_MESH:
 			mode = GL_POINTS;

+ 23 - 9
Core/Contents/Source/PolyGLTexture.cpp

@@ -35,11 +35,24 @@ OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height, char *text
 	glTextureLoaded = false;
 	frameBufferID = 999999;
 	
-	glTextureType = GL_RGBA;
-	if(type == Image::IMAGE_RGB) {
-		glTextureType = GL_RGB;		
-	} 
-	
+	switch(type) {
+		case Image::IMAGE_RGB:
+			glTextureType = GL_RGB;
+			glTextureFormat = GL_RGB;				
+			pixelType = GL_UNSIGNED_BYTE;			
+		break;
+		case Image::IMAGE_FP16:
+			glTextureType = GL_RGBA;
+			glTextureFormat = GL_RGBA16F_ARB;				
+			pixelType = GL_FLOAT;
+		break;		
+		default:
+			glTextureType = GL_RGBA;
+			glTextureFormat = GL_RGBA;	
+			pixelType = GL_UNSIGNED_BYTE;	
+		break;
+	}
+		
 	recreateFromImageData();
 }
 
@@ -52,6 +65,7 @@ void OpenGLTexture::recreateFromImageData() {
 	
 	glGenTextures(1, &textureID);
 	glBindTexture(GL_TEXTURE_2D, textureID);
+	
 	if(clamp) {
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
@@ -70,13 +84,13 @@ void OpenGLTexture::recreateFromImageData() {
 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 				if(textureData) {
-					gluBuild2DMipmaps(GL_TEXTURE_2D, glTextureType, width, height, glTextureType, GL_UNSIGNED_BYTE, textureData );
+					gluBuild2DMipmaps(GL_TEXTURE_2D, glTextureFormat, width, height, glTextureType, pixelType, textureData );
 				}
 			} else {
 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);		
 				if(textureData) {
-					glTexImage2D(GL_TEXTURE_2D, 0, glTextureType, width, height, 0, glTextureType, GL_UNSIGNED_BYTE, textureData);							
+					glTexImage2D(GL_TEXTURE_2D, 0, glTextureFormat, width, height, 0, glTextureType, pixelType, textureData);							
 				}						
 			}
 			break;
@@ -84,7 +98,7 @@ void OpenGLTexture::recreateFromImageData() {
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);		
 			if(textureData) {
-				glTexImage2D(GL_TEXTURE_2D, 0, glTextureType, width, height, 0, glTextureType, GL_UNSIGNED_BYTE, textureData);							
+				glTexImage2D(GL_TEXTURE_2D, 0, glTextureFormat, width, height, 0, glTextureType, pixelType, textureData);							
 			}			
 			break;
 	}	
@@ -104,7 +118,7 @@ void OpenGLTexture::setGLInfo(GLuint textureID, GLuint frameBufferID) {
 void OpenGLTexture::setTextureData(char *data) {
 	glBindTexture(GL_TEXTURE_2D, textureID);
 	glDrawBuffer(GL_AUX0);
-	glDrawPixels(width, height, glTextureType, GL_UNSIGNED_BYTE, data);
+	glDrawPixels(width, height, glTextureType, pixelType, data);
 	glReadBuffer(GL_AUX0);
 //	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 128, 128, 0);
 }

+ 36 - 2
Core/Contents/Source/PolyImage.cpp

@@ -54,9 +54,12 @@ void Image::setPixelType(int type) {
 			break;
 		case IMAGE_RGBA:
 			pixelSize = 4;						
-			break;
-			pixelSize = 4;						
+		break;
+		case IMAGE_FP16:		
+			pixelSize = 16;
+		break;
 		default:
+			pixelSize = 4;								
 			break;
 	}
 }
@@ -99,6 +102,37 @@ char *Image::getPixels() {
 	return imageData;
 }
 
+char *Image::getPixelsInRect(unsigned int x, unsigned int y, unsigned int width, unsigned int height) {
+	char *retBuf = (char*) malloc(pixelSize * width * height);
+	memset(retBuf, 0, pixelSize * width * height);
+	
+	if(x < this->width-1 && y < this->height-1) {
+		
+		unsigned int xAmt;
+		unsigned int yAmt;	
+		if(x + width > this->width) {
+			xAmt = this->width - x;
+		} else {
+			xAmt = width;
+		}
+
+		if(y + height > this->height) {
+			yAmt = this->height - y;
+		} else {
+			yAmt = height;
+		}
+
+		for(int i=0; i < yAmt; i++) {
+			long srcOffset = ((pixelSize*this->width) * (y+i)) + (pixelSize*x);
+			long dstOffset = (pixelSize*xAmt) * i;
+			memcpy(retBuf + dstOffset, imageData+srcOffset, pixelSize * xAmt);
+		}	
+	}
+		
+	return retBuf;
+}
+
+
 Color Image::getPixel(int x, int y) {
 	if(x < 0 || x >= width || y < 0 || y >= height)
 		return Color(0,0,0,0);

+ 6 - 4
Core/Contents/Source/PolyMesh.cpp

@@ -255,10 +255,12 @@ namespace Polycode {
 	
 	void Mesh::createVPlane(Number w, Number h) { 
 		Polygon *imagePolygon = new Polygon();
-		imagePolygon->addVertex(0,h,0,1,1);	
-		imagePolygon->addVertex(w,h,0, 0, 1);			
-		imagePolygon->addVertex(w,0,0, 0, 0);		
-		imagePolygon->addVertex(0,0,0,1,0);
+		
+		imagePolygon->addVertex(0,0,0,0,0);
+		imagePolygon->addVertex(w,0,0, 1, 0);		
+		imagePolygon->addVertex(w,h,0, 1, 1);									
+		imagePolygon->addVertex(0,h,0,0,1);	
+
 
 		addPolygon(imagePolygon);
 		

+ 5 - 5
Core/Contents/Source/PolySceneLabel.cpp

@@ -35,10 +35,6 @@ SceneLabel::SceneLabel(const String& fontName, const String& text, int size, Num
 	label = new Label(CoreServices::getInstance()->getFontManager()->getFontByName(fontName), text, size, amode);
 	this->scale = scale;
 	setText(text);
-	
-	for(int i=0; i < mesh->getPolygonCount(); i++) {
-		mesh->getPolygon(i)->flipUVY();
-	}
 	mesh->arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
 }
 
@@ -63,7 +59,11 @@ void SceneLabel::setText(const String& newText) {
 
 	delete mesh;
 	mesh = new Mesh(Mesh::QUAD_MESH);
-	mesh->createPlane(label->getWidth()*scale,label->getHeight()*scale);
+	mesh->createVPlane(label->getWidth()*scale,label->getHeight()*scale);
+	
+	for(int i=0; i < mesh->getPolygonCount(); i++) {
+		mesh->getPolygon(i)->flipUVY();
+	}
 	
 	// TODO: resize it here
 	

+ 4 - 0
Core/Contents/Source/PolyScreenMesh.cpp

@@ -28,6 +28,10 @@
 
 using namespace Polycode;
 
+ScreenMesh::ScreenMesh(Mesh *mesh) : ScreenEntity(), texture(NULL) {
+	this->mesh = mesh;
+}
+
 ScreenMesh::ScreenMesh(const String& fileName) : ScreenEntity(), texture(NULL) {
 	mesh = new Mesh(fileName);
 }

+ 18 - 12
Core/Contents/Source/PolyTexture.cpp

@@ -31,16 +31,19 @@ Texture::Texture(unsigned int width, unsigned int height, char *textureData,bool
 	this->clamp = clamp;
 	this->createMipmaps = createMipmaps;
 	
-	switch (type) {
+	switch(type) {
 		case Image::IMAGE_RGB:
-			pixelSize = 3;
+			pixelSize = 3;			
 			break;
 		case Image::IMAGE_RGBA:
-			pixelSize = 4;
-			break;			
+			pixelSize = 4;						
+		break;
+		case Image::IMAGE_FP16:		
+			pixelSize = 16;
+		break;
 		default:
-			pixelSize = 3;			
-			break;
+			pixelSize = 4;								
+		break;
 	}
 	
 	this->textureData = (char*)malloc(width*height*pixelSize);
@@ -71,14 +74,17 @@ void Texture::setImageData(Image *data) {
 
 	switch (data->getType()) {
 		case Image::IMAGE_RGB:
-			pixelSize = 3;
-			break;
+			pixelSize = 3;			
+		break;
 		case Image::IMAGE_RGBA:
-			pixelSize = 4;
-			break;			
+			pixelSize = 4;						
+		break;
+		case Image::IMAGE_FP16:		
+			pixelSize = 16;
+		break;
 		default:
-			pixelSize = 3;			
-			break;
+			pixelSize = 4;								
+		break;
 	}