Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
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  
  Some LWJGL 3d help?  (Read 645 times)
0 Members and 1 Guest are viewing this topic.
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Posted 2012-10-09 20:03:26 »

Hello All!
I am currently trying to learn opengl 3d rendering. I am using LWJGL as my engine. But I have got this problem. I follow tutorials from all over the internet, but when I run it in Eclipse, I just get a black screen:



This is the source 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  
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import static org.lwjgl.util.glu.GLU.*;

public class Main {
   
   public static void main(String args[]) {
      Main game = new Main();
      game.run();
   }
   
   private void run() {
     
      try {
         Display.setDisplayMode(new DisplayMode(500, 400));
         Display.setTitle("Hello World!");
         Display.create();
      } catch (LWJGLException ex) {
         ex.printStackTrace();
         System.exit(0);
      }
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f,((float)500)/((float)400),0.1f,100.0f);
      glEnable(GL_DEPTH_TEST);
      glMatrixMode(GL_MODELVIEW);
     
      while (!Display.isCloseRequested()) {
         
         render();
         
         Display.update();
         Display.sync(60);
         
         if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            System.exit(0);
         }
      }
     
   }
   
   public static void render() {
     
      glPushMatrix();
     
      glColor3f(0.0f, 0.5f, 0.0f);
     
      glBegin(GL_QUADS);
         glVertex3f(0.0f, 1.0f, 0.0f);
         glVertex3f(0.0f, 1.0f, -1.0f);
         glVertex3f(1.0f, 1.0f, -1.0f);
         glVertex3f(1.0f, 1.0f, 0.0f);
      glEnd();
     
      glPopMatrix();
     
   }
   
}


Please Help! If you have any more questions, then ask!

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline Giovanni

Junior Member




chop!


« Reply #1 - Posted 2012-10-09 20:22:53 »

Some things:

First of all, you forgot glClear. You have to call it in order for the quad to be visible. So your code should look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      while (!Display.isCloseRequested()) {
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         
        render();
         
         Display.update();
         Display.sync(60);
         
         if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            System.exit(0);
         }
      }





The second thing; The coordinates of your quad were pretty weird, especially the z-coords:

1  
2  
3  
4  
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);


I think you missunderstood something here. The function's parameters are like following:
glVertex3f(x, y, z);

The z-axis is in your case the depth of the whole scene. The way you specified the z-coords would mean that your quad had some weird rotation. Also, some coordinates have the same x and y coordiantes which doesn't really make sense in this case. I changed the coordinates a little bit so that the quad gets displayed:

1  
2  
3  
4  
5  
6  
glBegin(GL_QUADS);
         glVertex3f(-1.0f, 1.0f, -5.0f);
         glVertex3f(1.0f, 1.0f, -5.0f);
         glVertex3f(1.0f, -1.0f, -5.0f);
         glVertex3f(-1.0f, -1.0f, -5.0f);
glEnd();




The third thing; You don't seem to be really familiar with the OpenGL coordinate system or other basic stuff. You better stick to 2D rendering for a little bit longer to gain more knowledge.



The fourth thing; Immediate mode is wicked (yes, wicked)! It appeared in the first OpenGL version (if I'm right) which was released around 1992. I think it should be quite obvious that it is by now totally deprecated. In newer OpenGL versions these functions don't even exist anymore. You should better skip Immediate-Mode rendering and start with more modern techniques like using VAOs & VBOs or whatever.



Edit: This post should help you: http://www.java-gaming.org/topics/hello/24411/msg/205121/view.html#msg205121
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #2 - Posted 2012-10-11 20:33:45 »

Hi!
Thanks for your`e help!  Smiley I think I will try a bit more of 2d! Thanks for your`e help  Grin

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
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.

CogWheelz (17 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!