Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (866)
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  
  glVertex3f doesn't care about z axis???  (Read 7140 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Devvie


Medals: 4
Projects: 1


Noob going through metamorphosis...


« Posted 2015-01-04 23:17:25 »

Erm, I'm just trying to get used to LWJGL 3, and I'm following the newbie tutorials again.  For some reason, when I call glVertex3f(#, #, #), altering the z argument doesn't change the shape
in any way.  Is there some projection function I failed to call properly?  Here is my code.

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  
package driver;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.opengl.GL11.*;


/**
 * Class containing main method
 * @author William Andrew Cahill
 */

public class Driver
{
   // Fields
   private static long window;                     // Handle on window
   private static final int width = 600, height = 400;   // Size of window
   private static GLFWKeyCallback callback;         // Reference to callback so it doesn't get gced
   
   
   /**
    * Main method that fires the program up
    * @param args Command-line arguments
    */

   public static void main(String[] args)
   {
      // Initializes OpenGL
      init();
     
      // Runs loop until window closes
      while(glfwWindowShouldClose(window) == GL11.GL_FALSE)
      {
         loop();
         try
         {
            Thread.sleep(1000 / 60);
         }
         catch (InterruptedException e)
         {
            e.printStackTrace();
         }
      }
     
      // Terminates program, cleaning up resources
      terminate();
   }
   
   
   /**
    * Initializes window
    */

   public static void init()
   {
      // Tries to initialize GLFW
      int code = glfwInit();
      if(code != GL11.GL_TRUE)
         throw new IllegalStateException("Failed to initialize GLFW.  glfwInit() code was " + code);
     
      // Tries to initialize window
      window = GLFW.glfwCreateWindow(width, height, "Experiment", NULL, NULL);
      if(window == NULL)
         throw new IllegalStateException("Failed to initialize window.  glfwCreateWindow() code was " + window);
     
      // Determines how key input is handled
      GLFW.glfwSetKeyCallback(window, callback = new GLFWKeyCallback()
      {
         @Override
         public void invoke(long window, int key, int scancode, int action, int mods)
         {
            if(action == GLFW_PRESS)
               System.out.println("Pressed " + key + '!');
            else if(action == GLFW_RELEASE)
               System.out.println("Released " + key + '!');
         }
      });
     
      // Sets window to current window
      GLFW.glfwMakeContextCurrent(window);
   }
   
   
   /**
    * Main loop of the program
    */

   public static void loop()
   {
      // Header info
      GLContext.createFromCurrent();
        glViewport(0, 0, width, height);
        GL11.glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glClearColor(0.5f, 0.5f, 0.5f, 1);
        glClear(GL_COLOR_BUFFER_BIT);
       
        // Flips view
        GL11.glPushMatrix();
       
        // Rendering
        glBegin(GL_TRIANGLES);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
       
        // Triangle vertices
        // Vertex 1
        float depth = 0;
        glColor3f(1.f, -0.f, 0.f);
        glVertex3f(-1, -1, depth);
       
        // Vertex 2
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, depth);
       
        // Vertex 3
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, depth);
       
        // Done rendering triangle
        glEnd();
       
        // Reverts matrix
        GL11.glPopMatrix();
       
        // Draws
        glfwSwapBuffers(window);
        glfwPollEvents();
   }
   
   
   
   /**
    * Terminates the program
    */

   public static void terminate()
   {
      glfwTerminate();
      glfwDestroyWindow(window);
   }
}


It doesn't seem to matter what I set my depth value to in loop().  As long as it's between -1 and 1, the colorful triangle looks the same.  Help?

Did you know that 90% of statistics are wrong?
Offline gouessej
« Reply #1 - Posted 2015-01-04 23:36:06 »

Hi

Are you really sure that you want to modify the projection matrix?? glMatrixMode(GL_PROJECTION)?

Rather call glFrustum or gluPerspective somewhere when the projection matrix is active and then leave the model-view matrix active. Please learn the basic concepts of OpenGL before using any Java binding for OpenGL/OpenGL-ES. You shouldn't create a new context at each frame. Your code is really very wrong. It would be better if someone else with a better understanding of the library you use could fix your code.

Edit.: Look at this example:
http://www.lwjgl.org/guide
In this example, loop() is called only once.

Julien Gouesse | Personal blog | Website | Jogamp
Offline DrewLols

Senior Devvie


Medals: 4
Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2015-01-04 23:53:32 »

Hi

Are you really sure that you want to modify the projection matrix?? glMatrixMode(GL_PROJECTION)?

Rather call glFrustum or gluPerspective somewhere when the projection matrix is active and then leave the model-view matrix active. Please learn the basic concepts of OpenGL before using any Java binding for OpenGL/OpenGL-ES. You shouldn't create a new context at each frame. Your code is really very wrong. It would be better if someone else with a better understanding of the library you use could fix your code.

Edit.: Look at this example:
http://www.lwjgl.org/guide
In this example, loop() is called only once.

It would be easier to learn these basic opengl concepts if there were any guides that made any sense.  There are always lines and lines of unexplained code that cannot be understood by fiddling, and the opengl documentation is poor.  That's why I'm forced to ask my questions here, you see...  I haven't found a book or guide anywhere that is down to earth enough to understand.

Also, I'd like to point out that that is the exact tutorial I've been looking at...  If even that isn't useful for me, then what do you propose I do?

Did you know that 90% of statistics are wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 123
Projects: 15


★★★★★


« Reply #3 - Posted 2015-01-04 23:55:11 »

try the OpenGL tutorials here, they are easy to read, use GLFW and include source code. Your above snippet of code is using jurassic OpenGL which should be avoided.
Offline DrewLols

Senior Devvie


Medals: 4
Projects: 1


Noob going through metamorphosis...


« Reply #4 - Posted 2015-01-04 23:57:09 »

try the OpenGL Tutorials here, simply explained, using GLFW and with sample codes. Your above snippet of code is using jurassic OpenGL which should be avoided.

I'm well aware that I should be using VBO's.  I just wanted to get an image displayed before I tried anything else...

Did you know that 90% of statistics are wrong?
Offline KaiHH

JGO Kernel


Medals: 780



« Reply #5 - Posted 2015-01-05 00:01:08 »

Sri Harsha Chilakapati (known by his initials SHC here and on the LWJGL forum) is taking on the journey of writing a tutorial series on LWJGL 3.

I highly recommend having a visit to his site http://goharsha.com/lwjgl-tutorial-series/.

Regards,
Kai
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 123
Projects: 15


★★★★★


« Reply #6 - Posted 2015-01-05 00:05:19 »

I'm well aware that I should be using VBO's.  I just wanted to get an image displayed before I tried anything else...
OpenGL is pretty low level, so it not as straight forward as high level libraries to draw an image.

To draw an image you would normally first need to set up your matrices on how to draw your vertices on the screen which you can pass to a shader, then draw a square (using two triangles, the vertices and texture coordinates of which would be in a VBO). Then you would load your image as an OpenGL texture and pass that to a shader which draws it on the triangles when rendering them.
Offline DrewLols

Senior Devvie


Medals: 4
Projects: 1


Noob going through metamorphosis...


« Reply #7 - Posted 2015-01-05 00:09:39 »

I'm well aware that I should be using VBO's.  I just wanted to get an image displayed before I tried anything else...
OpenGL is pretty low level, so it not as straight forward as high level libraries to draw an image.

To draw an image you would normally draw a square (using two triangles, the vertices and texture coordinates of which would be in a VBO). Then you would load your image as an OpenGL texture and pass that to a shader which draws it on the triangles when rendering them.

That's my biggest fear, actually.  I've delved into VBO's before, and there is so much setup, it makes me want to cry.  I'm going to have to remember all of the c functions and constants required to make this work, and I don't even want to look at shaders again.  Shaders are another concept that seem to lack explanation, even in the most down-to-earth tutorials.

I think this thread has drifted away from it's intended purpose, though.

Did you know that 90% of statistics are wrong?
Offline KaiHH

JGO Kernel


Medals: 780



« Reply #8 - Posted 2015-01-05 00:22:32 »

Okay, so you need a perspective projection then.
Have a look at http://forum.lwjgl.org/index.php?topic=5586.0, which is a post on the LWJGL forum targeting LWJGL 3 with exactly that problem and a possible solution to it.
Normally I don't like referencing my own posts but it fits perfectly to your problem.
Offline gouessej
« Reply #9 - Posted 2015-01-05 14:15:17 »

It would be easier to learn these basic opengl concepts if there were any guides that made any sense.  
Maybe you have to learn the basic concepts of 3D computer graphics too. You can look at the OpenGL Red Book and the Blue Book. OpenGL isn't the kind of thing that you can learn in a few days.

There are always lines and lines of unexplained code that cannot be understood by fiddling, and the opengl documentation is poor.
The OpenGL SDK is very well documented, you should always look at the "man":
http://www.opengl.org/sdk/docs/

That's why I'm forced to ask my questions here, you see...  I haven't found a book or guide anywhere that is down to earth enough to understand.

Also, I'd like to point out that that is the exact tutorial I've been looking at...  If even that isn't useful for me, then what do you propose I do?
You modified a simple example that you didn't understand and you broke it. Just move the creation of the context (GLContext.createFromCurrent()) and the setup of the projection matrix somewhere else. I totally understand your position. I thought that someone else could fix your code despite the fact that it relies on some features of the fixed pipeline.

Julien Gouesse | Personal blog | Website | Jogamp
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #10 - Posted 2015-01-05 14:48:10 »

When there is no projection matrix defined, you are using Orthographic projection matrix. In orthographic projection, depth doesn't change the size of the shape.

To see the depth working, you need to have a perspective projection, so make a call to either glFrustum or gluPerspective functions after changing the matrix mode to GL_PROJECTION, and then change the mode back to GL_MODELVIEW.

Hope this helps..

Offline KaiHH

JGO Kernel


Medals: 780



« Reply #11 - Posted 2015-01-05 14:52:39 »

Quote
... so make a call to either glFrustum or gluPerspective ...
Hey SHC,
sorry, but just to prevent some misunderstanding: There is no gluPerspective or GLU or GLUT in general in LWJGL 3 anymore.
That's why for perspective projection the OP needs to build the matrix on his/her own.
Offline SHC
« Reply #12 - Posted 2015-01-05 14:58:29 »

Oops, I didn't read the thread completely, didn't notice that the OP is using LWJGL 3.

Offline DrewLols

Senior Devvie


Medals: 4
Projects: 1


Noob going through metamorphosis...


« Reply #13 - Posted 2015-01-05 18:05:07 »

Oops, I didn't read the thread completely, didn't notice that the OP is using LWJGL 3.

Bah, it's still actually quite helpful that you mentioned that I need to set up a projection... not that I quite know what that means when it comes to matricies, but it gives me somewhere to start looking for a solution.  I'll keep looking at tutorials hoping that at some point, they'll start making sense.  For now, I'll try copying and pasting code snippits and tinker with them.

Did you know that 90% of statistics are wrong?
Offline gouessej
« Reply #14 - Posted 2015-01-05 20:19:09 »

For now, I'll try copying and pasting code snippits and tinker with them.
You don't even understand what you copy, you're creating a context at each iteration which is absurd  Clueless

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (339 views)
2019-09-04 15:33:17

hadezbladez (5120 views)
2018-11-16 13:46:03

hadezbladez (2002 views)
2018-11-16 13:41:33

hadezbladez (5389 views)
2018-11-16 13:35:35

hadezbladez (1114 views)
2018-11-16 13:32:03

EgonOlsen (4528 views)
2018-06-10 19:43:48

EgonOlsen (5384 views)
2018-06-10 19:43:44

EgonOlsen (3076 views)
2018-06-10 19:43:20

DesertCoockie (3970 views)
2018-05-13 18:23:11

nelsongames (4520 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!