Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Blank screen when uisng shaders  (Read 1656 times)
0 Members and 1 Guest are viewing this topic.
Offline 4fingers

Senior Newbie





« Posted 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();
       
        [....]
               
        // Center frame
       frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        drawable.setGL(new DebugGL(drawable.getGL()));
        GL gl = drawable.getGL();
        glut = new GLUT();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
       gl.setSwapInterval(1);

        // Setup the drawing area and shading mode
       gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        //gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
       
        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);// comment this out and u can see the teapot
   }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        [.. usual ..]
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Clear the drawing area
       //gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
       // Reset the current matrix to the "identity"
       //gl.glLoadIdentity();

        // Move the "drawing cursor" around
       gl.glTranslatef(-1.5f, 0.0f, -6.0f);
   
        //gl.glPushMatrix();
       glut.glutSolidTeapot(1.0f);

        // Flush all drawing operations to the graphics card
       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));
    }
}
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2007-10-21 00:32:42 »

My I ask why you're enabling GL_TEXTURE_GEN_S and G_TEXTURE_1D if you never bind any textures?  If the shader source is expecting a sampler in order to get the brick information (it might be useful to post the shader source code as well), then it can't read anything because you have no real texture bound to a unit.  Also, I believe that having GL_TEXTURE_GEN_S enabled will do nothing because a custom vertex shader disables the FF automatic tc generation.  Similarly automatic normal generation is not computed with the vertex shader (or at least that is what the GL 2.1 specification says as I read it).

Offline 4fingers

Senior Newbie





« Reply #2 - Posted 2007-10-21 20:06:08 »

Quote
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 coordinates
But it appears that all I forgot to do was to actually set up the uniform values.
1  
2  
3  
4  
5  
6  
// Set up initial uniform values
       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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (34 views)
2014-07-23 21:16:32

Riven (22 views)
2014-07-23 21:07:15

Riven (25 views)
2014-07-23 20:56:16

ctomni231 (56 views)
2014-07-18 06:55:21

Zero Volt (48 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!