Fixed orientation and color when rendering to texture
authorSam Lantinga <slouken@libsdl.org>
Fri, 22 Jun 2012 11:38:49 -0400
changeset 6341057bbdb514d5
parent 6340 eadb3790009f
child 6342 ac83b73f0edd
Fixed orientation and color when rendering to texture
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/opengles2/SDL_render_gles2.c	Thu Jun 21 14:01:47 2012 -0300
     1.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Fri Jun 22 11:38:49 2012 -0400
     1.3 @@ -833,7 +833,7 @@
     1.4          break;
     1.5      case GLES2_IMAGESOURCE_TEXTURE_ABGR:
     1.6          ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC;
     1.7 -            break;
     1.8 +        break;
     1.9      case GLES2_IMAGESOURCE_TEXTURE_ARGB:
    1.10          ftype = GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC;
    1.11          break;
    1.12 @@ -906,15 +906,23 @@
    1.13      projection[0][2] = 0.0f;
    1.14      projection[0][3] = 0.0f;
    1.15      projection[1][0] = 0.0f;
    1.16 -    projection[1][1] = -2.0f / renderer->viewport.h;
    1.17 +    if (renderer->target) {
    1.18 +        projection[1][1] = 2.0f / renderer->viewport.h;
    1.19 +    } else {
    1.20 +        projection[1][1] = -2.0f / renderer->viewport.h;
    1.21 +    }
    1.22      projection[1][2] = 0.0f;
    1.23      projection[1][3] = 0.0f;
    1.24      projection[2][0] = 0.0f;
    1.25      projection[2][1] = 0.0f;
    1.26 -    projection[2][2] = 1.0f;
    1.27 +    projection[2][2] = 0.0f;
    1.28      projection[2][3] = 0.0f;
    1.29      projection[3][0] = -1.0f;
    1.30 -    projection[3][1] = 1.0f;
    1.31 +    if (renderer->target) {
    1.32 +        projection[3][1] = -1.0f;
    1.33 +    } else {
    1.34 +        projection[3][1] = 1.0f;
    1.35 +    }
    1.36      projection[3][2] = 0.0f;
    1.37      projection[3][3] = 1.0f;
    1.38  
    1.39 @@ -1027,11 +1035,21 @@
    1.40  
    1.41      /* Select the color to draw with */
    1.42      locColor = rdata->current_program->uniform_locations[GLES2_UNIFORM_COLOR];
    1.43 -    rdata->glUniform4f(locColor,
    1.44 -                renderer->r * inv255f,
    1.45 -                renderer->g * inv255f,
    1.46 -                renderer->b * inv255f,
    1.47 -                renderer->a * inv255f);
    1.48 +    if (renderer->target &&
    1.49 +        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
    1.50 +         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
    1.51 +        rdata->glUniform4f(locColor,
    1.52 +                    renderer->b * inv255f,
    1.53 +                    renderer->g * inv255f,
    1.54 +                    renderer->r * inv255f,
    1.55 +                    renderer->a * inv255f);
    1.56 +    } else {
    1.57 +        rdata->glUniform4f(locColor,
    1.58 +                    renderer->r * inv255f,
    1.59 +                    renderer->g * inv255f,
    1.60 +                    renderer->b * inv255f,
    1.61 +                    renderer->a * inv255f);
    1.62 +    }
    1.63      return 0;
    1.64  }
    1.65  
    1.66 @@ -1248,17 +1266,27 @@
    1.67      /* Select the target texture */
    1.68      locTexture = rdata->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
    1.69      rdata->glGetError();
    1.70 -	rdata->glActiveTexture(GL_TEXTURE0);
    1.71 +    rdata->glActiveTexture(GL_TEXTURE0);
    1.72      rdata->glBindTexture(tdata->texture_type, tdata->texture);
    1.73      rdata->glUniform1i(locTexture, 0);
    1.74  
    1.75      /* Configure color modulation */
    1.76      locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
    1.77 -    rdata->glUniform4f(locModulation,
    1.78 -                texture->r * inv255f,
    1.79 -                texture->g * inv255f,
    1.80 -                texture->b * inv255f,
    1.81 -                texture->a * inv255f);
    1.82 +    if (renderer->target &&
    1.83 +        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
    1.84 +         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
    1.85 +        rdata->glUniform4f(locModulation,
    1.86 +                    texture->b * inv255f,
    1.87 +                    texture->g * inv255f,
    1.88 +                    texture->r * inv255f,
    1.89 +                    texture->a * inv255f);
    1.90 +    } else {
    1.91 +        rdata->glUniform4f(locModulation,
    1.92 +                    texture->r * inv255f,
    1.93 +                    texture->g * inv255f,
    1.94 +                    texture->b * inv255f,
    1.95 +                    texture->a * inv255f);
    1.96 +    }
    1.97  
    1.98      /* Configure texture blending */
    1.99      GLES2_SetBlendMode(rdata, blendMode);
   1.100 @@ -1266,27 +1294,14 @@
   1.101      GLES2_SetTexCoords(rdata, SDL_TRUE);
   1.102  
   1.103      /* Emit the textured quad */
   1.104 -    if (renderer->target) {
   1.105 -        // Flip the texture vertically to compensate for the inversion it'll be subjected to later when it's rendered to the screen
   1.106 -        vertices[0] = (GLfloat)dstrect->x;
   1.107 -        vertices[1] = (GLfloat)renderer->viewport.h-dstrect->y;
   1.108 -        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.109 -        vertices[3] = (GLfloat)renderer->viewport.h-dstrect->y;
   1.110 -        vertices[4] = (GLfloat)dstrect->x;
   1.111 -        vertices[5] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
   1.112 -        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.113 -        vertices[7] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
   1.114 -    }
   1.115 -    else {
   1.116 -        vertices[0] = (GLfloat)dstrect->x;
   1.117 -        vertices[1] = (GLfloat)dstrect->y;
   1.118 -        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.119 -        vertices[3] = (GLfloat)dstrect->y;
   1.120 -        vertices[4] = (GLfloat)dstrect->x;
   1.121 -        vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
   1.122 -        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.123 -        vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
   1.124 -    }
   1.125 +    vertices[0] = (GLfloat)dstrect->x;
   1.126 +    vertices[1] = (GLfloat)dstrect->y;
   1.127 +    vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.128 +    vertices[3] = (GLfloat)dstrect->y;
   1.129 +    vertices[4] = (GLfloat)dstrect->x;
   1.130 +    vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
   1.131 +    vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.132 +    vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
   1.133      rdata->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
   1.134      texCoords[0] = srcrect->x / (GLfloat)texture->w;
   1.135      texCoords[1] = srcrect->y / (GLfloat)texture->h;
   1.136 @@ -1423,11 +1438,21 @@
   1.137  
   1.138      /* Configure color modulation */
   1.139      locModulation = rdata->current_program->uniform_locations[GLES2_UNIFORM_MODULATION];
   1.140 -    rdata->glUniform4f(locModulation,
   1.141 -                texture->r * inv255f,
   1.142 -                texture->g * inv255f,
   1.143 -                texture->b * inv255f,
   1.144 -                texture->a * inv255f);
   1.145 +    if (renderer->target &&
   1.146 +        (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
   1.147 +         renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
   1.148 +        rdata->glUniform4f(locModulation,
   1.149 +                    texture->b * inv255f,
   1.150 +                    texture->g * inv255f,
   1.151 +                    texture->r * inv255f,
   1.152 +                    texture->a * inv255f);
   1.153 +    } else {
   1.154 +        rdata->glUniform4f(locModulation,
   1.155 +                    texture->r * inv255f,
   1.156 +                    texture->g * inv255f,
   1.157 +                    texture->b * inv255f,
   1.158 +                    texture->a * inv255f);
   1.159 +    }
   1.160  
   1.161      /* Configure texture blending */
   1.162      GLES2_SetBlendMode(rdata, blendMode);
   1.163 @@ -1435,27 +1460,14 @@
   1.164      GLES2_SetTexCoords(rdata, SDL_TRUE);
   1.165  
   1.166      /* Emit the textured quad */
   1.167 -    if (renderer->target) {
   1.168 -        // Flip the texture vertically to compensate for the inversion it'll be subjected to later when it's rendered to the screen
   1.169 -        vertices[0] = (GLfloat)dstrect->x;
   1.170 -        vertices[1] = (GLfloat)renderer->viewport.h-dstrect->y;
   1.171 -        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.172 -        vertices[3] = (GLfloat)renderer->viewport.h-dstrect->y;
   1.173 -        vertices[4] = (GLfloat)dstrect->x;
   1.174 -        vertices[5] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
   1.175 -        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.176 -        vertices[7] = (GLfloat)renderer->viewport.h-(dstrect->y + dstrect->h);
   1.177 -    }
   1.178 -    else {
   1.179 -        vertices[0] = (GLfloat)dstrect->x;
   1.180 -        vertices[1] = (GLfloat)dstrect->y;
   1.181 -        vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.182 -        vertices[3] = (GLfloat)dstrect->y;
   1.183 -        vertices[4] = (GLfloat)dstrect->x;
   1.184 -        vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
   1.185 -        vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.186 -        vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
   1.187 -    }
   1.188 +    vertices[0] = (GLfloat)dstrect->x;
   1.189 +    vertices[1] = (GLfloat)dstrect->y;
   1.190 +    vertices[2] = (GLfloat)(dstrect->x + dstrect->w);
   1.191 +    vertices[3] = (GLfloat)dstrect->y;
   1.192 +    vertices[4] = (GLfloat)dstrect->x;
   1.193 +    vertices[5] = (GLfloat)(dstrect->y + dstrect->h);
   1.194 +    vertices[6] = (GLfloat)(dstrect->x + dstrect->w);
   1.195 +    vertices[7] = (GLfloat)(dstrect->y + dstrect->h);
   1.196      if (flip & SDL_FLIP_HORIZONTAL) {
   1.197          tmp = vertices[0];
   1.198          vertices[0] = vertices[4] = vertices[2];