OpenGL 3.2 Texture Not Displaying - java

I'm trying to create a class to speed up the production of textured polygons in my program but the texture is not displaying.
I'm using LWJGL with Slick2D for the texture loading.
This is my TexturedPolygon class:
package zeus.core.geom;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import zeus.core.Disposable;
public class TexturedPolygon extends Disposable {
private final List<Float> vertices = new ArrayList<Float>();
private final List<Float> texCoords = new ArrayList<Float>();
private final int VAO, VBO;
private final FloatBuffer vertBuffer;
private final ByteBuffer texBuffer;
private final int GL_DRAWING_MODE;
private final String IMG_FILE;
private final Texture TEXTURE;
public TexturedPolygon(final LinkedHashMap<Vector2f, Vector2f> info, final String IMG_FILE, final int GL_DRAWING_MODE) throws IOException {
this.IMG_FILE = IMG_FILE;
this.GL_DRAWING_MODE = GL_DRAWING_MODE;
this.TEXTURE = TextureLoader.getTexture(".png", new FileInputStream(new File(System.getProperty("user.dir") + "/res/img/", IMG_FILE)));
for(Map.Entry<Vector2f, Vector2f> entry : info.entrySet()) {
vertices.add(entry.getKey().x);
vertices.add(entry.getKey().y);
texCoords.add(entry.getValue().x);
texCoords.add(entry.getValue().y);
}
vertBuffer = BufferUtils.createFloatBuffer(vertices.size());
final float[] vertArray = new float[vertices.size()];
int i = 0;
for(final float f : vertices) {
vertArray[i++] = f;
}
vertBuffer.put(vertArray);
vertBuffer.flip();
VAO = GL30.glGenVertexArrays();
GL30.glBindVertexArray(VAO);
VBO = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(0);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL30.glBindVertexArray(VAO);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, TEXTURE.getTextureID());
texBuffer = BufferUtils.createByteBuffer(TEXTURE.getTextureData().length);
texBuffer.put(TEXTURE.getTextureData());
texBuffer.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, TEXTURE.getTextureID());
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(1);
}
public void draw() {
GL30.glBindVertexArray(VAO);
GL11.glDrawArrays(GL_DRAWING_MODE, 0, vertices.size());
GL30.glBindVertexArray(0);
}
public void update(final int delta) {
}
#Override
public void dispose() {
GL20.glDisableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(VBO);
GL30.glBindVertexArray(0);
GL30.glDeleteVertexArrays(VAO);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, TEXTURE.getTextureID());
}
public int getVAO() {
return VAO;
}
public int getVBO() {
return VBO;
}
public int getDrawingMode() {
return GL_DRAWING_MODE;
}
public FloatBuffer getVertBuffer() {
return vertBuffer;
}
public ByteBuffer getTextureBuffer() {
return texBuffer;
}
public List<Float> getVertices() {
return vertices;
}
public List<Float> getTexCoords() {
return texCoords;
}
public String getImageFilename() {
return IMG_FILE;
}
}
This is my main class:
package tests;
import java.util.LinkedHashMap;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.vector.Vector2f;
import zeus.core.Window;
import zeus.core.geom.TexturedPolygon;
import zeus.core.opengl.Shader;
import zeus.core.opengl.ShaderProgram;
public class WindowTest {
public static void main(String[] args) throws Exception {
final Window win = new Window(800, 600, "Window Test");
win.create();
Shader vertexShader = new Shader("drawing_test_vertex", GL20.GL_VERTEX_SHADER);
Shader fragmentShader = new Shader("drawing_test_fragment", GL20.GL_FRAGMENT_SHADER);
ShaderProgram program = new ShaderProgram();
vertexShader.compile();
fragmentShader.compile();
LinkedHashMap<Vector2f, Vector2f> info = new LinkedHashMap<Vector2f, Vector2f>();
info.put(new Vector2f(0f, 0f), new Vector2f(0f, 0f));
info.put(new Vector2f(1f, 0f), new Vector2f(1f, 0f));
info.put(new Vector2f(1f, 1f), new Vector2f(1f, 1f));
info.put(new Vector2f(1f, 1f), new Vector2f(1f, 1f));
info.put(new Vector2f(0f, 1f), new Vector2f(0f, 1f));
info.put(new Vector2f(0f, 0f), new Vector2f(0f, 0f));
TexturedPolygon p = new TexturedPolygon(info, "test.png", GL11.GL_TRIANGLES);
program.addShader(vertexShader);
program.addShader(fragmentShader);
program.link();
program.use();
while(!win.isCloseRequested()) {
win.clear();
p.update(1);
p.draw();
win.update(120);
}
p.dispose();
vertexShader.dispose();
fragmentShader.dispose();
program.dispose();
win.dispose();
}
}
And these are my shader files:
Vertex:
#version 410
layout(location=0) vec3 pos;
layout(location=1) vec2 tex;
out smooth vec2 Tex;
void main() {
gl_Position = vec4(pos, 1.0f);
Tex = tex;
}
Fragment:
#version 410
layout(location=0) vec2 Tex;
uniform sampler2D textureDiffuse;
out vec4 color;
void main() {
color = texture(textureDiffuse, Tex);
}
I have no idea what is causing the problem.
This is what is currently displaying:
The colors here change when I change the 'size' argument between 1 and 4 on this line:
GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0);

I haven't checked all of your code, but your shaders are broken. You aren't declaring correct in variables, so you bascially use uninitialized data. Do those shaders even compile with layout qualifiers for global variables?
The vertex shader should be changed to:
layout(location=0) in vec3 pos;
layout(location=1) in vec2 tex;
and analogously for the fragment shader:
in vec2 Tex;
Note that locations are only relevant for cases where is some host <-> GL communication (like uniforms, vertex attributes and fragment shader outputs), but there are no locations for shader in- and outputs which are internal to the GL pipeline, so you cannot declare a location for fragment shader inputs. Those variables are matched by the names.

Related

rendering 3d lwjgl flat terrain doesn't work

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL15.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
public class man {
private static final int VERTEX_COUNT = 128;
private static final float SIZE = 1;
public static int count;
public static int vid;
public static int iid;
public static int program;
public static float fov = 70f;
public static float near = 0.1f;
public static float far = 1000f;
public static Matrix4f modelMatrix = new Matrix4f() ;
public static Matrix4f view = new Matrix4f();
public static Matrix4f projectionmatrix = new Matrix4f();
public static GLFWCursorPosCallback mouseCallback;
public static boolean t = true;
public static void main(String[] argv) throws IOException {
glfwInit();
int prog = 0 , id=0;
long window = glfwCreateWindow(1920,
1080, "HI", 0 , 0);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
// glfwSetCursorPosCallback(window, mouseCallback = new mouse());
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
new man().createShader();
new man().bind();
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glLoadIdentity();
glEnable(GL_PROJECTION_MATRIX);
glEnable(GL_PROJECTION);
new man().createprojection();
new man().createview();
glUseProgram(program);
new man().loadtoshader();
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vid);
glVertexPointer(3, GL_FLOAT , 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iid);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisable(GL_VERTEX_ARRAY);
}
}
public void createShader() throws IOException
{
StringBuilder vertex = new StringBuilder();
StringBuilder frag = new StringBuilder();
BufferedReader vert ,fragment;
vert = new BufferedReader(new FileReader("D:/work/opengl2/src/opengl2/vertexShader.txt"));
fragment = new BufferedReader(new FileReader("D:/work/opengl2/src/opengl2/frageShader.txt"));
//vertex Shader
String line, line2;
while ( (line = vert.readLine()) != null)
{
vertex.append(line).append('\n');
}
while ( (line2 = fragment.readLine()) != null)
{
frag.append(line2).append('\n');
}
//create and comile shaders
int vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glShaderSource(vertexShader, vertex);
GL20.glCompileShader(vertexShader);
int fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(fragmentShader, frag);
GL20.glCompileShader(fragmentShader);
program = GL20.glCreateProgram();
GL20.glAttachShader(program, vertexShader);
GL20.glAttachShader(program, fragmentShader);
GL20.glBindAttribLocation(program, 0 , "postion");
GL20.glLinkProgram(program);
if (GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) != 1)
{
System.err.println(GL20.glGetProgramInfoLog(program));
System.exit(1);
}
GL20.glValidateProgram(program);
}
public void bind()
{
float[] vertices = new float[128 * 128 * 3];
int[] indices = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)];
//generaete terrain
int vertexPointer = 0;
for(int i=0;i<VERTEX_COUNT;i++){
for(int j=0;j<VERTEX_COUNT;j++){
vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * SIZE;
vertices[vertexPointer*3+1] = 0;
vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
vertexPointer++;
}
}
int pointer = 0;
for(int gz=0;gz<VERTEX_COUNT-1;gz++){
for(int gx=0;gx<VERTEX_COUNT-1;gx++){
int topLeft = (gz*VERTEX_COUNT)+gx;
int topRight = topLeft + 1;
int bottomLeft = ((gz+1)*VERTEX_COUNT)+gx;
int bottomRight = bottomLeft + 1;
indices[pointer++] = topLeft;
indices[pointer++] = bottomLeft;
indices[pointer++] = topRight;
indices[pointer++] = topRight;
indices[pointer++] = bottomLeft;
indices[pointer++] = bottomRight;
}
}
//end generate terrain
FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
buffer.put(vertices);
buffer.flip();
count = indices.length ;
vid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vid);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
iid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, iid);
IntBuffer indbuf = BufferUtils.createIntBuffer(indices.length);
indbuf.put(indices);
indbuf.flip();
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indbuf, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
public void createprojection()
{
float aspect = (float) 1920/1080;
float y_scale = (float) (1/Math.tan(Math.toRadians(fov/2f)));
float x_scale = y_scale / aspect;
float frustum_length = far - near;
projectionmatrix.m00 = x_scale;
projectionmatrix.m11 = y_scale;
projectionmatrix.m22 = -((far + near) / frustum_length);
projectionmatrix.m23 = -1;
projectionmatrix.m32 = -((2 * near * far) / frustum_length);
projectionmatrix.m33 = 0;
}
public void createview()
{
Vector3f modelPos = null;
Vector3f modelAngle = null;
Vector3f modelScale = null;
Vector3f camera = new Vector3f(0,1f,-2f);
modelPos = new Vector3f(0,0,0);
modelAngle = new Vector3f(0,0,0);
modelScale = new Vector3f(1, 1, 1);
modelMatrix.setIdentity();
Matrix4f.scale(modelScale, modelMatrix, modelMatrix);
Matrix4f.translate(modelPos, modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.z), new Vector3f(0, 0, 1),
modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.y), new Vector3f(0, 1, 0),
modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.x), new Vector3f(1, 0, 0),
modelMatrix, modelMatrix);
Matrix4f.translate(camera, view, view);
}
public void loadtoshader()
{
int loc1 = glGetUniformLocation(program, "projection");
FloatBuffer matrixx = BufferUtils.createFloatBuffer(16);
projectionmatrix.store(matrixx);
matrixx.flip();
glUniformMatrix4fv(loc1, false,matrixx);
int loc2 = glGetUniformLocation(program, "view");
FloatBuffer matrixx2 = BufferUtils.createFloatBuffer(16);
view.store(matrixx2);
matrixx2.flip();
glUniformMatrix4fv(loc2, false,matrixx2);
int loc3 = glGetUniformLocation(program, "model");
FloatBuffer matrixx3 = BufferUtils.createFloatBuffer(16);
modelMatrix.store(matrixx3);
matrixx3.flip();
glUniformMatrix4fv(loc3, false,matrixx3);
}
}
class mouse extends GLFWCursorPosCallback
{
#Override
public void invoke(long arg0, double x, double y) {
// TODO Auto-generated method stub
System.out.println(x+ " "+ y );
}
}
Hi guys !
I want to generate a simple flat terrain in lwjgl but this code doesn't produce me anything. I am very new in this chapter so if you can explain why this code does nothing please do it! I am generating the terrain with the code shared by ThinMatrix , then i upload the vertices to the buffers and then render them in the main game loop . When i compile it , it shows me a black screen. I have searched for a lot of tutorials but I didn't find anything that can help me .
I see several issues with your code:
Issue Number 1: You are generating a new instance of the man class on every iteration of the loop. Create a single instance of the object outside of the loop and use it in the loop.
Issue Number 2: You are using the old, static pipeline OpenGL. Following ThinMatrix's tutorials, you should be using OpenGL 3 or newer. Everything should be going through shaders, rather than using the GL_PROJECTION_MATRIX and the likes of that.
Your program should look something like the following:
man instance = new man()
while (!glfwWindowShouldClose(window))
{
RENDER THE TERRAIN HERE
glfwPoll
glfwSwapBuffers
glClear
}
If you want some example code, I have a repository here that should help, as I used to watch ThinMatrix tutorials.
EDIT: SOLUTION
The vertex shader code should read:
gl_Position = projection * view * model * position;
My shaders: fragment
#version 330
in vec4 pos;
void main()
{
gl_FragColor = vec4 (pos);
}
vertex:
#version 330
attribute vec3 postion;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
out vec4 pos;
void main()
{
gl_Position = vec4(postion, 1.0) * projection * view * model;
pos = vec4(postion, 1.0) ;
}
I have updated my java code so it renders different colors base on the vertex position.

OpenGL 4.3 Java Not Rendering in Window

I'm following the OpenGL 3D Game Tutorial series by ThinMatrix and everything seems to work fine except nothing appears in the window. I got to the 5th video.
My Loader Class
package org.voxsim;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Loader {
private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();
public Models loadToVao(float[] pos, int[] ind) {
int vaoId = createVao();
bindIndBuffer(ind);
storeData(0,pos);
unbind();
return new Models(vaoId, ind.length);
}
public void cleanUp() {
for (int vao:vaos) {
GL30.glDeleteVertexArrays(vao);
}
for (int vbo:vbos) {
GL15.glDeleteBuffers(vbo);
}
}
private int createVao() {
int VaoId = GL30.glGenVertexArrays();
vaos.add(VaoId);
GL30.glBindVertexArray(VaoId);
return VaoId;
}
private void storeData(int num, float[] data) {
int VboId = GL15.glGenBuffers();
vbos.add(VboId);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VboId);
FloatBuffer buffer = storeInFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(num,3,GL11.GL_FLOAT,false,0,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbind() {
GL30.glBindVertexArray(0);
}
private void bindIndBuffer(int[] ind) {
int vboId = GL15.glGenBuffers();
vbos.add(vboId);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboId);
IntBuffer buffer = storeInIntBuffer(ind);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}
private IntBuffer storeInIntBuffer(int[] data) {
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
private FloatBuffer storeInFloatBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
}
My Render Class
package org.voxsim;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Render {
public void render(Models model) {
GL11.glColor4f(0.5f, 0.9f, 0.2f, 1.0f);
GL30.glBindVertexArray(model.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
}
My Model Class
package org.voxsim;
public class Models {
private int vaoID;
private int vaoNum;
public Models(int vaoID, int vaoNum) {
this.vaoID = vaoID;
this.vaoNum = vaoNum;
}
public int getVaoID() {
return vaoID;
}
public int getVertexCount() {
return vaoNum;
}
}
My Main Class
package org.voxsim;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.GL_FALSE;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLCapabilities;
import org.voxsim.game.Block;
import org.voxsim.shaders.Shader;
import org.voxsim.shaders.Static;
public class Game {
static public int width = 600;
static public int height = 400;
static public int vsync = 1;
static public String title = "VoxSim";
static private Loader loader = new Loader();
static private Render render = new Render();
private static void loop() {
Shader shader = new Static();
float[] v = {
-0.5f,0.5f,0,
-0.5f,-0.5f,0,
0.5f,-0.5f,0,
0.5f,0.5f,0
};
int[] ind = {
0,1,3,
3,1,2
};
Models model = loader.loadToVao(v, ind);
while (glfwWindowShouldClose(DisplayManager.windowID) == GL_FALSE) {
DisplayManager.updateDisplay();
shader.start();
render.render(model);
shader.stop();
}
shader.cleanUp();
}
public static void main(String[] args) {
DisplayManager.createDisplay();
DisplayManager.updateDisplay();
loop();
loader.cleanUp();
}
}
I followed the tutorial exactly but I don't know what's wrong. I am running an intel 4th gen i3 with intel hd graphics 4400 with 4GB of ram on windows 10 with
OpenGL Version 4.3,
OpenCL Version 1.2, and
Shader Model Version 5

LibGDX depth test error

I am doing some basic code in LibGDX and want to render a grid of green spheres, but the depth testing which I set to LEQUAL has an error. The error is it seems to render them back to front like GEQUAL.
Here's a screenshot - https://www.imageupload.co.uk/image/5ot2.
Here's the code -
MyGdxGame.java
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.utils.Array;
public class MyGdxGame extends ApplicationAdapter {
Environment env;
PerspectiveCamera camera;
CameraInputController camController;
ModelBatch modelBatch;
Array<ModelInstance> instances;
CustomShader shader;
#Override
public void create () {
env = new Environment();
env.set(new ColorAttribute(ColorAttribute.AmbientLight,
0.5f, 0.5f, 0.5f, 1.0f));
env.add(new DirectionalLight().set(0.7f, 0.7f, 0.7f,
1.0f, 1.0f, 1.0f));
camera = new PerspectiveCamera(60.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(0.0f, 5.0f, 0.0f);
camera.lookAt(10.0f, 5.0f, 10.0f);
camera.near = 0.0f;
camera.far = 300.0f;
camera.update();
camController = new CameraInputController(camera);
Gdx.input.setInputProcessor(camController);
modelBatch = new ModelBatch();
instances = new Array<ModelInstance>();
ModelBuilder mb = new ModelBuilder();
for(float x = 0.0f; x < 100.0f; x += 10.0f) {
for(float z = 0.0f; z < 100.0f; z += 10.0f) {
Model model = mb.createSphere(5.0f, 5.0f, 5.0f, 20, 20,
new Material(),
Usage.Position | Usage.Normal | Usage.TextureCoordinates);
ModelInstance instance = new ModelInstance(model);
instance.transform.setTranslation(x, 0.0f, z);
instances.add(instance);
}
}
shader = new CustomShader();
shader.init();
}
#Override
public void dispose () {
instances.clear();
}
#Override
public void render () {
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(camera);
for(ModelInstance instance : instances) {
modelBatch.render(instance, shader);
}
modelBatch.end();
}
}
CustomShader.java
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class CustomShader implements Shader {
ShaderProgram program;
int u_worldTrans, u_projViewTrans;
#Override
public void dispose() {
program.dispose();
}
#Override
public void init() {
String vert = Gdx.files.internal("shaders/custom.vertex.glsl").readString();
String frag = Gdx.files.internal("shaders/custom.fragment.glsl").readString();
program = new ShaderProgram(vert, frag);
if(!program.isCompiled()) {
throw new GdxRuntimeException(program.getLog());
}
// Cache uniform positions
u_worldTrans = program.getUniformLocation("u_worldTrans");
u_projViewTrans = program.getUniformLocation("u_projViewTrans");
}
#Override
public int compareTo(Shader other) {
return 0;
}
#Override
public boolean canRender(Renderable instance) {
return true;
}
#Override
public void begin(Camera camera, RenderContext context) {
program.begin();
program.setUniformMatrix(u_projViewTrans, camera.combined);
context.setDepthTest(GL20.GL_LEQUAL);//not working/displaying GEQUAL instead?
context.setCullFace(GL20.GL_BACK);
}
#Override
public void render(Renderable renderable) {
program.setUniformMatrix(u_worldTrans, renderable.worldTransform);
renderable.mesh.render(program,
renderable.primitiveType,
renderable.meshPartOffset,
renderable.meshPartSize);
}
#Override
public void end() {
program.end();
}
}
custom.vertex.glsl
uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;
attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;
varying vec2 v_texCoord0;
void main() {
v_texCoord0 = a_texCoord0;
gl_Position = u_projViewTrans * u_worldTrans * vec4(a_position, 1.0);
}
custom.fragment.glsl
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord0;
void main() {
//gl_FragColor = vec4(v_texCoord0, 0.0f, 1.0f);
gl_FragColor = vec4(0.0f, (0.5f*v_texCoord0.x)/v_texCoord0.y, 0.0f, 1.0f);
}

OpenGL ES 2.0: Vertex Shader compile status result is 0 with no Shader Log Info

Current situation doesn't allow me to use a computer. And it will be like this for a while. I use Android IDE (AIDE) to program on my phone.
In my code, I used glGetShaderiv() to get the compile status, and noticed the status value is 0. However, there is no indication to suggest the vertex shader code contains an error or something is wrong with loading the GLSL code from a text file.
Below are the codes. Note that I jumbled all the codes together so that the execution of the code is as iterative as possible. Meaning the code doesn't jump around a lot using function calls, for easier debugging.
RenderView class:
package p.e;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import static android.opengl.GLES20.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.Matrix;
public class RenderView extends GLSurfaceView implements GLSurfaceView.Renderer{
private Context context;
private Table table;
private int texture;
private final float[] projectionMatrix=new float[16];
private final float[] modelMatrix=new float[16];
private Shader shader;
public RenderView(Context context){
super(context);
this.context=context;
}
#Override
public void onSurfaceCreated(GL10 p1, EGLConfig p2)
{
// TODO: Implement this method
glClearColor(1f,0f,1f,1f);
this.shader=new Shader(context);
this.table=new Table();
final int[] textureID=new int[1];
glGenTextures(1,textureID,0);
texture=textureID[0];
BitmapFactory.Options options=new BitmapFactory.Options();
options.inScaled=false;
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher, options);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
GLUtils.texImage2D(GL_TEXTURE_2D,0,bitmap,0);
glGenerateMipmap(GL_TEXTURE_2D);
bitmap.recycle();
glBindTexture(GL_TEXTURE_2D,0);
}
#Override
public void onSurfaceChanged(GL10 p1, int width, int height)
{
// TODO: Implement this method
glViewport(0,0,width,height);
perspectiveM(projectionMatrix,45f, (float)width/(float)height,1f,10f);
Matrix.setIdentityM(modelMatrix, 0);
Matrix.translateM(modelMatrix,0,0f,0f,-2.5f);
Matrix.rotateM(modelMatrix,0,-60f,1f,0f,0f);
final float[] temp=new float[16];
Matrix.multiplyMM(temp,0,projectionMatrix,0,modelMatrix,0);
System.arraycopy(temp,0,projectionMatrix,0,temp.length);
}
#Override
public void onDrawFrame(GL10 p1)
{
// TODO: Implement this method
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader.getProgram());
glUniformMatrix4fv(shader.uMatrixLocation, 1, false, projectionMatrix,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture);
glUniform1i(shader.uTextureUnitLocation,0);
table.getVertexBuffer().position(0);
glVertexAttribPointer(shader.aPositionLocation, 2, GL_FLOAT,false,2*4,table.getVertexBuffer());
//glBindBuffer(GL_ARRAY_BUFFER, table.vertexBufferPointer);
//table.getVertexBuffer().position(0);
glEnableVertexAttribArray(shader.aPositionLocation);
//table.getVertexBuffer().rewind();
table.getVertexBuffer().position(0);
//table.getTexBuffer().position(0);
//glBindBuffer(GL_ARRAY_BUFFER, table.texBufferPointer);
table.getTexBuffer().position(0);
glVertexAttribPointer(shader.aTexPositionLocaton,2,GL_FLOAT,false,2*4,table.getTexBuffer());
glEnableVertexAttribArray(shader.aTexPositionLocaton);
//table.getTexBuffer().rewind();
table.getTexBuffer().position(0);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
//glDisableVertexAttribArray(shader.aPositionLocation);
//glDisableVertexAttribArray(shader.aTexPositionLocaton);
}
public static void perspectiveM(float[] m, float yFovInDegrees, float aspect, float n, float f) {
final float angleInRadians = (float) (yFovInDegrees * Math.PI / 180.0);
final float a = (float) (1.0 / Math.tan(angleInRadians / 2.0));
m[0] = a / aspect;
m[1] = 0f;
m[2] = 0f;
m[3] = 0f;
m[4] = 0f;
m[5] = a;
m[6] = 0f;
m[7] = 0f;
m[8] = 0f;
m[9] = 0f;
m[10] = -((f + n) / (f - n));
m[11] = -1f;
m[12] = 0f;
m[13] = 0f;
m[14] = -((2f * f * n) / (f - n));
m[15] = 0f;
}
}
`
Table class:
package p.e;
import java.nio.*;
import static android.opengl.GLES20.*;
public class Table
{
private FloatBuffer vertexBuffer;
private FloatBuffer texBuffer;
public int vertexBufferPointer;
public int texBufferPointer;
private final float[] vertexData={
-0.5f,-0.5f,
0.5f,-0.5f,
0.5f,0.5f,
-0.5f,0.5f
};
private final float[] texData={
0f,1f,
1f,1f,
1f,0f,
0f,0f
};
public Table(){
int[] bufferPointer=new int[1];
glGenBuffers(1,bufferPointer,0);
vertexBuffer=ByteBuffer.allocateDirect(vertexData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(vertexData);
vertexBuffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, bufferPointer[0]);
glBufferData(GL_ARRAY_BUFFER, vertexData.length*4, vertexBuffer, GL_STATIC_DRAW);
vertexBufferPointer=bufferPointer[0];
texBuffer=ByteBuffer.allocateDirect(texData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
texBuffer.put(texData);
texBuffer.flip();
glGenBuffers(1, bufferPointer,0);
glBindBuffer(GL_ARRAY_BUFFER,bufferPointer[0]);
glBufferData(GL_ARRAY_BUFFER,texData.length*4, texBuffer, GL_STATIC_DRAW);
}
public FloatBuffer getVertexBuffer(){
return vertexBuffer;
}
public FloatBuffer getTexBuffer(){
return texBuffer;
}
}
Shader class:
package p.e;
import java.io.*;
import android.content.Context;
import static android.opengl.GLES20.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
import android.util.Log;
import java.nio.ByteOrder;
public class Shader
{
private int program;
private final String U_MATRIX="u_matrix";
private final String U_TEXTUREUNIT="u_texUnit";
private final String A_POSITION="a_position";
private final String A_TEXCOORDS="a_texPos";
public int uMatrixLocation;
public int uTextureUnitLocation;
public int aPositionLocation;
public int aTexPositionLocaton;
public Shader(Context context){
int vertex=glCreateShader(GL_VERTEX_SHADER);
IntBuffer intBuf=ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
Log.wtf("Code",Shader.loadString(context,R.raw.tex_vert));
glShaderSource(vertex,Shader.loadString(context,R.raw.tex_vert));
glCompileShader(vertex);
//check
int status;
glGetShaderiv(vertex, GL_COMPILE_STATUS, intBuf);
status=intBuf.get(0);
if(status==0){
glGetShaderiv(vertex,GL_INFO_LOG_LENGTH,intBuf);
status=intBuf.get(0);
if (status>1){
Log.i("Shader","Vertex Shader: "+glGetShaderInfoLog(vertex));
}
glDeleteShader(vertex);
Log.w("Shader","Vertex Shader error.");
return;
}
//check end
int fragment=glCreateShader(GL_FRAGMENT_SHADER);
Log.wtf("Code",Shader.loadString(context,R.raw.tex_frag));
glShaderSource(fragment, Shader.loadString(context,R.raw.tex_frag));
glCompileShader(fragment);
//check
glGetShaderiv(fragment, GL_COMPILE_STATUS, intBuf);
status=intBuf.get(0);
Log.i("Shader","Fragment Shader: "+glGetShaderInfoLog(fragment));
if(status==0){
glDeleteShader(fragment);
Log.w("Shader","Fragment Shader error.");
return;
}
//check end
program=glCreateProgram();
//check
Log.i("Shader","Program: "+glGetProgramInfoLog(program));
if(program==0){
Log.w("Shader","Program not created.");
return;
}
//check end
glAttachShader(program,vertex);
glAttachShader(program,fragment);
glLinkProgram(program);
//check
glValidateProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, intBuf);
status=intBuf.get(0);
if(status==0){
glDeleteProgram(program);
Log.w("Shader","Program unable to link.");
return;
}
//check end
//check
glGetProgramiv(program, GL_VALIDATE_STATUS, intBuf);
status=intBuf.get(0);
Log.i("Shader","Program validation: "+glGetProgramInfoLog(program));
if(status==0){
glDeleteProgram(program);
Log.w("Shader","Program validation failed.");
return;
}
//check end
uMatrixLocation=glGetUniformLocation(program,U_MATRIX);
uTextureUnitLocation=glGetUniformLocation(program,U_TEXTUREUNIT);
aPositionLocation=glGetAttribLocation(program,A_POSITION);
aTexPositionLocaton=glGetAttribLocation(program,A_TEXCOORDS);
}
public void setVertexAttributePointer(int location, int offset, int componentCount, int type, boolean isTranspose, int stride, FloatBuffer buffer){
buffer.position(offset);
glVertexAttribPointer(location, componentCount,type,isTranspose,stride,buffer);
glEnableVertexAttribArray(location);
buffer.rewind();
}
public void setup(FloatBuffer vertexBuffer, FloatBuffer texBuffer){
vertexBuffer.position(0);
glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT,false,0,vertexBuffer);
glEnableVertexAttribArray(aPositionLocation);
vertexBuffer.rewind();
texBuffer.position(0);
glVertexAttribPointer(aTexPositionLocaton,2,GL_FLOAT,false,0,texBuffer);
glEnableVertexAttribArray(aTexPositionLocaton);
texBuffer.rewind();
}
public int getProgram(){
return program;
}
public void bind(int texture, float[] matrix){
glUniformMatrix4fv(uMatrixLocation, 1, false, matrix,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture);
glUniform1i(uTextureUnitLocation,0);
}
private static String loadString(Context context, int resourceID){
StringBuilder builder=new StringBuilder();
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(context.getResources().openRawResource(resourceID)));
String line;
while((line=reader.readLine())!=null){
builder.append(line);
builder.append('\n');
}
}
catch(Exception e){
}
return builder.toString();
}
}
Below are the shader source codes.
Vertex Shader:
uniform mat4 u_matrix;
attribute vec4 a_position;
attribute vec2 a_texPos;
varying vec2 v_texPos;
void main{
v_texPos=a_texPos;
gl_Position=u_matrix*a_position;
}
Fragment Shader:
precision mediump float;
uniform sampler2D u_texUnit;
varying vec2 v_texPos;
void main(){
gl_FragColor=texture2D(u_texUnit,v_texPos);
}
I will post a screenshot of Logcat displaying nothing but the phrase "Vertex shader error" debug message. The GL_INVALID_OPERATION is caused by the vertex shader status being 0, was deleted and returned back to the onSurfaceCreated(), and not being able to point to the matrix location, as the vertex shader is nonexistent at that point in time.
I think it's just a simple typo. The vertex shader is missing () after main:
void main{
needs to be:
void main(){

Textures in OpenGL ES 2.0 for Android

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.)

Categories

Resources