Show Posts
|
|
Pages: [1]
|
|
3
|
Java Game APIs & Engines / JOGL Development / Print variables from shader
|
on: 2007-11-06 23:59:04
|
|
Hi, I was just wandering if it is possible to display a variable defined in a vertex or fragment shader and display it in the console like you would do in Java using System.out. Just that I am finding it hard to debug shaders when I currently cant see what some of the variables are set to.
Thanks
|
|
|
|
|
4
|
Java Game APIs & Engines / JOGL Development / Re: Blank screen when uisng shaders
|
on: 2007-10-21 20:06:08
|
My I ask why you're enabling GL_TEXTURE_GEN_S and G_TEXTURE_1D if you never bind any textures? It appears I accidentally left the code there after I was learning how to get a teapot on the screen: How to draw a texture mapped teapot with automatically generated texture coordinatesBut it appears that all I forgot to do was to actually set up the uniform values. 1 2 3 4 5 6
| gl.glUniform3f(gl.glGetUniformLocation(shaderprogram, "BrickColor"), 1.0f, 0.3f, 0.2f); gl.glUniform3f(gl.glGetUniformLocation(shaderprogram, "MortarColor"), 0.85f, 0.86f, 0.84f); gl.glUniform2f(gl.glGetUniformLocation(shaderprogram, "BrickSize"), 0.30f, 0.15f); gl.glUniform2f(gl.glGetUniformLocation(shaderprogram, "BrickPct"), 0.90f, 0.85f); gl.glUniform3f(gl.glGetUniformLocation(shaderprogram, "LightPosition"), 5.0f, 0.0f, 0.0f); |
Otherwise it seems to working fine now and I have since removed those redundant enables and a bunch of other code. Thanks for your help; I really do appreciate it especially through these really noobish times.
|
|
|
|
|
5
|
Java Game APIs & Engines / JOGL Development / Blank screen when uisng shaders
|
on: 2007-10-20 16:59:26
|
I am currently trying to get a shder to create a brick like texture on a teapot. I can render a teapot fine using the GLUnit tools but as soon as I add the shaders the teapot just disapears. The frag and vertex shaders were a copy paste from the orange book so they should be fine and I dont get any errors in the shader log, so at the moment I hit a wall and not sure what I have done wrong. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| public class SimpleJOGL implements GLEventListener {
private int shaderprogram = 0; private GLUT glut; private String vertsrc = "VertexShader.vert"; private String fragsrc = "FragmentShader.frag";
public static void main(String[] args) { Frame frame = new Frame("Simple JOGL Application"); GLCanvas canvas = new GLCanvas(); [....] frame.setLocationRelativeTo(null); frame.setVisible(true); animator.start(); }
public void init(GLAutoDrawable drawable) { drawable.setGL(new DebugGL(drawable.getGL())); GL gl = drawable.getGL(); glut = new GLUT(); System.err.println("INIT GL IS: " + gl.getClass().getName());
gl.setSwapInterval(1);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glEnable(GL.GL_DEPTH_TEST); gl.glDepthFunc(GL.GL_LESS); gl.glEnable(GL.GL_TEXTURE_GEN_S); gl.glEnable(GL.GL_TEXTURE_1D); gl.glEnable(GL.GL_CULL_FACE); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_LIGHT0); gl.glEnable(GL.GL_AUTO_NORMAL); gl.glEnable(GL.GL_NORMALIZE); gl.glFrontFace(GL.GL_CW); gl.glCullFace(GL.GL_BACK); gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 64.0f); setupShaders(gl); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { [.. usual ..] }
public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL();
gl.glTranslatef(-1.5f, 0.0f, -6.0f); glut.glutSolidTeapot(1.0f);
gl.glFlush(); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
private void setupShaders(GL gl) { int v = gl.glCreateShader(GL.GL_VERTEX_SHADER); int f = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
if (shaderprogram == 0) { System.out.println("initializing shaders"); try { String[] vertexsource = {stringifyfile(vertsrc)}; String[] fragmentsource = {stringifyfile(fragsrc)}; gl.glShaderSource(v, 1, vertexsource, (int[]) null, 0); gl.glCompileShader(v); checkLogInfo(gl, v);
gl.glShaderSource(f, 1, fragmentsource, (int[]) null, 0); gl.glCompileShader(f); checkLogInfo(gl, f);
shaderprogram = gl.glCreateProgram(); gl.glAttachShader(shaderprogram, v); gl.glAttachShader(shaderprogram, f); gl.glLinkProgram(shaderprogram); gl.glValidateProgram(shaderprogram); checkLogInfo(gl, shaderprogram); } catch (FileNotFoundException e) { System.err.println("Shader source file not found." + e); } catch (IOException e) { System.err.println("Could not read shader file."); } } gl.glUseProgram(shaderprogram); checkLogInfo(gl, shaderprogram); checkLogInfo(gl, v); checkLogInfo(gl, f); }
private String stringifyfile(String filename) throws IOException { BufferedReader br = new BufferedReader(new FileReader(filename)); String res = ""; String line; while ((line = br.readLine()) != null) { res += line + "\n"; } return res; }
private void checkLogInfo(GL gl, int obj) { IntBuffer iVal = BufferUtil.newIntBuffer(1); gl.glGetObjectParameterivARB(obj, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
int length = iVal.get();
if (length <= 1) { return; }
ByteBuffer infoLog = BufferUtil.newByteBuffer(length);
iVal.flip(); gl.glGetInfoLogARB(obj, length, iVal, infoLog);
byte[] infoBytes = new byte[length]; infoLog.get(infoBytes); System.out.println("GLSL Validation >> " + new String(infoBytes)); } } |
|
|
|
|
|
6
|
Java Game APIs & Engines / JOGL Development / Re: simple procedural texture
|
on: 2007-10-12 15:13:27
|
Hi Jark, Thanks for the tip and book suggestion. But back to my problem, and after searching around I came across a MSDN page that gave more information about the glBindTextute method: http://msdn2.microsoft.com/en-us/library/ms537140.aspxIn particular this is what it said: Data is read from pixels as a sequence of signed or unsigned bytes, shorts or longs, or single-precision floating-point values, depending on type. The thing that struck me was no mention of integers so I changed the type over to floats and it started to work again. Maybe leaving a more open question if people are willing to help me gain a better understanding. At the moment I am happy to continue using this method in generating any other procedural texture work that I will do but due to the lack of knowledge I am currently unaware of any other more efficient ways in doing this. Will there be any performance issues that I might not be aware at in this moment in time? Thanks
|
|
|
|
|
7
|
Java Game APIs & Engines / JOGL Development / Re: simple procedural texture
|
on: 2007-10-12 13:55:24
|
Sorry for double post but I have some developments. I was going to try get buffered image and graphics2D involved until I found this tutorial which seems to be the thing I was looking for. It doesn't use glDrawPixels instead it used glBindTexture. http://www.java-tips.org/other-api-tips/jogl/demonstration-of-using-glbindtexture-by-creating-and-managing-two-tex.htmlUntil I manage to get a book on JOGL or OpenGL I am still in the learning stages, but at the moment I am wondering that instead of using bytes for the RGBA values I could use Integers instead. I am aware there might be some efficiency problems there but at the moment I find it easier working with simple ints. I tried converting that example to use ints but so far it hasn't worked and I am currently left with two white textures. Here is what I have tried to do. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| import java.awt.event.*; import javax.swing.JFrame; import javax.media.opengl.*; import javax.media.opengl.glu.*; import com.sun.opengl.util.*; import java.nio.IntBuffer;
public class texbind extends JFrame implements GLEventListener, KeyListener {
private GLCapabilities caps; private GLCanvas canvas; private GLU glu; private GLUT glut; private static final int rgba = 4; private static final int checkImageWidth = 64; private static final int checkImageHeight = 64; private IntBuffer checkImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba); private IntBuffer otherImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba); private int[] texName = new int[2];
public texbind() { super("texbind");
caps = new GLCapabilities(); canvas = new GLCanvas(caps); canvas.addGLEventListener(this); canvas.addKeyListener(this);
getContentPane().add(canvas); }
public void run() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250, 250); setLocationRelativeTo(null); setVisible(true); canvas.requestFocusInWindow(); }
public static void main(String[] args) { new texbind().run(); }
public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_FLAT); gl.glEnable(GL.GL_DEPTH_TEST);
makeCheckImages();
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glGenTextures(2, texName, 0); gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, checkImageBuf);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL); gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, otherImageBuf); gl.glEnable(GL.GL_TEXTURE_2D); }
public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]); gl.glBegin(GL.GL_QUADS); gl.glTexCoord2d(0.0, 0.0); gl.glVertex3d(-2.0, -1.0, 0.0); gl.glTexCoord2d(0.0, 1.0); gl.glVertex3d(-2.0, 1.0, 0.0); gl.glTexCoord2d(1.0, 1.0); gl.glVertex3d(0.0, 1.0, 0.0); gl.glTexCoord2d(1.0, 0.0); gl.glVertex3d(0.0, -1.0, 0.0); gl.glEnd();
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]); gl.glBegin(GL.GL_QUADS); gl.glTexCoord2d(0.0, 0.0); gl.glVertex3d(1.0, -1.0, 0.0); gl.glTexCoord2d(0.0, 1.0); gl.glVertex3d(1.0, 1.0, 0.0); gl.glTexCoord2d(1.0, 1.0); gl.glVertex3d(2.41421, 1.0, -1.41421); gl.glTexCoord2d(1.0, 0.0); gl.glVertex3d(2.41421, -1.0, -1.41421); gl.glEnd(); gl.glFlush(); }
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { GL gl = drawable.getGL();
gl.glViewport(0, 0, w, h); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslated(0.0, 0.0, -3.6); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
private void makeCheckImages() { for (int i = 0; i < checkImageWidth; i++) { for (int j = 0; j < checkImageHeight; j++) { checkImageBuf.put((int) (Math.random()*256)); checkImageBuf.put((int) (Math.random()*256)); checkImageBuf.put((int) (Math.random()*256)); checkImageBuf.put((int) (Math.random()*256)); otherImageBuf.put((int) (Math.random()*256)); otherImageBuf.put((int) (Math.random()*256)); otherImageBuf.put((int) (Math.random()*256)); otherImageBuf.put((int) (Math.random()*256)); } } checkImageBuf.rewind(); otherImageBuf.rewind(); }
public void keyTyped(KeyEvent key) { }
public void keyPressed(KeyEvent key) { switch (key.getKeyCode()) { case KeyEvent.VK_ESCAPE: System.exit(0); default: break; } }
public void keyReleased(KeyEvent key) { } } |
|
|
|
|
|
8
|
Java Game APIs & Engines / JOGL Development / simple procedural texture
|
on: 2007-10-11 08:16:54
|
I am just trying my hands at a procedural type of texture but so far any attempts have failed yet I can draw squares and triangles no problem. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 1.0f, -6.0f);
float[] brickColour = { 0.5f, 0.15f, 0.14f}; gl.glBegin(GL.GL_TRIANGLES); gl.glColor3fv(brickColour,0); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glColor3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glEnd();
gl.glTranslatef(3.0f, 0.0f, 0.0f); drawImage(gl);
gl.glFlush(); }
private void drawImage(GL gl) { gl.glColor3f(1.0f, 0.5f, 0.0f); gl.glRecti(0, 1, 1, 0); gl.glColor3f(0.0f, 0.0f, 0.0f);
int width = 50; int height = 100; IntBuffer buffer = IntBuffer.allocate(3 * width * height);
for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { buffer.put(0); buffer.put(0); buffer.put(255); } } buffer.rewind();
gl.glDrawBuffer(GL.GL_FRONT); gl.glRasterPos2i(0, 0); gl.glDrawPixels(width, height, GL.GL_RGB, GL.GL_INT, buffer); } |
Any idea where I am going wrong. Thanks Edit: Woops left in two unrealted functions, now been removed
|
|
|
|
|