Reputation: 853
This is going to be a really short question without any code, so I feel I shouldn't be asking it, but I can't find any information on it online. I want to lay a color over my entire screen, or more correct terms would be the surface render, in android. For example, I have a simple white background, and then I lay a green overlay on top of it, making it appear green. So like how a view matrix is used to move everything in the scene. Is there a way I can overlay a color on top of my surface render? Again, sorry for having to ask, but I couldn't find any information about it elsewhere.
Upvotes: 0
Views: 1688
Reputation: 3504
Overlaying a (non-translucent) color is same as drawing everything with that color itself. In this case, you should draw every object with that color itself.
If on the other hand, what you meant is overlaying (modulating) with a translucent color, then you need to modify the output colors for every pixel, typically done with the fragment shader. For example say you are drawing everything with a red color, then want to modulate it with another color passed from your application as an uniform, you do like below in the fragment shader main function:
inputcolor = vec4(1.0,0.0,0.0,0.0);
gl_FragColor = inputcolor * modulating_color;
If you are concerned about the full screen itself, and not just the objects, then you also need to take care of the background color, using the glClear()
functionality.
Alternatively you can draw a full screen rectangle and blend, but it is too much of code, compared to localised functions in the fragment shader. Assuming you are using OpenGLES2.0.
Upvotes: 0
Reputation: 947
There's a lot that goes with, what I assume, is opengl es. IN short, you should have a self implemented GLSurfaceView to which you pass your renderer. In your renderer you can use this code to override your onSurfaceCreated with a green background:
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background frame color
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mSquare = new Square();
}
Call mSquare.draw(gl);
in your onDrawFrame
.
You can define your own class of shape and pass that to your renderer as well, specifying it's color. For example:
package com.example.example;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Square {
private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float squareCoords[] = {
-1f, 1f, 0f, // top left
-1f,-1f, 0f, // bottom left
1f,-1f, 0f, // bottom right
1f, 1f, 0f}; // top right
// order to draw vertices
private final short drawOrder[] = {0, 1, 2, 0, 2, 3};
float color[] = {0f, 1f, 0f, 1f};
/**
* Sets up the drawing object data for use in an OpenGL ES context.
*/
public Square() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
}
/**
* Encapsulates the OpenGL ES instructions for drawing this shape.
*
* @param gl - The OpenGL ES context in which to draw this shape.
*/
public void draw(GL10 gl) {
// Since this shape uses vertex arrays, enable them
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// draw the shape
gl.glColor4f( // set color
color[0], color[1],
color[2], color[3]);
gl.glVertexPointer( // point to vertex data:
COORDS_PER_VERTEX,
GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements( // draw shape:
GL10.GL_TRIANGLES,
drawOrder.length, GL10.GL_UNSIGNED_SHORT,
drawListBuffer);
// Disable vertex array drawing to avoid
// conflicts with shapes that don't use it
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
Upvotes: 2