Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (569)
Games in Android Showcase (154)
games submitted by our members
Games in WIP (618)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 ... 5 6 [7] 8 9 10
 61 
 on: 2015-04-15 09:32:44 
Started by GNecro1 - Last post by GNecro1
So i am trying to implement shaders on my own with lwjgl.
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  
package necro.graphical;

import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL20.GL_COMPILE_STATUS;
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
import static org.lwjgl.opengl.GL20.glAttachShader;
import static org.lwjgl.opengl.GL20.glCompileShader;
import static org.lwjgl.opengl.GL20.glCreateProgram;
import static org.lwjgl.opengl.GL20.glCreateShader;
import static org.lwjgl.opengl.GL20.glGetShader;
import static org.lwjgl.opengl.GL20.glLinkProgram;
import static org.lwjgl.opengl.GL20.glShaderSource;
import static org.lwjgl.opengl.GL20.glUseProgram;
import static org.lwjgl.opengl.GL20.glValidateProgram;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.opengl.Display;

public class ShaderProgram {

   private int shaderProgram, verShader, fragShader;

   private String vertexShader, fragmentShader;

   private StringBuilder vertexShaderSource;
   private StringBuilder fragmentShaderSource;

   /**
    * !!! THIS MUST BE IN THE INIT METHOD!!! initializes shader program. Use
    * load to load and then create() to create!
    *
    * @param verShader
    * @param fragShader
    */

   public ShaderProgram(String verShader, String fragShader) {
      vertexShader = verShader;
      fragmentShader = fragShader;
   }
   /**
    * Load the shaders!
    */

   public void load() {
      shaderProgram = glCreateProgram();
      verShader = glCreateShader(GL_VERTEX_SHADER);
      fragShader = glCreateShader(GL_FRAGMENT_SHADER);

      vertexShaderSource = new StringBuilder();
      fragmentShaderSource = new StringBuilder();

      BufferedReader reader = null;
      try {
         reader = new BufferedReader(new FileReader(vertexShader));
         String line;

         while ((line = reader.readLine()) != null) {
            vertexShaderSource.append(line).append('\n');
         }

      } catch (IOException e) {
         System.err.println("Can not load vertex shader! There is no :" + vertexShader);
         e.printStackTrace();
         Display.destroy();
         System.exit(1);
      } finally {
         if (reader != null) {
            try {
               reader.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      BufferedReader reader1 = null;
      try {
         reader1 = new BufferedReader(new FileReader(fragmentShader));
         String line;

         while ((line = reader1.readLine()) != null) {
            fragmentShaderSource.append(line).append('\n');
         }

      } catch (IOException e) {
         System.err.println("Can not load fragment shader! There is no :" + fragmentShader);
         e.printStackTrace();
         Display.destroy();
         System.exit(1);
      } finally {
         if (reader1 != null) {
            try {
               reader1.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }

   }

   @SuppressWarnings("deprecation")
   /**
    * Create and compile shaders!
    */

   public void create() {
      glShaderSource(verShader, vertexShaderSource);
      glCompileShader(verShader);
      if (glGetShader(verShader, GL_COMPILE_STATUS) == GL_FALSE) {
         System.err.println("Vertex shader could not comple!");
         Display.destroy();
         System.exit(1);
      }

      glShaderSource(fragShader, fragmentShaderSource);
      glCompileShader(fragShader);
      if (glGetShader(fragShader, GL_COMPILE_STATUS) == GL_FALSE) {
         System.err.println("Fragment shader could not comple!");
         Display.destroy();
         System.exit(1);
      }
      glAttachShader(shaderProgram, verShader);
      glAttachShader(shaderProgram, fragShader);
      glLinkProgram(shaderProgram);
      glValidateProgram(shaderProgram);

   }
   /**
    * Start using shader program!
    */

   public void startShader() {
      glUseProgram(shaderProgram);
   }
   /**
    * Stop using shader program!
    */

   public void stopShader() {
      glUseProgram(0);
   }

}

and using them
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  
public class Game extends State {

   ShaderProgram sp;
   Texture grass;
   MyGame mg;

   public Game(MyGame myGame) {
      mg = myGame;
   }

   @Override
   public void init() {
      grass = Loader.getTexture("img/grass.png");
      sp = new ShaderProgram("shader.ver", "shader.frag");
      sp.load();
      sp.create();
   }

   @Override
   public void render() {
      sp.startShader();
         Render.drawTexture(grass, 100, 100, 64, 64);
      sp.stopShader();
   }

   @Override
   public void update() {

   }

}

And there is nothing i can do with them now...... xD
how do i pass the values (exp. color) to the shader and use it?

 62 
 on: 2015-04-15 05:45:00 
Started by Dane - Last post by Jesse
The elliptic paraboloid stuff is interesting, but I don't think it's directly relevant here (it's just incidental to the fact that you're performing length and squared length computations).

For what it's worth, here:

1  
maxDepth = sqrt( x^2 + z^2 + minY^2 )

You've omitted the inclusion of 'minDepth' in the original code, although that may be intentional.

The use of integers and the arbitrary boundary initializers (+/-99999) could also be problematic, depending on the circumstances, but maybe that's done for a reason as well.

If you need more info, perhaps you could tell us a little more about the context, such as where the code is from, what space the computations take place in, and so on.

 63 
 on: 2015-04-15 03:10:27 
Started by Dane - Last post by Dane
Before you begin reading the code below, let it be known that this is how the coordinate system works that the snippet is for:

+X = Right
-Y = Up
+Z = Forward

I'm not very familiar with geometry, but I'm confident in my understanding and research abilities to any information given to me.

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  
   public void calculateBoundaries() {
      this.lengthXZ = 0;

      this.minBoundX = 999999;
      this.maxBoundX = -999999;

      this.maxBoundY = 0;
      this.minBoundY = 0;

      this.maxBoundZ = -99999;
      this.minBoundZ = 99999;

      for (int v = 0; v < this.vertexCount; v++) {
         int x = this.vertexX[v];
         int y = this.vertexY[v];
         int z = this.vertexZ[v];

         if (x < this.minBoundX) {
            this.minBoundX = x;
         }

         if (x > this.maxBoundX) {
            this.maxBoundX = x;
         }

         if (z < this.minBoundZ) {
            this.minBoundZ = z;
         }

         if (z > this.maxBoundZ) {
            this.maxBoundZ = z;
         }

         if (-y > this.maxBoundY) {
            this.maxBoundY = -y;
         }

         if (y > this.minBoundY) {
            this.minBoundY = y;
         }

         int lengthSquared = x * x + z * z;

         if (lengthSquared > this.lengthXZ) {
            this.lengthXZ = lengthSquared;
         }
      }

      this.lengthXZ = (int) Math.sqrt((double) this.lengthXZ);
      this.minDepth = (int) Math.sqrt((double) (this.lengthXZ * this.lengthXZ + this.maxBoundY * this.maxBoundY));
      this.maxDepth = this.minDepth + (int) Math.sqrt((double) (this.lengthXZ * this.lengthXZ + this.minBoundY * this.minBoundY));
   }


I'm trying to understand this more. lengthXZ is later multiplied by the sin(cameraPitch) and cos(cameraPitch) values for some algorithm to check if the model is within the screen. The most important part of this code to me is understanding the assignments to lengthXZ, minDepth, and maxDepth.

minDepth and maxDepth are used for sorting triangles by depth before being drawn.

I did some googling and found Elliptic Paraboloid which has the equation z = Ax^2 + By^2. I feel like it's related, as the Y and Z coordinates in this application have been flipped, so they must apply in the same way for y = Ax^2 + Bz^2. http://www.math.umn.edu/~rogness/quadrics/ellparab.shtml

By the looks of it, these are the equations for the assignments: (minDepth using maxY and maxDepth using minY was not an oversight)
1  
2  
3  
lengthXZ = sqrt( x^2 + z^2 )
minDepth = sqrt( x^2 + z^2 + maxY^2 )
maxDepth = sqrt( x^2 + z^2 + minY^2 )


Any input is appreciated. Thanks  Grin Grin

 64 
 on: 2015-04-15 02:42:11 
Started by MrPork - Last post by chrislo27
For clarity, immutability isn't why you can't use == on Strings for equality. It's because string (and most datatypes) equivalence is not object identity (pointer equivalence).

Oops, I was thinking of something else. I have no idea where I got immutability (I was reading something on primitives earlier, maybe that bled in). Thanks for correcting me!

 65 
 on: 2015-04-15 02:02:05 
Started by MrPork - Last post by BurntPizza
I didn't pick this up earlier but you CANNOT compare strings with ==. They are immutable objects, and must be compared with <someStringObject>.equals(<anotherString>).

For clarity, immutability isn't why you can't use == on Strings for equality. It's because string (and most datatypes) equivalence is not object identity (pointer equivalence).

Just be glad this isn't completely insane like in JS: https://dorey.github.io/JavaScript-Equality-Table/unified/  http://stackoverflow.com/a/359509

 66 
 on: 2015-04-15 00:11:49 
Started by MrPork - Last post by chrislo27
I didn't pick this up earlier but you CANNOT compare strings with ==. They are immutable objects, and must be compared with <someStringObject>.equals(<anotherString>). You'll also want to rid the "else" from the first if statement to check the idle stances. If you're using Eclipse you can press Control+Shift+F to reformat all the code to look better and become more readable (this also annoyingly formats comments -- you can change this in the preferences if you'd like). Using a string for left and right stances is pretty slow when you're comparing the strings to determine what idle stance you want. You should use an Enum, integer, or even a boolean.

Modified update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void update(){
      if(Gdx.input.isKeyPressed(Keys.A)){
         CurrentAnimation = WalkLeftAnimation;
         directionString = "Left";
         
      }
      if(Gdx.input.isKeyPressed(Keys.D)){
          CurrentAnimation = WalkRightAnimation;
          directionString = "Right";
      }

       if(directionString == "Left"){
          CurrentAnimation = IdleLeftAnimation;
       }
       else if(directionString == "Right"){
          CurrentAnimation = IdleRightAnimation;
       }
     
   }

 67 
 on: 2015-04-14 23:56:53 
Started by MrPork - Last post by MrPork
Ok, thanks for that help but also how would I have it so when I press left and release left the player will change its idle stance to left instead of reverting back to right?

 68 
 on: 2015-04-14 20:16:21 
Started by Fairy Tailz - Last post by Fairy Tailz
Because of jumping and movement speed, going by 2 wouldn't work well. I tried to create an AABB class but that doesn't work correctly though.
http://www.java-gaming.org/?action=pastebin&id=1260

ANy examples I'd find online would use either the same collision method I did, or not cover using it with platformers properly.

 69 
 on: 2015-04-14 18:33:32 
Started by Oskuro - Last post by cebarks
If a class doesn't define a constructor, it inherits the default constructor from the Object class, since the inheritance hierarchy of any class eventually leads down to the Object class.

 70 
 on: 2015-04-14 16:46:15 
Started by yumsoup - Last post by yumsoup
I'll look into why it doesn't work on the tablet. Was it connected to internet? I'm not sure, but maybe being unable to sign into Google Play Games might cause it to hang.

I also included an option for controlling movement using the accelerometer if that's more to your liking.

Again, thanks for your help and feedback. I really appreciate it!

Pages: 1 ... 5 6 [7] 8 9 10
 
Riven (22 views)
2015-04-16 10:48:47

Duke0200 (35 views)
2015-04-16 01:59:01

Fairy Tailz (27 views)
2015-04-14 20:13:12

Riven (28 views)
2015-04-12 21:36:37

bus hotdog (45 views)
2015-04-10 02:39:32

CopyableCougar4 (46 views)
2015-04-10 00:51:04

BurntPizza (46 views)
2015-04-06 22:06:58

ags1 (51 views)
2015-04-02 10:58:48

Riven (49 views)
2015-04-01 18:27:05

ags1 (66 views)
2015-03-31 10:55:12
How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27
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!