I'm trying to make a post processing shader using a framebuffer. But when I attempt to sample the texture in the shaders it doesn't do anything.
As soon as I comment the line(in the fragment shader)
vec4 textureColour = texture(screenTexture, textureCoords);
out everything works again
FrameBuffer.java
package postprocessing;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL32;
public class FrameBuffer {
protected static final int WIDTH = Display.getWidth();
private static final int HEIGHT = Display.getHeight();
private int framebuffer;
private int texture;
private int depthBuffer;
private int depthTexture;
public FrameBuffer()
{
initialise();
}
public void cleanUp()
{
GL30.glDeleteFramebuffers(framebuffer);
GL11.glDeleteTextures(texture);
GL30.glDeleteRenderbuffers(depthBuffer);
GL11.glDeleteTextures(depthTexture);
}
public int getTexture()
{
return texture;
}
public int getDepthTexture()
{
return depthTexture;
}
private void initialise()
{
framebuffer = createFrameBuffer();
texture = createTextureAttachment(WIDTH, HEIGHT);
depthBuffer = createDepthBufferAttachment(WIDTH, HEIGHT);
depthTexture = createDepthTextureAttachment(WIDTH, HEIGHT);
unbindCurrentFrameBuffer();
}
public void bindFrameBuffer(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);//To make sure the texture isn't bound
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, framebuffer);
GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public void unbindCurrentFrameBuffer() {//call to switch to default frame buffer
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
}
private int createFrameBuffer() {
int frameBuffer = GL30.glGenFramebuffers();
//generate name for frame buffer
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
//create the framebuffer
GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
//indicate that we will always render to color attachment 0
return frameBuffer;
}
private int createTextureAttachment( int width, int height) {
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0,
texture, 0);
return texture;
}
private int createDepthTextureAttachment(int width, int height){
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT32, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
return texture;
}
private int createDepthBufferAttachment(int width, int height) {
int depthBuffer = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width,
height);
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT,
GL30.GL_RENDERBUFFER, depthBuffer);
return depthBuffer;
}
}
FrameBufferShader.java
package postprocessing;
import shaders.ShaderProgram;
public class FrameBufferShader extends ShaderProgram {
private static final String VERTEX_FILE = "src/postprocessing/vertexShader.txt";
private static final String FRAGMENT_FILE = "src/postprocessing/fragmentShader.txt";
private int location_texture;
private int location_depthMap;
public FrameBufferShader()
{
super(VERTEX_FILE, FRAGMENT_FILE);
}
#Override
protected void getAllUniformLocations()
{
bindAttributes(0, "position");
}
#Override
protected void bindAttributes()
{
location_texture = super.getUniformLocation("screenTexture");
location_depthMap = super.getUniformLocation("depthMap");
}
public void connectTextureUnits()
{
super.loadInt(location_texture, 0);
super.loadInt(location_depthMap, 1);
}
}
FrameBufferRenderer.java
package postprocessing;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import models.RawModel;
import renderEngine.Loader;
public class FrameBufferRenderer {
private RawModel quad;
private FrameBufferShader shader;
private FrameBuffer fbo;
public FrameBufferRenderer(Loader loader, FrameBufferShader shader, FrameBuffer fbo)
{
this.fbo = fbo;
this.shader = shader;
setUpVAO(loader);
shader.start();
shader.connectTextureUnits();
shader.stop();
}
public void render()
{
prepareRender();
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, quad.getVertexCount());
unbind();
}
private void prepareRender()
{
shader.start();
GL30.glBindVertexArray(quad.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getTexture());
GL13.glActiveTexture(GL13.GL_TEXTURE1);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getDepthTexture());
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
private void unbind()
{
GL11.glDisable(GL11.GL_BLEND);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
shader.stop();
}
private void setUpVAO(Loader loader) {
float[] vertices = { -1, -1, 1, -1, 1, 1, -1, 1 };
quad = loader.loadtoVAO(vertices, 2);
}
}
Vertex Shader
#version 400
in vec2 position;
void main()
{
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}
Fragment Shader
#version 400
in vec2 textureCoords;
out vec4 outColor;
uniform sampler2D screenTexture;
uniform sampler2D depthTexture;
void main()
{
vec4 textureColour = texture(screenTexture, textureCoords);
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
You can't sample the destination texture in the shader.
What you can do is make a new texture and make that the destination texture for the previous step and use it as a normal texture in the current step.
Related
Hello I am trying to render texture on two triangles. glGetError() does not return any error. I can render colored rectangle, but texture does not work. Shaders are compiled without errors. Why this code does not work? (Image)
class MainRenderer implements GLSurfaceView.Renderer {
int vertexBuffer;
int indexBuffer;
int shaderProgram;
int texture;
#Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glClearColor(1, 0, 0, 1);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
texture = textures[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, BitmapFactory.decodeResource(MainActivity.MainActivityHandle.getResources(), R.drawable.pes), 0);
float vertices[] = {
1, 1,
1, 0,
0, 0,
0, 1
};
int indices[] = {
0, 1, 3,
1, 2, 3
};
String vertexShaderCode = "attribute vec2 position; vec2 texturePosition; void main(){texturePosition = position;gl_Position = vec4(position,0.0, 1.0);}";
String fragmentShaderCode = "precision mediump float;uniform sampler2D texture; vec2 texturePosition; void main(){gl_FragColor = texture2D(texture, texturePosition);}";
int vertexBuffers[] = new int[1];
GLES20.glGenBuffers(1, vertexBuffers, 0);
vertexBuffer = vertexBuffers[0];
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, 4 * 2 * 4, FloatBuffer.wrap(vertices), GLES20.GL_STATIC_DRAW);
int indexBuffers[] = new int[1];
GLES20.glGenBuffers(1, indexBuffers, 0);
indexBuffer = indexBuffers[0];
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 2 * 3 * 4, IntBuffer.wrap(indices), GLES20.GL_STATIC_DRAW);
int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, vertexShaderCode);
GLES20.glCompileShader(vertexShader);
int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, fragmentShaderCode);
GLES20.glCompileShader(fragmentShader);
shaderProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(shaderProgram, vertexShader);
GLES20.glAttachShader(shaderProgram, fragmentShader);
GLES20.glLinkProgram(shaderProgram);
}
#Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
#Override
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(shaderProgram);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
int positionAttributeHandle = GLES20.glGetAttribLocation(shaderProgram, "position");
GLES20.glEnableVertexAttribArray(positionAttributeHandle);
GLES20.glVertexAttribPointer(positionAttributeHandle, 2, GLES20.GL_FLOAT, false, 2*4, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_INT, 0);
GLES20.glDisableVertexAttribArray(positionAttributeHandle);
}
}
I can at least see that you are using (in vertex and fragment shaders) just 'vec2 texturePosition', while you should use a 'varying' for output from vertex shader to the fragment shader. Try adding 'varying' before 'vec2 ...' in shaders.
Why my code OpenGL android did't draw a triangle,I don't know where is the problem why forever render black screen, Someone can help me resolve this problem?
public class OpenGL implements GLSurfaceView.Renderer {
int width,height;
float blue = 0.f;
#Override
public void onDrawFrame(GL10 gl){
gl.glClearColor(.0f,.0f,blue,0.f);
gl.glClear(gl.GL_COLOR_BUFFER_BIT);
float Vertex[] = {
0.f, 1.f,
1.f, -1.f,
-1.f, -1.f,
};
ByteBuffer buffer;
FloatBuffer _float;
buffer = ByteBuffer.allocateDirect( Vertex.length * 4 );
buffer.order( ByteOrder.nativeOrder() );
_float = buffer.asFloatBuffer();
_float.put( Vertex );
_float.position(0);
gl.glColor4f(1.f,1.f,0.f,1.f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _float);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, Vertex.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height){
this.width = width;
this.height = height;
gl.glViewport(0,0,width,height);
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0,width,0,height,0,1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
Okay i have a orthographic matrix (left = 0, right = 800, bottom = 0, top = 600, near = -1.0f, far = 1.0f).How to scale the object like for half size 400 width and 300 height.When i scale it it only scales with 0.5f for half size not 400 or 300.
MainGame java
package com.engine.base.test;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.glu.GLU;
import com.engine.base.game.Game;
import com.engine.base.game.GameObject;
import com.engine.base.maths.Vector3f;
import com.engine.base.utils.Console;
public class MainGame extends Game{
public MainGame(int width, int height, String title) {
super(width, height, title);
}
#Override
protected void updateGame() {
Console.printLog(GLU.gluErrorString(glGetError()));
}
#Override
public void init() {
GameObject object = new GameObject();
object.getTransformation().Scale2f(400, 300); //Here it only scales from 0.0f to 1.0f
object.getTransformation().Rotate1f(150);
//object.getTool().setColor(new Vector3f(0.2f, 1f, 0.5f));
//object.setAmbientLight(new Vector3f(0.2f, 0.2f, 0.2f));
getLevel().addObject(object);
}
#Override
public void destroy(){
}
}
Rectangle java (here is where i write the vertecies and the indices)
package com.engine.base.graphicComponents;
import com.engine.base.graphics.Mesh;
import com.engine.base.graphics.Vertex;
import com.engine.base.maths.Vector3f;
public class Rectangle extends Shape{
public Rectangle(float x, float y, float width, float height, Vector3f color)
{
super(x, y,color);
}
public Rectangle(float x, float y, float width, float height)
{
super(x, y, new Vector3f(1, 1, 1));
}
public Mesh getMesh(){
Vertex[] vertecies = new Vertex[]{
new Vertex(new Vector3f(800, 600, 0)),
new Vertex(new Vector3f(0, 600, 0)),
new Vertex(new Vector3f(800, -0, 0)),
new Vertex(new Vector3f(0, 0, 0))
};
int[] indices = new int[]{
3, 1, 0,
3, 2, 0
};
return new Mesh(vertecies, indices);
}
}
Transformation java
package com.engine.base.game;
import org.lwjgl.util.vector.Vector2f;
import com.engine.base.maths.Matrix4f;
public class Transformation {
private Vector2f pos, scale;
private float angle = -1;
public Transformation(){
pos = new Vector2f(0, 0);
scale = new Vector2f(1, 1);
angle = 0;
}
public Matrix4f getWorldTransformation(){
return new Matrix4f().initOrthographic(0, 800, 0, 600, -1.0f, 1.0f).mul(new Matrix4f().initTranslation(pos.getX(), pos.getY(), 0).mul(new Matrix4f().initRotation(angle).mul(new Matrix4f().initScale(scale.getX(), scale.getY(), 0))));
}
public void Rotate1f(float angle){
this.angle = angle;
}
public void Scale2f(float x, float y){
scale = new Vector2f(x, y);
}
public void Translate2f(float x, float y){
pos = new Vector2f(x, y);
}
public Vector2f getPos() {
return pos;
}
public void setPos(Vector2f pos) {
this.pos = pos;
}
public Vector2f getScale() {
return scale;
}
public void setScale(Vector2f scale) {
this.scale = scale;
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
}
I am trying to select a specific region of a texture in LWJGL during the drawing process, but my efforts have been fruitless. How (if possible) can I select only a region of a whole texture during drawing.
Please note that I am nowhere new to Java, I am an experienced programmer, but not when it comes to LWGJL, as I'm still getting the full grasps with it.
Below is my code.
Sprite.java
public class Sprite
{
private Icon icon;
private int width;
private int height;
private int offcutX; // all below and this for selecting the region of the image.
private int offcutY;
private int imageX;
private int imageY;
public Sprite(TextureRegistry registry, String ref)
{
this(registry, ref, 0, 0, 0, 0);
}
public Sprite (TextureRegistry registry, String ref, int offcutX, int offcutY, int imageX, int imageY)
{
try
{
icon = registry.getIcon("net/blockbuster/resources/" + ref); // loads the icon and converts it to opengl-readable format.
width = icon.getIconWidth();
height = icon.getIconHeight();
this.offcutX = offcutX;
this.offcutY = offcutY;
this.imageX = imageX;
this.imageY = imageY;
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
public int getWidth()
{
return icon.getIconWidth();
}
public int getHeight()
{
return icon.getIconHeight();
}
public void draw(int x, int y) // drawing method, called on every loop.
{
glPushMatrix();
icon.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
{ // I would like to be able to do this within this block.
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
}
glEnd();
glPopMatrix();
}
}
TextureRegistry.java
public class TextureRegistry
{
private HashMap<String, Icon> table = new HashMap<String, Icon>(); // map to hold all loaded textures
public Icon getIcon(String resourceName) throws IOException
{
Icon tex = table.get(resourceName);
if (tex != null)
{
return tex;
}
tex = getIcon(resourceName, GL_TEXTURE_2D, GL_RGBA8, GL_LINEAR, GL_LINEAR);
table.put(resourceName, tex);
return tex;
}
public Icon getIcon(String resourceName, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException
{
BufferedImage image = loadImage(resourceName);
int textureID = loadTexture(image);
Icon icon = new Icon(target, textureID);
icon.setWidth(image.getWidth());
icon.setHeight(image.getHeight());
return icon;
}
public static int loadTexture(BufferedImage image)
{
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte)((pixel >> 16) & 0xFF)); // r
buffer.put((byte)((pixel >> 8) & 0xFF)); // g
buffer.put((byte)(pixel * 0xFF)); // b
buffer.put((byte)((pixel >> 24) & 0xFF)); // a
}
}
buffer.flip();
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
return textureID;
}
public static BufferedImage loadImage(String ref) throws IOException
{
URL url = TextureRegistry.class.getClassLoader().getResource(ref);
if (url == null)
{
throw new IOException("Cannot find: " + ref);
}
Image img = new ImageIcon(url).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
}
Icon.java
public class Icon
{
private int target;
private int iconID;
private int width;
private int height;
private int iconWidth;
private int iconHeight;
private float widthRatio;
private float heightRatio;
public Icon(int target, int iconID)
{
this.target = target;
this.iconID = iconID;
}
public void bind()
{
glBindTexture(target, iconID); // binds the texture
}
public void setHeight(int newHeight)
{
this.height = newHeight;
setHeight();
}
public void setWidth(int newWidth)
{
this.width = newWidth;
setWidth();
}
public int getIconHeight()
{
return height;
}
public int getIconWidth()
{
return width;
}
public float getHeight()
{
return heightRatio;
}
public float getWidth()
{
return widthRatio;
}
public void setIconHeight(int iconHeight)
{
this.iconHeight = iconHeight;
setHeight();
}
public void setIconWidth(int iconWidth)
{
this.iconWidth = iconWidth;
setWidth();
}
private void setHeight()
{
if (iconHeight != 0)
{
heightRatio = ((float)height) / iconHeight;
}
}
private void setWidth()
{
if (iconWidth != 0)
{
widthRatio = ((float)width) / iconWidth;
}
}
}
To specify a region of a texture to be drawn all you need to do is modify your texCoords. Currently you are using your entire texture, if for example you wanted only the quarter of it closest to the origin you would use this in your draw method:
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1/2f);
glVertex2f(0, height);
glTexCoord2f(1/2f, 1/2f);
glVertex2f(width, height);
glTexCoord2f(1/2f, 0);
glVertex2f(width, 0);
which would result in an image the same size as before although using only the bottom left quarter of the texture.
I'm new to OpenGL and I'm teaching myself by making a 2D game for Android with ES 2.0. I am starting off by creating a "Sprite" class that creates a plane and renders a texture to it. To practice, I have two Sprite objects that are drawn alternating in the same place. I got this much working fine and well with ES 1.0, but now that I've switched to 2.0, I am getting a black screen with no errors. I'm exhausted trying to figure out what I'm doing wrong, but I have a strong feeling it has to do with my shaders. I'm going to dump all the relevant code here and hopefully somebody can give me an answer or some advice as to what I'm doing wrong. And if it's not immediately apparent what I'm doing wrong, perhaps some advice on how to figure it out? Thanks in advance for looking through all the code I'm about to post.
The three classes I'm posting are:
GameRenderer - the renderer for my GLSurfaceView
Shader - creates a shader program object
Sprite - creates a square and draws a texture on it
Also, I'll post my vertex and fragment shader source.
Related classes I didn't think were relevant enough to post:
GameActivity
GameView - A GLSurfaceView
GameLoopThread - My main game loop
FPSCounter - outputs the average FPS to logcat every 100 frames.
GameRender class:
package com.detour.raw;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLU;
import android.opengl.Matrix;
import android.opengl.GLSurfaceView;
public class GameRenderer implements GLSurfaceView.Renderer{
private static final String LOG_TAG = GameRenderer.class.getSimpleName();
Context mContext;
Bitmap bitmap;
private float red = 0.0f;
private float green = 0.0f;
private float blue = 0.0f;
Shader shader;
FPSCounter fps;
Sprite sprite;
Sprite sprite2;
int x = 0;
private float[] mProjMatrix = new float[16];
private float[] mVMatrix = new float[16];
//int[] vertexShader;
//int[] fragmentShader;
//int program;
//String vShaderSource = "";
//String fShaderSource = "";
public GameRenderer(Context context){
mContext = context;
//create objects/sprites
sprite = new Sprite(mContext);
sprite2 = new Sprite(mContext);
fps = new FPSCounter();
}
#Override
public void onDrawFrame(GL10 gl) {
GLES20.glClearColor(red, green, blue, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
if(x>3){
x=0;
}
if(x%2==0){
sprite.draw(gl);
}else{
sprite2.draw(gl);
}
x++;
fps.calculate();
//fps.draw(gl);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float)(width/height);
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 0.5f, 10);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glClearDepthf(1.0f);
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
GLES20.glDepthMask(true);
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glCullFace(GLES20.GL_BACK);
GLES20.glClearColor(red, green, blue, 1.0f);
//load sprite/object textures (preferably loop through an array of all sprites).
sprite.loadGLTexture(gl, mContext, R.drawable.raw1);
sprite2.loadGLTexture(gl, mContext, R.drawable.raw2);
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5.0f, 0.0f, 0f, 0f, 0f, 0.0f, 0.0f);
System.gc();
}
}
Shader class:
package com.detour.raw;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;
public class Shader {
public static final String TAG = Shader.class.getSimpleName();
int program;
int vertexShader;
int fragmentShader;
String vShaderSource;
String fShaderSource;
public Shader(){
//blank constructor
//createProgram();
}
public Shader(String vs_source, String fs_source){
this.vShaderSource = vs_source;
this.fShaderSource = fs_source;
createProgram();
}
public Shader(int vs_source_id, int fs_source_id, Context context) {
StringBuffer vs = new StringBuffer();
StringBuffer fs = new StringBuffer();
try{
InputStream inputStream = context.getResources().openRawResource(vs_source_id);
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String read = in.readLine();
while (read != null) {
vs.append(read + "\n");
read = in.readLine();
}
vs.deleteCharAt(vs.length() - 1);
inputStream = context.getResources().openRawResource(fs_source_id);
in = new BufferedReader(new InputStreamReader(inputStream));
read = in.readLine();
while (read != null) {
fs.append(read + "\n");
read = in.readLine();
}
fs.deleteCharAt(fs.length() - 1);
}catch (Exception e){
Log.d("ERROR-readingShader", "Could not read shader: " + e.getLocalizedMessage());
}
this.vShaderSource = vs.toString();
this.fShaderSource = fs.toString();
createProgram();
}
private void createProgram(){
program = GLES20.glCreateProgram();
if(program!=0){
vertexShader = createShader(GLES20.GL_VERTEX_SHADER, vShaderSource);
fragmentShader = createShader(GLES20.GL_FRAGMENT_SHADER, fShaderSource);
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
GLES20.glLinkProgram(program);
}else{
Log.e(TAG, "Couldn't create program.");
}
}
private int createShader(int type, String source){
int shader = GLES20.glCreateShader(type);
if(shader!=0){
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
}
return shader;
}
public int getProgram(){
return program;
}
Sprite class:
package com.detour.raw;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
public class Sprite {
//public static final int FRAME_WIDTH = 64;
//public static final int FRAME_HEIGHT = 64;
private static final String LOG_TAG = Sprite.class.getSimpleName();
Context mContext;
Bitmap bitmap;
private int textureLoc;
private int vertexLoc;
private int[] textures = new int[1];
//private int[] pixels;
/*private float textureCoordinates[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f};*/
private float vertices[] = {
-1.0f, 1.0f,// 0.0f,
-1.0f, -1.0f,// 0.0f,
1.0f, -1.0f,// 0.0f,
1.0f, 1.0f// 0.0f
};
private short[] indices = {
0, 1, 2,
0, 2, 3};
private FloatBuffer vertexBuffer;
//private IntBuffer textureBuffer;
private ShortBuffer indexBuffer;
Shader shader;
int program;
String vShaderSource = "";
String fShaderSource = "";
public Sprite(Context context){
mContext = context;
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl) {
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_FLOAT, indexBuffer);
}
public void loadGLTexture(GL10 gl, Context context, int id){
shader = new Shader(R.raw.sprite_vs, R.raw.sprite_fs, mContext);
program = shader.getProgram();
GLES20.glUseProgram(program);
vertexLoc = GLES20.glGetAttribLocation(program, "a_position");
textureLoc = GLES20.glGetUniformLocation(program, "u_texture"); //texture
InputStream is = context.getResources().openRawResource(id);
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
}
}
//pixels = new int[(bitmap.getWidth()*bitmap.getHeight())];
//bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
/*ByteBuffer byteBuf = ByteBuffer.allocateDirect(pixels.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asIntBuffer();
textureBuffer.put(pixels);
textureBuffer.position(0);*/
GLES20.glDeleteTextures(1, textures, 0);
GLES20.glGenTextures(1, textures, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glUniform1i(textureLoc, 0);
GLES20.glEnableVertexAttribArray(vertexLoc);
GLES20.glVertexAttribPointer(vertexLoc, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
//GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, FRAME_WIDTH, FRAME_HEIGHT, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, byteBuf);//(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
Vertex shader (sprite_vs.txt):
#version 110
attribute vec2 a_position;
varying vec2 v_texcoord;
void main()
{
gl_Position = vec4(a_position, 0.0, 1.0);
v_texcoord = a_position * vec2(0.5) + vec2(0.5);
}
Fragment (pixel) shader (sprite_fs.txt):
#version 110
uniform sampler2D u_texture;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = texture2D(u_texture, v_texcoord);
}
Thank you so much if you actually took the time to look through this! Hopefully someone else can use this as a resource for themselves in the future, also.
A few observations/questions:
I don't know how you changed the fragment shader, but the version that is currently posted needs a precision specifier. Just add:
precision mediump float;
to the top, and it should work. Now regarding the black screen here are some questions:
When you change the glClearColor to something not black and comment out all the draw commands, does it still look black? If so, then you have a bigger problem than textures.
Second, if you ignore the texture output and try drawing each sprite as just a flat colored rectangle with no texture data, what do you get? You should be able to see some colored rectangle on the screen.
Finally, you need to bind the texture before you call glDrawElements. (Though this shouldn't matter in this example since you haven't changed the state yet.)