Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075930 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:07:04 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)LWJGL GL11 Rendering a 3D Shape
Pages: [1]
Print
Author Topic: LWJGL GL11 Rendering a 3D Shape  (Read 3329 times)
Jay_PC
Level 1
*


It, Happens...


View Profile Email
« on: June 03, 2011, 08:15:51 PM »

I am trying to Learn 3D finally for kicks and giggles. I Can Render 2D objects in OGL but when it comes to 3D im lost. Im Trying to Render a Triangle using:

Code:
   GL11.glBegin(GL11.GL_TRIANGLES);

   GL11.glVertex3f(-1.0f, -0.5f, -4.0f);    // lower left vertex
   GL11.glVertex3f( 1.0f, -0.5f, -4.0f);    // lower right vertex
   GL11.glVertex3f( 0.0f,  0.5f, -4.0f);    // upper vertex

   GL11.glEnd();


But when I run, it Dosent Display anything. I read somthing about a GL11.glEnable(Somthing3D) in order for it to work but I cant find any working examples. Anyone able to at least point me in the right direction?

Edit: The Window Opens and everything 2D renders. But the Triangle dosent Show.
Logged

Sakar
Level 9
****


BONES


View Profile WWW Email
« Reply #1 on: June 03, 2011, 08:46:14 PM »

Impossible to tell what's wrong without seeing all the code
Logged

cold void | twitter
Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #2 on: June 03, 2011, 08:47:47 PM »

EDIT: I fixed some stuff and now I have Quads Rendering, but My new issue is that when the box rotates the Green one is always on top.

Edit: I though I could Fix this with a Quad_Strip but It dident work... anyway here is the updated code. It seems like the White Face is always on top now... I know its because the White Face is rendered on screen first but I thought glRotatef(angle, 0.0f, 0.5f, 0.0f); Would Rotate the Box on the Y by the angle which would translate the Z of the white Face Behind the Z of the Orange Face.... hmmmmm
Code:
package test;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.*;

import static org.lwjgl.opengl.GL11.*;
 
public class DisplayExample2 {

    public void start() {
     float angle = 0;
        try {
         Display.setDisplayMode(new DisplayMode(800,600));
         Display.create();
        } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
        }
 
while (!Display.isCloseRequested()) {
   // Clear the screen and depth buffer
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 800, -600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

   GL11.glPushMatrix();
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
   GL11.glLoadIdentity();
   glShadeModel(GL_FLAT);
   GL11.glTranslatef(400,300,0.0f);
   GL11.glRotatef(angle, 0.0f, 0.5f, 0.0f);
   GL11.glRotatef(50, 0.0f, 0.0f, 1.0f);
   GL11.glScalef(100,100,100);
   GL11.glBegin(GL11.GL_QUAD_STRIP);
   {  
   
    //this Makes a box that has no top or bottom.
    //Front - Orange
    //Right - White
    //Back - Blue
    //Left - Teal
    glColor4f(1.0f,0.5f,0.0f,1.0f);
    glVertex3f(0.0f,1.0f,0.0f); //Front Bottom Left
    glVertex3f(0.0f,0.0f,0.0f); //Front Top Left
    glVertex3f(1.0f,1.0f,0.0f); //Front Bottom Right
    glVertex3f(1.0f,0.0f,0.0f); //Front Top Right
   
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glVertex3f(1.0f,1.0f,1.0f); //Right Bottom Back
    glVertex3f(1.0f,0.0f,1.0f); //Right Top Back


    glColor4f(0.0f,0.0f,1.0f,1.0f);
    glVertex3f(0.0f,1.0f,1.0f); //Back left Bottom
    glVertex3f(0.0f,0.0f,1.0f); //Back Left Top
   

    glColor4f(0.0f,1.0f,1.0f,1.0f);
    glVertex3f(0.0f,1.0f,0.0f); //Left Bottom Front
    glVertex3f(0.0f,0.0f,0.0f); //Left Top Front
   
   }
   GL11.glEnd();
   GL11.glPopMatrix();
   angle +=0.5f;
   System.out.println(angle);

   Display.update();
   
}
 
Display.destroy();
    }
 
    public static void main(String[] argv) {
        DisplayExample2 quadExample = new DisplayExample2();
        quadExample.start();
    }
}


All this Pain in my butt to Render a cube!!!
« Last Edit: June 04, 2011, 11:03:33 PM by Jay_PC » Logged

Nugsy
Level 10
*****



View Profile
« Reply #3 on: June 05, 2011, 07:45:16 AM »

From looking at this. It looks like you have alot of extra code, and some of it seems to be redundant.

I'm no OpenGL expert, i've only dabbled with it a few times in the past; but have you tried placing

Code:
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

above

Code:
// Clear the screen and depth buffer

?

I'm pretty sure that as with other graphical applications, you need to clear the screen first at the beginning of each frame.
Logged


Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #4 on: June 05, 2011, 04:31:14 PM »

I had that there at one point, It just got moved. Either place it Doesn't Fix the issue sadly, it only clears the window so you don't get a Smear effect as it moves.

Thank you for the help though, as its always appreciated

I still Don't know what's wrong... Seriously I have No Idea. I think its somthing to do with Quads Render as 2D polygons and the 3D floats make the Planes LOOK 3D but they Draw to the screen like 2D shapes(Last on top)

My code is currently this:
Code:
package test;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.*;

import static org.lwjgl.opengl.GL11.*;
 
public class DisplayExample2 {

    public void start() {
    float angle = 0;
        try {
        Display.setDisplayMode(new DisplayMode(800,600));
        Display.create();
        } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
        }
 
while (!Display.isCloseRequested()) {

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    // Clear the screen and depth buffer
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 800, -600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

    glShadeModel(GL_FLAT);
   
    GL11.glPushMatrix();
    GL11.glLoadIdentity();
   
      GL11.glTranslatef(400,300,0.0f);
    GL11.glRotatef(angle, 0.0f, 0.5f, 0.0f);
    GL11.glRotatef(50, 0.0f, 0.0f, 1.0f);
    GL11.glScalef(100,100,100);
   
    GL11.glBegin(GL11.GL_QUAD_STRIP);
     {
    //this Makes a box that has no top or bottom.
    //Front - Orange
    //Right - White
    //Back - Blue
    //Left - Teal
   
    glColor4f(1.0f,0.5f,0.0f,1.0f);
    glVertex3f(0.0f,1.0f,0.0f); //Front Bottom Left
    glVertex3f(0.0f,0.0f,0.0f); //Front Top Left
    glVertex3f(1.0f,1.0f,0.0f); //Front Bottom Right
    glVertex3f(1.0f,0.0f,0.0f); //Front Top Right
   
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glVertex3f(1.0f,1.0f,1.0f); //Right Bottom Back
    glVertex3f(1.0f,0.0f,1.0f); //Right Top Back


    glColor4f(0.0f,0.0f,1.0f,1.0f);
    glVertex3f(0.0f,1.0f,1.0f); //Back left Bottom
    glVertex3f(0.0f,0.0f,1.0f); //Back Left Top
   

    glColor4f(0.0f,1.0f,1.0f,1.0f);
    glVertex3f(0.0f,1.0f,0.0f); //Front Bottom Left
    glVertex3f(0.0f,0.0f,0.0f); //Front Top Left
   
     }
    glEnd();
    GL11.glPopMatrix();
    angle +=0.5f;
    System.out.println(angle);

    Display.update();
   
}
 
Display.destroy();
    }
   
   
   
   
 
    public static void main(String[] argv) {
        DisplayExample2 quadExample = new DisplayExample2();
        quadExample.start();
    }
}


If anyone can point me to a good "Render a Box" Tutorial or Source example. Or one that shows the easiest way to load .obj files and bind a texture Id really appreciate it.
« Last Edit: June 07, 2011, 07:03:26 PM by Jay_PC » Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic