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.
Related
So im working on a Java/Jogl application that basically just translates the vertices of a triangle. So far I am able to get the triangle to move left to right, but I cant figure out how to edit the vertex shader so that when I click a button, the triangle begins to move up and down instead of left and right.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.*;
import javax.swing.*;
import static com.jogamp.opengl.GL4.*;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import graphicslib3D.GLSLUtils;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;
public class a1 extends JFrame implements GLEventListener
{
private GLCanvas myCanvas;
private int rendering_program;
private int vao[] = new int[1];
private GLSLUtils util = new GLSLUtils();
private Button button1, button2;
private float x = 0.0f;
private float y = 0.0f;
private float inc = 0.01f;
private float incy = 0.01f;
private int click = 1;
public a1()
{ setTitle("Chapter2 - program2");
setSize(600, 400);
myCanvas = new GLCanvas();
myCanvas.addGLEventListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(myCanvas, BorderLayout.CENTER);
JPanel sidePanel = new JPanel();
sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.Y_AXIS));
button1 = new Button("button");
button2 = new Button("button2");
sidePanel.add(button1);
sidePanel.add(button2);
getContentPane().add(sidePanel, BorderLayout.WEST);
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
click = 1;
}
});
setVisible(true);
FPSAnimator animator = new FPSAnimator(myCanvas, 30);
animator.start();
}
public void display(GLAutoDrawable drawable)
{ GL4 gl = (GL4) GLContext.getCurrentGL();
gl.glUseProgram(rendering_program);
gl.glPointSize(50.0f);
float bkg[] = {0.0f, 0.0f, 0.0f, 1.0f};
FloatBuffer bkgBuffer = Buffers.newDirectFloatBuffer(bkg);
gl.glClearBufferfv(GL_COLOR, 0, bkgBuffer);
x+=inc;
y+=incy;
if(x > 1.0f) inc = -0.01f;
if(x < -1.0f) inc = 0.01f;
if(y > 1.0f) incy = -0.01f;
if(y< -1.0f) incy = 0.01f;
int offset_loc = gl.glGetUniformLocation(rendering_program, "inc");
int offset_y = gl.glGetUniformLocation(rendering_program, "incy");
int flag = gl.glGetUniformLocation(rendering_program, "flag");
gl.glProgramUniform1f(rendering_program, offset_loc, x);
gl.glProgramUniform1f(rendering_program, offset_y, y);
gl.glProgramUniform1f(rendering_program, flag, click);
gl.glDrawArrays(GL_TRIANGLES,0,3);
}
public void init(GLAutoDrawable drawable)
{ GL4 gl = (GL4) GLContext.getCurrentGL();
rendering_program = createShaderProgram();
gl.glGenVertexArrays(vao.length, vao, 0);
gl.glBindVertexArray(vao[0]);
}
private int createShaderProgram()
{
GL4 gl = (GL4) GLContext.getCurrentGL();
int[] vertCompiled = new int[1];
int[] fragCompiled = new int[1];
int[] linked = new int[1];
String vshaderSource[] = readShaderSource("src/vert.shader");
String fshaderSource[] = readShaderSource("src/frag.shader");
int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0);
gl.glCompileShader(vShader);
util.checkOpenGLError();
gl.glGetShaderiv(vShader, GL_COMPILE_STATUS, vertCompiled, 0);
if(vertCompiled[0] == 1)
{
System.out.println("vertex compilation success");
}else{
System.out.println("vertex compilation failed");
util.printShaderLog(vShader);
}
int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0);
gl.glCompileShader(fShader);
util.checkOpenGLError();
gl.glGetShaderiv(fShader, GL_COMPILE_STATUS, fragCompiled, 0);
if(fragCompiled[0] == 1)
{
System.out.println("fragment compilation success");
}else{
System.out.println("fragment compilation failed");
util.printShaderLog(fShader);
}
int vfprogram = gl.glCreateProgram();
gl.glAttachShader(vfprogram, vShader);
gl.glAttachShader(vfprogram, fShader);
gl.glLinkProgram(vfprogram);
util.checkOpenGLError();
gl.glGetProgramiv(vfprogram, GL_LINK_STATUS, linked, 0);
if(linked[0] == 1)
{
System.out.println("linking succeeded");
}else{
System.out.println("linking failed");
util.printProgramLog(vfprogram);
}
//gl.glDeleteShader(vShader);
//gl.glDeleteShader(fShader);
return vfprogram;
}
public static void main(String[] args) { new a1(); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
public void dispose(GLAutoDrawable drawable) {}
private String[] readShaderSource(String filename)
{
Vector<String> lines = new Vector<String>();
Scanner sc;
try
{
sc = new Scanner(new File(filename));
}catch (IOException e)
{
System.err.println("IOException reading file: " + e);
return null;
}
while(sc.hasNext())
{
lines.addElement(sc.nextLine());
}
String[] program = new String[lines.size()];
for(int i=0; i<lines.size(); i++)
{
program[i] = (String) lines.elementAt(i)+ "\n";
}
return program;
}
}
#version 430
uniform float inc;
void main(void)
{
if(gl_VertexID == 0) gl_Position = vec4(0.25, -0.25+inc, 0.0, 1.0);
else if(gl_VertexID == 1) gl_Position = vec4(-0.25, -0.25+inc, 0.0, 1.0);
else gl_Position = vec4(0.25, 0.25+inc, 0.0, 1.0);
}
How do I edit my vertex shader so that when I click on a button the location of the vertices changes and the triangle begins to move up and down instead of left to right?
1) Don't do the update like that
2) Don't use an int array for the OpenGL resources (vao), prefer a direct integer buffer, you can use GLBuffers.newDirectIntBuffer(1); for the vao for example..
3) Don't use an FPSAnimator, use Animator instead
4) Don't inialize a new direct buffer everytime in the display() method (bkgBuffer), do it just once in the variable declaration or in the init(). You should also dispose any direct buffer, since it is not guaranteed that they are gonna be removed by the garbage collector. I have a small class for that here. A better one however is the implementation of Gouessej here.
4) Use a vertex buffer object to declare your vertices attributes (like position)
5) Declare a boolean flag variable update to trigger the update.
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
update = 1;
}
});
public void display(GLAutoDrawable drawable) {
GL4 gl = (GL4) GLContext.getCurrentGL();
if(update) {
update = false;
...
}
}
now you have 2 possibilities:
you update the vertices directly
gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
gl3.glBufferSubData(GL_ARRAY_BUFFER, 0, vertexBuffer.capacity(), vertexBuffer);
gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);
and your vertex shader may be something like
#version 430
layout (location = POSITION) in vec2 position;
void main(void)
{
gl_Position = vec4(position, 0, 1);
}
you update only the matrix that is going to multiply the vertices
gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
gl3.glBufferSubData(GL_UNIFORM_BUFFER, 0, Mat4.SIZE, matBuffer);
gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);
and in this case the vertex may be
#version 430
layout (location = POSITION) in vec2 position;
layout (binding = TRANSFORM) uniform Transform
{
mat4 mat;
};
void main(void)
{
gl_Position = mat * vec4(position, 0, 1);
}
In case you need further assistance, don't hesitate to ask for help.
For inspiration, you can refer to this hello triangle of mine
I am learning how to work with OpenGL ES 1.0 (I was told that it is better to start with "1.0"/"1.1").
I am trying to create transperent 3D globe on android device using transparent texture and vertex indexing but having problem which I don't understand and can't find any solution on internet.
The problem is that depending on angle of camera rotation my globe stops rendering "back side" of sphere or sphere back sides texture. On image below you can see what happens if I rotate camera in same direction.
Link to image: CLICK HERE (I have no reputation to add image directly, sorry)
So can you help me? What is my mistake, why "back" is disappearing?
"Sphere" class:
package com.example.OpenGL_Testing.openGl.geometry;
import com.example.OpenGL_Testing.openGl.Texture;
import javax.microedition.khronos.opengles.GL10;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
public class Sphere {
float step = 5f; // density of sphere; step in degrees
float[] verArray;
float[] texIndex;
short[] verIndex;
private FloatBuffer vBuff;
private FloatBuffer tBuff;
private ShortBuffer iBuff;
public final float mCenterX, mCenterY, mCenterZ;
public final float mRadius;
private final GL10 mGL;
private int stepsPai, stepsTheta;
private Texture texture;
public Sphere(float mRadius, float mCenterX, float mCenterY, float mCenterZ, GL10 mGL) {
this.mGL = mGL;
// sphere parameters
this.mRadius = mRadius;
this.mCenterX = mCenterX;
this.mCenterY = mCenterY;
this.mCenterZ = mCenterZ;
stepsPai = (int) (180f / step) ; //sphere vertical 'lines'
stepsTheta = (int) (360f / step) + 1; //sphere horizontal 'lines'
// create sphere 'dots'
createVerticesBuff();
createIndexingBuffer();
}
private float[] createVerticesArray() {
float[] vertices = new float[stepsPai * stepsTheta * 3];
int n = 0;
float cRadius, cHeight, co, si;
for (float pai = 180f - step; pai > 0f; pai -= step) {
cRadius = (float) Math.sin((pai + step) * Math.PI / 180f);
cHeight = (float) Math.cos((pai + step) * Math.PI / 180f);
for (float theta = 0.0f; theta <= 360f; theta += step) {
co = (float) Math.cos(theta * Math.PI / 180f);
si = -(float) Math.sin(theta * Math.PI / 180f);
vertices[n * 3] = mRadius * (cRadius * co) + mCenterX;
vertices[n * 3 + 1] = mRadius * (cHeight) + mCenterY;
vertices[n * 3 + 2] = mRadius * (cRadius * si) + mCenterZ;
n ++;
}
}
return vertices;
}
public FloatBuffer createVerticesBuff() {
// create array
verArray = createVerticesArray();
// create buffer
if (vBuff == null) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(verArray.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vBuff = byteBuffer.asFloatBuffer();
vBuff.put(verArray);
vBuff.flip();
}
return vBuff;
}
public short[] createIndexingArray() {
short[] indexies = new short[verArray.length*2];
int n=0;
for (int i = 0; i < stepsPai-1; i++) {
for (int j = 0; j < stepsTheta; j++) {
indexies[n] = (short) (i*stepsTheta+j);
indexies[n+1] = (short) ((i+1)*stepsTheta+j);
indexies[n+2] = (short) ((i+1)*stepsTheta+j+1);
indexies[n+3] = indexies[n+2];
indexies[n+4] = (short) (i*stepsTheta+j+1);
indexies[n+5] = indexies[n];
n+=6;
}
}
return indexies;
}
public void createIndexingBuffer(){
// create array
verIndex = createIndexingArray();
//create buffer
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(verIndex.length * 2);
byteBuffer.order(ByteOrder.nativeOrder());
iBuff = byteBuffer.asShortBuffer();
iBuff.put(verIndex);
iBuff.flip();
}
public void draw() {
// setup vertices buffer
mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vBuff.position(0);
mGL.glVertexPointer(3, GL10.GL_FLOAT, 0, vBuff);
// setup texture
if (texture != null) {
mGL.glEnable(GL10.GL_TEXTURE_2D);
mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
tBuff.position(0);
mGL.glTexCoordPointer(2, GL10.GL_FLOAT, 0, tBuff);
}
// display poligons on screen
mGL.glDrawElements(GL10.GL_TRIANGLES, verIndex.length, GL10.GL_UNSIGNED_SHORT, iBuff);
// reset settings
if (texture != null) {
mGL.glDisable(GL10.GL_TEXTURE_2D);
mGL.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
} else {
mGL.glColor4f(1, 1, 1, 1);
}
mGL.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
public void setTexture(Texture texture) {
if (this.texture != null) {
this.texture.dispose();
}
this.texture = texture;
// create array
texIndex = new float[stepsPai * stepsTheta * 2 * 2];
int n = 0;
for (float pai = 0f; pai < 180f; pai += step) {
for (float theta = 0.0f; theta <= 360f; theta += step) {
texIndex[n] = theta / 360f;
texIndex[n + 1] = pai / 180f;
n += 2;
}
}
// create buffer
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(texIndex.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
tBuff = byteBuffer.asFloatBuffer();
tBuff.put(texIndex);
tBuff.flip();
// bind texture
texture.bind();
}
}
"World" class
package com.example.OpenGL_Testing.openGl;
import android.opengl.GLSurfaceView;
import android.support.v4.view.GestureDetectorCompat;
import android.view.MotionEvent;
import android.view.View;
import com.example.OpenGL_Testing.MyApp;
import com.example.OpenGL_Testing.MyGestureListener;
import com.example.OpenGL_Testing.Screen;
import com.example.OpenGL_Testing.openGl.geometry.Sphere;
import com.example.OpenGL_Testing.openGl.geometry.Sphere2;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by Aleksandr.Tsatski on 11.11.2014.
*/
public class WorldMap implements Screen {
GLSurfaceView glView;
static final int VERTEX_SIZE = (3) * 4;
Sphere sphere;
GL10 gl;
private GestureDetectorCompat mDetector;
final Object stateChanged = new Object();
public WorldMap(final GLSurfaceView glView, final GL10 gl) {
this.glView = glView;
this.gl = gl;
sphere = new Sphere(400, 0, 0, 0, gl);
}
private void setupGestureListener(final GLSurfaceView glView, final GL10 gl) {
final MyGestureListener listener = new MyGestureListener();
listener.setGestureFeedBackListener(new MyGestureListener.Feedback() {
#Override
public void onFeedback(final int event, final float parameter1, final float parameter2) {
glView.queueEvent(new Runnable() {
#Override
public void run() {
switch (event) {
case MyGestureListener.SCROLL:
gl.glRotatef(parameter1/10, 0f, 1f, 0f);
gl.glRotatef(-parameter2/10, 0f, 0f, 1f);
break;
default:
break;
}
}
});
}
});
glView.post(new Runnable() {
#Override
public void run() {
mDetector = new GestureDetectorCompat(MyApp.getAppContext(), listener);
}
});
glView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return mDetector.onTouchEvent(motionEvent);
}
});
}
#Override
public void present(float deltaTime) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
sphere.draw();
}
#Override
public void resume() {
gl.glClearColor(1, 0, 0, 1);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
setupGestureListener(glView, gl);
sphere.setTexture(new Texture(gl, "world_map.png"));
}
#Override
public void pause() {
}
#Override
public void dispose() {
}
}
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(){
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.
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.)