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 (536)
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  
  Problem with cel shading  (Read 1706 times)
0 Members and 1 Guest are viewing this topic.
Offline TarmTarm

Senior Newbie




Write once, never run.


« Posted 2003-07-22 18:33:29 »

I read the tutorial at nehe and found some other resources on the net and thought it would be cool to try. But I don't get it to work, the shading looks more like a zebra than the effect i want. I'm sure the error is very obvious but i've looked at this code for ages now and I don't understand what's wrong.

This is the effect I get: (it's supposed to be a cube)

I'll give a big cel shaded hug to the person who can help me Smiley

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  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
import net.java.games.jogl.*;

import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class TestCelShade implements GLEventListener {
    private GL gl;
    private GLU glu;
    private GLDrawable glDrawable;
    private FloatBuffer cubeVertives;
    private FloatBuffer cubeNormals;
    private int numberOfVertices = 0;
    private int[] shaderTexture = new int[1];
    private int[] shaderProgram = new int[1];
    private String programString = "";
    private float rotz = 0;
    private float[] lp = new float[]{1,-2,-3};

    public void initShadow() {
        int[] shaderData = new int[]{127, 127, 127, 191, 191, 191, 191, 191,
                                     255, 255, 255, 255, 255, 255, 255, 255};

        gl.glGenTextures(1, shaderTexture);
        gl.glBindTexture(GL.GL_TEXTURE_1D, shaderTexture[0]);
        gl.glTexImage1D (GL.GL_TEXTURE_1D, 0, GL.GL_RGBA, 16, 0, GL.GL_LUMINANCE , GL.GL_UNSIGNED_BYTE, shaderData);

        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);

        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
    }

    public void addVertice(float x, float y, float z) {
        cubeVertives.put(x);
        cubeVertives.put(y);
        cubeVertives.put(z);
        numberOfVertices++;
    }

    public void addNormal(float x, float y, float z) {
        for (int i = 0; i < 4; i++) {
            cubeNormals.put(x);
            cubeNormals.put(y);
            cubeNormals.put(z);
        }
    }

    public void initCube() {
        ByteBuffer verticeByteBuffer = ByteBuffer.allocateDirect(36000);
        verticeByteBuffer.order(ByteOrder.nativeOrder());
        cubeVertives = verticeByteBuffer.asFloatBuffer();


        addVertice(-1.0f, -1.0f, 1.0f);
        addVertice(1.0f, -1.0f, 1.0f);
        addVertice(1.0f, 1.0f, 1.0f);
        addVertice(-1.0f, 1.0f, 1.0f);

        addVertice(-1.0f, -1.0f, -1.0f);
        addVertice(-1.0f, 1.0f, -1.0f);
        addVertice(1.0f, 1.0f, -1.0f);
        addVertice(1.0f, -1.0f, -1.0f);

        addVertice(-1.0f, 1.0f, -1.0f);
        addVertice(-1.0f, 1.0f, 1.0f);
        addVertice(1.0f, 1.0f, 1.0f);
        addVertice(1.0f, 1.0f, -1.0f);

        addVertice(-1.0f, -1.0f, -1.0f);
        addVertice(1.0f, -1.0f, -1.0f);
        addVertice(1.0f, -1.0f, 1.0f);
        addVertice(-1.0f, -1.0f, 1.0f);

        addVertice(1.0f, -1.0f, -1.0f);
        addVertice(1.0f, 1.0f, -1.0f);
        addVertice(1.0f, 1.0f, 1.0f);
        addVertice(1.0f, -1.0f, 1.0f);

        addVertice(-1.0f, -1.0f, -1.0f);
        addVertice(-1.0f, -1.0f, 1.0f);
        addVertice(-1.0f, 1.0f, 1.0f);
        addVertice(-1.0f, 1.0f, -1.0f);

        ByteBuffer normalByteBuffer = ByteBuffer.allocateDirect(36000);
        normalByteBuffer.order(ByteOrder.nativeOrder());
        cubeNormals = normalByteBuffer.asFloatBuffer();

        addNormal(0, 0, 1);
        addNormal(0, 0, -1);
        addNormal(0, 1, 0);
        addNormal(0, -1, 0);
        addNormal(1, 0, 0);
        addNormal(-1, 0, 0);

    }

    public void init(GLDrawable drawable) {
        this.gl = drawable.getGL();
        this.glu = drawable.getGLU();
        this.glDrawable = drawable;


        this.glDrawable.setGL(new DebugGL(drawable.getGL()));
        gl.glEnable(GL.GL_TEXTURE_2D);
        gl.glDisable(GL.GL_LINE_SMOOTH);

        gl.glEnable(GL.GL_CULL_FACE);
        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1f);
        gl.glClearDepth(1.0f);

        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
        gl.glLineWidth(2f);

        try {
            BufferedReader input = new
                    BufferedReader(new InputStreamReader(new FileInputStream("shader.txt")));

            String line = input.readLine();
            while (line != null) {
                programString += line + "\n";
                line = input.readLine();
            }
            input.close();
        } catch (Exception e) {
            System.out.println("Error, vertex program not loaded");
        }
        gl.glGenProgramsNV(1, shaderProgram);
        gl.glLoadProgramNV(GL.GL_VERTEX_PROGRAM_NV, shaderProgram[0], programString.length(), programString);
        gl.glBindProgramNV(GL.GL_VERTEX_PROGRAM_NV, shaderProgram[0]);

        gl.glTrackMatrixNV(GL.GL_VERTEX_PROGRAM_NV, 0, GL.GL_MODELVIEW_PROJECTION_NV, GL.GL_IDENTITY_NV);


        initShadow();
        initCube();

        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, cubeVertives);

        gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
        gl.glNormalPointer(GL.GL_FLOAT, 0, cubeNormals);

    }



    public void display(GLDrawable drawable) {

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glFrustum(-1, 1, -1, 1, 1, 20);
        float[] imm = new float[16];


        gl.glTranslatef(0.0f, 0.0f, -5.0f);

        gl.glRotatef(-45.0f, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(rotz, 0.0f, 0.0f, 1.0f);
        gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, imm);

        float modelLightPosX = imm[0]*lp[0] + imm[1]*lp[1] + imm[2]*lp[2];
        float modelLightPosY = imm[4]*lp[0] + imm[5]*lp[1] + imm[6]*lp[2];
        float modelLightPosZ = imm[8]*lp[0] + imm[9]*lp[1] + imm[10]*lp[2];

        gl.glProgramParameter4fNV(GL.GL_VERTEX_PROGRAM_NV, 4,
                                  modelLightPosX, modelLightPosY,
                                  modelLightPosZ, 1f);



        gl.glColor4f(0.75f, 0.95f, 1.0f, 1.0f);
        gl.glDisable(GL.GL_TEXTURE_2D);
        gl.glBindTexture(GL.GL_TEXTURE_1D, shaderTexture[0]);
        gl.glEnable(GL.GL_TEXTURE_1D);
        gl.glEnable(GL.GL_VERTEX_PROGRAM_NV);
        gl.glDrawArrays(GL.GL_QUADS, 0, numberOfVertices);
        gl.glDisable(GL.GL_VERTEX_PROGRAM_NV);

        gl.glDisable(GL.GL_TEXTURE_1D);

        gl.glEnable(GL.GL_CULL_FACE);
        gl.glCullFace(GL.GL_FRONT);
        gl.glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glPolygonMode(GL.GL_BACK, GL.GL_LINE);
        gl.glDrawArrays(GL.GL_QUADS, 0, numberOfVertices);
        gl.glCullFace(GL.GL_BACK);



        rotz -= 0.8f;

    }

    public void reshape(GLDrawable drawable, int x, int y, int width, int height) {

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
}


and the vertex program
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  
!!VP1.0
# Vertex Program for cel shading
# c[0]-c[3] modelviewProjection matrix
# c[4]      model space light position

# Compute position
DP4 o[HPOS].x, c[0], v[OPOS];
DP4 o[HPOS].y, c[1], v[OPOS];
DP4 o[HPOS].z, c[2], v[OPOS];
DP4 o[HPOS].w, c[3], v[OPOS];

#output color
MOV o[COL0], v[COL0];

#calculate tex coord
#R0=light vector
ADD R0, c[4], -v[OPOS];

#R1 = normalize R0
DP3 R1.w, R0, R0;
RSQ R1.w, R1.w;
MUL R1.xyz, R0, R1.w;

#dot with normal for tex0.x
DP3 o[TEX0].x, R1, v[NRML];

END

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.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

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

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

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

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

ctomni231 (59 views)
2014-07-18 06:55:21
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!