Exemplo n.º 1
0
Arquivo: sgui.c Projeto: rsenn/tichu
static void sgLeaks(sgList *widgets)
{
    sgWidget *widget;
    sgWidget *next;

    sgForeachSafe(widgets, widget, next)
    {
        sgLog("Deleting leaked %s widget: %s",
              widget->type->name, widget->caption);

        sgDeleteWidget(widget);
    }
Exemplo n.º 2
0
void sgTexture::setPixel(int x, int y, sgColorA color)
{
	if(x < 0)
	{
		int facx = -x/width;
		x += facx*width;
	}
	x %= width;

	if(y < 0)
	{
		int facy = -y/height;
		y += facy*height;
		sgLog("%i, %i", facy, y);
	}
	y %= height;

	int bytes = ((format == GL_RGBA)?4:3);
	texdata[y*width*bytes+x*bytes+0] = color.r;
	texdata[y*width*bytes+x*bytes+1] = color.g;
	texdata[y*width*bytes+x*bytes+2] = color.b;
	if(format = GL_RGBA)
		texdata[y*width*bytes+x*bytes+3] = color.a;
}
Exemplo n.º 3
0
void sgTexture::createTexture(const char *filename, bool mipmaps, bool lock)
{
	if(loaded)
		return;

	sgTextureFiles::sgUncompressedTexture *tex = 0;
	if(!sgTextureFiles::loadPNG(&tex, filename))
	{
		if(tex)
		{
			if(tex->bytes)
				delete[] tex->bytes;
			delete tex;
		}

		sgLog("Can´t load texture file: %s", filename);
		return;
	}

	width = tex->width;
	height = tex->height;

/*	if(!((width != 0) && !(width & (width - 1))))
	{
		if(!((height != 0) && !(height & (height - 1))))
		{
			sgLog("Texture resolution is not a power of two: %s", filename);
			width = 0;
			height = 0;
			return;
		}
	}*/

	glGenTextures(1, &texid);
	glBindTexture(GL_TEXTURE_2D, texid);

	if(mipmaps)
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#if defined __IOS__
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		if(sgRenderer::oglversion <= 1)
		{
			glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
		}
#else
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
#endif
	}else
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}

	texdata = tex->bytes;
	if(tex->hasalpha)
	{
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texdata);
		format = GL_RGBA;
	}
	else
	{
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texdata);
		format = GL_RGB;
	}

	if(mipmaps && sgRenderer::oglversion > 1)
	{
#if defined __IOS__
		glGenerateMipmap(GL_TEXTURE_2D);
#endif
	}

	if(!lock)
	{
		if(tex->bytes)
			delete[] tex->bytes;
		texdata = NULL;
	}
	delete tex;

	sgResourceManager::addResource(filename, this);

	loaded = true;
}
Exemplo n.º 4
0
void sgTexture::createPVRTexture(const char *filename)
{
	if(loaded)
		return;

	sgTextureFiles::sgPVRTexture *tex = 0;
	if(!sgTextureFiles::loadPVR(&tex, filename) || tex->mipsizes.size() < 1)
	{
		if(tex)
		{
			if(tex->bytes)
				delete[] tex->bytes;
			delete tex;
		}

		sgLog("Can´t load texture file: %s", filename);
		return;
	}

	width = tex->width;
	height = tex->height;

	unsigned int w = width;
	unsigned int h = height;

	glGenTextures(1, &texid);
	glBindTexture(GL_TEXTURE_2D, texid);

	if(tex->mipsizes.size() > 1)
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	}else
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}

	unsigned long offset = 0;
	for(int i = 0; i < tex->mipsizes.size(); i++)
	{
		glCompressedTexImage2D(GL_TEXTURE_2D, i, tex->glformat, w, h, 0, tex->mipsizes[i], &tex->bytes[offset]);
		offset += tex->mipsizes[i];

		w = fmaxf((unsigned int)(w >> 1), 1.0f);
		h = fmaxf((unsigned int)(h >> 1), 1.0f);
	}

	format = tex->glformat;

	if(tex->bytes)
		delete[] tex->bytes;
	delete tex;

	sgResourceManager::addResource(filename, this);

	loaded = true;
}
Exemplo n.º 5
0
void sgShadowVolume::update(sgLight *firstlight)
{
	sgLight *light = firstlight;

	sgVector3 normal;
	sgVector3 lightdir;

	sgVertex *volvertices;
	unsigned short *volindices;
	memcpy(tempedges, edges, edgenum*sizeof(sgShadowEdge));

	if(light->position.w == 0)
	{
		lightdir = sgVector3(light->position.x, light->position.y, light->position.z);
		lightdir.normalize();
	}

	//Find silouette
	for(unsigned int n = 0; n < facenum; n++)
	{
		normal = object->matnormal*faces[n].normal;

		if(light->position.w != 0)
		{
			sgLog("Shadows for point lights aren´t supported yet!");
			return;
		}

		if(normal.dot(lightdir) <= 0.0)
		{
			if(faces[n].flipped1)
				tempedges[faces[n].edge1].counter2 -= 1;
			else
				tempedges[faces[n].edge1].counter1 -= 1;

			if(faces[n].flipped2)
				tempedges[faces[n].edge2].counter2 -= 1;
			else
				tempedges[faces[n].edge2].counter1 -= 1;

			if(faces[n].flipped3)
				tempedges[faces[n].edge3].counter2 -= 1;
			else
				tempedges[faces[n].edge3].counter1 -= 1;
		}
	}

	unsigned int edgnum = 0;
	for(int x = 0; x < edgenum; x++)
	{
		if(tempedges[x].counter1+tempedges[x].counter2 == 1)
		{
			edgnum += 1;
		}
	}

	//Create mesh
	volvertices = new sgVertex[edgnum*4];
	volindices = new unsigned short[edgnum*6];

	unsigned int indcounter = 0;
	for(int x = 0; x < edgenum; x++)
	{
		if(tempedges[x].counter1+tempedges[x].counter2 == 1)
		{
			if(tempedges[x].counter1 == 1)
			{
				volvertices[0+indcounter*4] = vertices[edges[x].vert1];
				volvertices[0+indcounter*4].normal.makeIdentity();
				volvertices[1+indcounter*4] = vertices[edges[x].vert2];
				volvertices[1+indcounter*4].normal.makeIdentity();

				volvertices[2+indcounter*4] = vertices[edges[x].vert1];
				volvertices[2+indcounter*4].normal = lightdir;
				volvertices[3+indcounter*4] = vertices[edges[x].vert2];
				volvertices[3+indcounter*4].normal = lightdir;
			}else
			{
				volvertices[0+indcounter*4] = vertices[edges[x].vert2];
				volvertices[0+indcounter*4].normal.makeIdentity();
				volvertices[1+indcounter*4] = vertices[edges[x].vert1];
				volvertices[1+indcounter*4].normal.makeIdentity();

				volvertices[2+indcounter*4] = vertices[edges[x].vert2];
				volvertices[2+indcounter*4].normal = lightdir;
				volvertices[3+indcounter*4] = vertices[edges[x].vert1];
				volvertices[3+indcounter*4].normal = lightdir;
			}

			volindices[0+indcounter*6] = 3+indcounter*4;
			volindices[1+indcounter*6] = 2+indcounter*4;
			volindices[2+indcounter*6] = 0+indcounter*4;

			volindices[3+indcounter*6] = 1+indcounter*4;
			volindices[4+indcounter*6] = 3+indcounter*4;
			volindices[5+indcounter*6] = 0+indcounter*4;

			indcounter += 1;
		}
	}

	if(mesh->vertices != NULL)
		delete[] mesh->vertices;
	if(mesh->indices != NULL)
		delete[] mesh->indices;

	mesh->vertexnum = edgnum*4;
	mesh->indexnum = edgnum*6;
	mesh->vertices = (float*)volvertices;
	mesh->indices = volindices;

/*	if(mesh->vbo == -1)
		mesh->generateVBO(true);*/
}