Пример #1
0
	void run() {
	    RGBA c;

	    // test += 
	    c = RGBA(1.0,2.0,3.0,4.0);
	    c += RGBA(1.0,2.0,3.0,4.0);
	    assertTrue(c == RGBA(2.0,4.0,6.0,8.0));
	    assertTrue(c.a() == 8.0);

	    c = RGBA(1.0,2.0,3.0,0.0);
	    c += RGBA(1.0,2.0,3.0,1.0);
	    assertTrue(c == RGBA(2.0,4.0,6.0,1.0));
	    assertTrue(c.a() == 1.0);

	    // test / and *
	    c = RGBA(1.0,2.0,3.0,4.0);
	    c = c * 2.0;
	    assertTrue(c == RGBA(2.0,4.0,6.0,8.0));
	    c = c / 2.0;
	    assertTrue(c == RGBA(1.0,2.0,3.0,4.0));

	    c = RGB(1.0,2.0,3.0);
	    assertTrue(c.a() == 1);

	    assertTrue(RGB(1.0,2.0,3.0) == RGBA(1.0,2.0,3.0,1.0));
	    assertTrue(RGBA(1.0,2.0,3.0,1.0) == RGB(1.0,2.0,3.0));
	}
Пример #2
0
void WidgetBase::RenderOutline(const Vec2& worldPos, const Vec2& widgetSize, float lineWidth)
{
	glPushAttrib(GL_LINE_BIT);
	glLineWidth(lineWidth);
	float opacity = GetOpacity();

	RGBA edgeColor;
	Vertex baseOutlineVertex;
	Vertex outlineVertices[4];
	GetPropertyForCurrentState("border color", edgeColor);

	edgeColor.a() = static_cast<unsigned char>(edgeColor.a() * opacity);

	baseOutlineVertex.m_color = edgeColor;

	Vertex bottomLeftOutlineVertex = baseOutlineVertex;
	Vertex topLeftOutlineVertex = baseOutlineVertex;
	Vertex topRightOutlineVertex = baseOutlineVertex;
	Vertex bottomRightOutlineVertex = baseOutlineVertex;

	bottomLeftOutlineVertex.m_position = worldPos;
	topLeftOutlineVertex.m_position = worldPos + Vec2(0.f, widgetSize.y());
	topRightOutlineVertex.m_position = worldPos + widgetSize;
	bottomRightOutlineVertex.m_position = worldPos + Vec2(widgetSize.x(), 0.f);

	outlineVertices[0] = bottomLeftOutlineVertex;
	outlineVertices[1] = topLeftOutlineVertex;
	outlineVertices[2] = topRightOutlineVertex;
	outlineVertices[3] = bottomRightOutlineVertex;

	Renderer& renderer = Renderer::GetInstance();
	renderer.RenderPrimitives(GL_LINE_LOOP, outlineVertices, 4);
	glPopAttrib();
}
Пример #3
0
void EditLineWidget::Render()
{
	WidgetBase::Render();
	LabelWidget::Render();

	Vec2 worldPos = GetWorldPosition();
	float textOpacity = GetTextOpacity();

	RGBA cursorColor;
	GetPropertyForCurrentState("text color", cursorColor);
	cursorColor.a() = static_cast<unsigned char>(cursorColor.a() * textOpacity);

	float textScale;
	GetPropertyForCurrentState("text scale", textScale);

	double time;
	GetPropertyForCurrentState("blink time", time);

	if ((m_currentBlinkTimeSeconds >= time) && m_canType) {
		Vec2 cursorSize = Vec2(1.f, textScale * 5.f);
		if (m_cursorIndex == 0) {
			RenderBackground(worldPos, cursorSize, cursorColor);
		}
		else {
			std::string leftOfCursor = m_fullText.substr(m_leftmostCharacterIndex, m_cursorIndex - m_leftmostCharacterIndex);
			float cursorOffset = m_fontRenderer->CalcTextWidth(leftOfCursor, cursorSize.y());
			RenderBackground(Vec2(worldPos.x() + cursorOffset, worldPos.y()), cursorSize, cursorColor);
		}
	}
	if (m_currentBlinkTimeSeconds >= (time * 2)) {
		m_currentBlinkTimeSeconds = 0.0;
	}
}
Пример #4
0
inline RGBA Interpolate(const RGBA& start, const RGBA& end, float fractionComplete) {
	RGBA interpRGBA;
	interpRGBA.r() = Interpolate(start.r(), end.r(), fractionComplete);
	interpRGBA.g() = Interpolate(start.g(), end.g(), fractionComplete);
	interpRGBA.b() = Interpolate(start.b(), end.b(), fractionComplete);
	interpRGBA.a() = Interpolate(start.a(), end.a(), fractionComplete);
	return interpRGBA;
}
Пример #5
0
void ProgressBarWidget::Render()
{
	Vec2 worldPos = GetWorldPosition();
	float progress;

	Vec2 size;
	GetPropertyForCurrentState("size", size);

	float borderSize;
	GetPropertyForCurrentState("border size", borderSize);

	float opacity = GetOpacity();

	RGBA backgroundColor;
	RGBA innerColor;
	GetPropertyForCurrentState("color", backgroundColor);
	GetPropertyForCurrentState("inner color", innerColor);

	backgroundColor.a() *= static_cast<unsigned char>(backgroundColor.a() * opacity);
	innerColor.a() = static_cast<unsigned char>(innerColor.a() * opacity);


	RenderBackground(worldPos, size, backgroundColor);
	GetPropertyForCurrentState("progress", progress);

	CardinalDir dir;
	GetPropertyForCurrentState("direction", dir);
	switch (dir) {
	case C_DIRECTION_EAST:
		RenderBackground(worldPos, Vec2(size.x() * progress, size.y()), innerColor);
		break;
	case C_DIRECTION_WEST: {
		Vec2 fillSize = Vec2(size.x() * progress, size.y());
		Vec2 leftEnd = Vec2(worldPos.x() + size.x() - fillSize.x(), worldPos.y());
		RenderBackground(leftEnd, fillSize, innerColor);
		break;
	}
	case C_DIRECTION_SOUTH: {
		Vec2 fillSize = Vec2(size.x(), size.y()  * progress);
		Vec2 top = Vec2(worldPos.x(), worldPos.y() + size.y() - fillSize.y());
		RenderBackground(top, fillSize, innerColor);
		break;
	}
	case C_DIRECTION_NORTH:
		RenderBackground(worldPos, Vec2(size.x(), size.y()  * progress), innerColor);
		break;
	}

	RenderOutline(worldPos, size, borderSize);

	WidgetBase::ProcessRenderEvent();
	/*
	RenderBackground(worldPos + (size * 0.25f), size * 0.5f, innerColor);
	RenderOutline(worldPos, size, borderSize);
	*/
}
Пример #6
0
void ImageDrawing::filledcircle(Image* image, float x, float y, float r, const RGBA& c, AlphaCombineMode am) {
    for(float yi = y-r; yi < y+r; yi++) {
        float phi = ::asin(::abs(yi-y)/r);
        float xr = abs(::cos(phi)*r);
        // Antialiased begin point
        float a = 1 - ((x-xr) - int(x-xr));
        RGBA d = RGBA(c.r(), c.g(), c.b(), c.a() * a);
        pixel(image, int(x-xr), yi, d, ImageDrawing::DECAL);
        // Middle 
        for(float xi = x-xr+1; xi <= int(x+xr); xi++) {
            pixel(image, xi, yi, c);
        }
        // Antialiased end point
        a = ((x+xr) - int(x+xr));
        d = RGBA(c.r(), c.g(), c.b(), c.a() * a);
        pixel(image, int(x+xr), yi, d, ImageDrawing::DECAL);
    }
}
Пример #7
0
void WidgetBase::Render()
{
	Vec2 worldPos = GetWorldPosition();

	float opacity = GetOpacity();

	RGBA backgroundColor;
	GetPropertyForCurrentState("color", backgroundColor);
	backgroundColor.a() = static_cast<unsigned char>(backgroundColor.a() * opacity);

	Vec2 size;
	GetPropertyForCurrentState("size", size);

	float borderSize;
	GetPropertyForCurrentState("border size", borderSize);

	RenderBackground(worldPos, size, backgroundColor);
	RenderOutline(worldPos, size, borderSize);
}
Пример #8
0
RGBA ColorMatrix::operator*(const RGBA& color) const {
    double result[4] = {0,0,0,0};
    double c[4] = {color.r(), color.g(), color.b(), color.a()};

    for(int y = 0; y < 4; y++) {
	result[y] = matrix[y*4 + 0] * c[0] +
	            matrix[y*4 + 1] * c[1] +
	            matrix[y*4 + 2] * c[2] +
	            matrix[y*4 + 3] * c[3];
    }
    return RGBA(c[0],c[1],c[2],c[3]);
}
Пример #9
0
static bool readRGBA(const zeitgeist::ParameterList& in, RGBA& m)
{
    if (
        (in.GetSize() != 4) ||
        (! in.GetValue(in[0], m.r())) ||
        (! in.GetValue(in[1], m.g())) ||
        (! in.GetValue(in[2], m.b())) ||
        (! in.GetValue(in[3], m.a()))
        )
        {
            return false;
        }

    return true;
}
Пример #10
0
void ImageDrawing::pixel(Image* image, float xf, float yf, const RGBA& color, ImageDrawing::AlphaCombineMode am) {
    int x = int(xf);
    int y = int(yf);
    if (x >= 0 && x < image->getWidth() && y >= 0 && y < image->getHeight()) {
        RGBA c = color;
        double a = c.a();
        if (a < 1.0) {
            RGB existing = image->getRGBA(x,y);    
            if (am == ImageDrawing::ADD) {
                c = existing + c * a;
            } else if (am == ImageDrawing::DECAL) {
                c = (c * a) + (existing * (1.0 - a));
            } 
        }        
        image->setRGBA(x,y,c); 
    }    
}
Пример #11
0
void Buffer::change_color(uint32_t vertex_offset, uint32_t vertex_count, const RGBA& color)
{
	ColorStruct clr;
	clr.color[0] = color.r();
	clr.color[1] = color.g();
	clr.color[2] = color.b();
	clr.color[3] = color.a();
	
	std::vector<ColorStruct> colors;
	for (uint32_t i=0; i < vertex_count; i++)
		colors.push_back(clr);

	glBindBuffer(GL_ARRAY_BUFFER, color_buffer_id);
	glBufferSubData(GL_ARRAY_BUFFER, vertex_offset * sizeof(ColorStruct), 
		vertex_count * sizeof(ColorStruct), &colors[0]);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Пример #12
0
static bool
readRGBAVal(const zeitgeist::ParameterList& in, RGBA& m)
{
    int r,g,b;

    if (
        (in.GetSize() != 4) ||
        (! in.GetValue(in[0], r)) ||
        (! in.GetValue(in[1], g)) ||
        (! in.GetValue(in[2], b)) ||
        (! in.GetValue(in[3], m.a()))
        )
    {
        return false;
    }
    m.r() = r / 255.0;
    m.g() = g / 255.0;
    m.b() = b / 255.0;
    return true;
}
Пример #13
0
RGBA applyAlpha(double a, const RGBA& c) {
    return RGBA(c.r(), c.g(), c.b(), c.a()*a);         
}
Пример #14
0
bool Buffer::add_ctm_data(CTMDecoder& ctm, const glm::mat4& matrix, const RGBA& color, Box& bbox, BufferOffset& offset)
{
	auto vertCnt = ctm.get_vertex_count();
	auto triCnt = ctm.get_tri_count();
	auto indexCnt = triCnt * 3;

	update_buffer_count();

	if ((buf_vertex_count + vertCnt) > MAX_VERTEX_COUNT || (buf_index_count + indexCnt) > MAX_INDEX_COUNT)
		return false;

	vertex_buffer.reserve(buf_vertex_count + vertCnt);
	index_buffer.reserve(buf_index_count + indexCnt);

	transparent = color.is_transparent();

	const float* ctm_vertices = ctm.get_vertices();
	const unsigned int* ctm_indices = ctm.get_indices();

	offset.vertex_offset = buf_vertex_count;
	offset.vertex_count = vertCnt;
	offset.index_offset = buf_index_count;
	offset.index_count = indexCnt;

	for (unsigned int i=0; i < vertCnt; i++)
	{
		glm::vec4 pos(ctm_vertices[i * 3], ctm_vertices[i * 3 + 1], ctm_vertices[i * 3 + 2], 1);
		pos = matrix * pos;
		bbox.add_point(glm::vec3(pos));

		VertexStruct vertex;
		vertex.position[0] = pos.x;
		vertex.position[1] = pos.y;
		vertex.position[2] = pos.z;

		vertex.normal[0] = 0;
		vertex.normal[1] = 0;
		vertex.normal[2] = 0;

		ColorStruct clr;
		clr.color[0] = color.r();
		clr.color[1] = color.g();
		clr.color[2] = color.b();
		clr.color[3] = color.a();

		vertex_buffer.push_back(vertex);
		color_buffer.push_back(clr);
	}

	for (unsigned int i=0; i < indexCnt; i += 3)
	{
		auto i0 = ctm_indices[i] + buf_vertex_count;
		auto i1 = ctm_indices[i + 1] + buf_vertex_count;
		auto i2 = ctm_indices[i + 2] + buf_vertex_count;

		index_buffer.push_back(i0);
		index_buffer.push_back(i1);
		index_buffer.push_back(i2);

		glm::vec3 p0(vertex_buffer[i0].position[0], vertex_buffer[i0].position[1], vertex_buffer[i0].position[2]);
		glm::vec3 p1(vertex_buffer[i1].position[0], vertex_buffer[i1].position[1], vertex_buffer[i1].position[2]);
		glm::vec3 p2(vertex_buffer[i2].position[0], vertex_buffer[i2].position[1], vertex_buffer[i2].position[2]);
		glm::vec3 norm = glm::cross((p1 - p0), (p2 - p0));
		
		vertex_buffer[i0].normal[0] += norm.x;
		vertex_buffer[i0].normal[1] += norm.y;
		vertex_buffer[i0].normal[2] += norm.z;

		vertex_buffer[i1].normal[0] += norm.x;
		vertex_buffer[i1].normal[1] += norm.y;
		vertex_buffer[i1].normal[2] += norm.z;

		vertex_buffer[i2].normal[0] += norm.x;
		vertex_buffer[i2].normal[1] += norm.y;
		vertex_buffer[i2].normal[2] += norm.z;
	}

	for (unsigned int i=0; i < vertCnt; i++)
	{
		glm::vec3 norm(vertex_buffer[buf_vertex_count + i].normal[0], 
					vertex_buffer[buf_vertex_count + i].normal[1], 
					vertex_buffer[buf_vertex_count + i].normal[2]);
		norm = glm::normalize(norm);
		vertex_buffer[buf_vertex_count + i].normal[0] = norm.x;
		vertex_buffer[buf_vertex_count + i].normal[1] = norm.y;
		vertex_buffer[buf_vertex_count + i].normal[2] = norm.z;
	}

	return true;
}
Пример #15
0
u32 Surface::MapRGB(const RGBA & color) const
{
    return amask() ? SDL_MapRGBA(surface->format, color.r(), color.g(), color.b(), color.a()) : SDL_MapRGB(surface->format, color.r(), color.g(), color.b());
}
Пример #16
0
void PngIO::save(const Image* const image, FILE* fp) const {

    png_structp png_ptr;
    png_infop info_ptr;

    int width = image->getWidth();
    int height = image->getHeight();

    /* Create and initialize the png_struct with the desired error handler
     * functions.  If you want to use the default stderr and longjump method,
     * you can supply NULL for the last three parameters.  We also check that
     * the library version is compatible with the one used at compile time,
     * in case we are using dynamically linked libraries.  REQUIRED.
     */
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
	    NULL, NULL, NULL);

    if (png_ptr == NULL)
    {
	::fclose(fp);
	throw_exception("Error saving PNG");
    }

    /* Allocate/initialize the image information data.  REQUIRED */
    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL)
    {
	::fclose(fp);
	png_destroy_write_struct(&png_ptr,  NULL);
	throw_exception("Error saving PNG");
    }

    /* Set error handling.  REQUIRED if you aren't supplying your own
     * error handling functions in the png_create_write_struct() call.
     */
    if (setjmp(png_jmpbuf(png_ptr)))
    {
	/* If we get here, we had a problem reading the file */
	::fclose(fp);
	png_destroy_write_struct(&png_ptr, &info_ptr);
	throw_exception("Error saving ");
    }

    png_init_io(png_ptr, fp);

    png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
	    PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, 
	    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

    /*
       text_ptr[0].key = "Title";
       text_ptr[0].text = "RayGay Image";
       text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
       text_ptr[1].key = "Author";
       text_ptr[1].text = "RayGay";
       text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
       text_ptr[2].key = "Description";
       text_ptr[2].text = "An image rendered with RayGay";
       text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
#ifdef PNG_iTXt_SUPPORTED
text_ptr[0].lang = NULL;
text_ptr[1].lang = NULL;
text_ptr[2].lang = NULL;
#endif

png_set_text(png_ptr, info_ptr, text_ptr, 3);
*/
    png_write_info(png_ptr, info_ptr);

    /* If you are only writing one row at a time, this works */
    png_byte row[width*4];
    png_bytep rowp = row;

    for (int y = 0; y < height; y++)
    {
	for(int x = 0; x < width; x++) {
	    RGBA pixel = image->getRGBA(x,y);
	    row[x*4 + 0] = int(pixel.r() * 255);
	    row[x*4 + 1] = int(pixel.g() * 255);
	    row[x*4 + 2] = int(pixel.b() * 255);
	    row[x*4 + 3] = int(pixel.a() * 255);
	}
	png_write_rows(png_ptr, &rowp, 1);
    }

    png_write_end(png_ptr, info_ptr);

    /* clean up after the write, and free any memory allocated */
    png_destroy_write_struct(&png_ptr, &info_ptr);
}