Hello Guest

Meshes are not drawing.

  • 7 Replies
  • 4672 Views
Meshes are not drawing.
« on: October 01, 2011, 22:18:18 »
I have been working on a small game test. I have several classes.

A class for the First Person Control.
A class to get the frame-rate from cached frame times.
A class that is the World-Render Manager.
An abstract class that is used to define different pre-made shapes.
Several classes that extend that class. These are the Shape classes.
A settings class that holds stuff like fps sensitivity, is game paused, key controls, etc...
And my main class.

there are a few other classes.

the process is. In the main loop I use a method of the world renderer, which at this point just calls the render method on the shape object I pass through, plus setting color.

  • Main Loop passes a RenderShape object into the world renderer
  • World Renderer runs the render method on the shape after setting up some stuff
  • RenderShape object render method sets all the vertices up and etc...

My problem is that even though I render the objects (and the debug shows they render) the objects do not appear on the screen.

Everything else works perfectly.

Here are my codes that deal with anything with rendering.

----------------------------------------------------------------

In my main loop: render is a WorldRenderer object.

DisplayMain.java(Part of it at least)
Code: [Select]
render.addShape    ( RenderShape.CUBE, 0F,  0F, 0F);
render.addShape    ( RenderShape.CUBE, 2F,  4F, 2F);
render.addShape    ( RenderShape.CUBE, 3F,  6F, 2F);
render.addRGBShape ( RenderShape.CUBE, 1F,  0F, 1F, 1F, 0.4F, 0F);
render.addRGBShape ( RenderShape.CUBE, 1F,  0F, 1F, 1F, 0.4F, 0F);
render.addRGBShape ( RenderShape.CUBE, 2F,  1F, 6F, 1F, 0.4F, 0F);

WorldRenderer.java
Code: [Select]
public class WorldRender {

public WorldRender(){
}

public void addShape(RenderShape s, Float x, Float y, Float z){

GL11.glColor3f(1.0f,0.25f,0.25f);

s.render(x, y, z);

}
public void addRGBShape(RenderShape s, Float x, Float y, Float z, Float r, Float g, Float b){

GL11.glColor3f(r,g,b);

s.render(x, y, z);

}

}

RenderShape.java
Code: [Select]
public abstract class RenderShape{

public RenderShape(){}

public abstract void render(float x, float y, float z);

public abstract void scale(float f);

//Define Shapes

public static RenderShape UVSPHERE;//       NOT MADE YET
public static RenderShape CUBE = new RenderBox();
public static RenderShape PLANE = new RenderPlane();
public static RenderShape ICOSPHERE;//     NOT MADE YET
public static RenderShape CYLINDER_8;//    NOT MADE YET
public static RenderShape CYLINDER_16;//  NOT MADE YET
public static RenderShape CYLINDER_32;//  NOT MADE YET
public static RenderShape CONE;//            NOT MADE YET
public static RenderShape CIRCLE;//          NOT MADE YET
}

RenderBox.java
Code: [Select]
public class RenderBox extends RenderShape{

public RenderBox() {
}

public void render(float x, float y, float z){

Vertex v1 = Shape.makeVertice(x+(1F*scale), y+(1F*scale), z+(1F*scale));
Vertex v2 = Shape.makeVertice(x+(1F*scale), y-(1F*scale), z+(1F*scale));
Vertex v3 = Shape.makeVertice(x-(1F*scale), y-(1F*scale), z+(1F*scale));
Vertex v4 = Shape.makeVertice(x-(1F*scale), y+(1F*scale), z+(1F*scale));
Vertex v5 = Shape.makeVertice(x+(1F*scale), y+(1F*scale), z-(1F*scale));
Vertex v6 = Shape.makeVertice(x+(1F*scale), y-(1F*scale), z-(1F*scale));
Vertex v7 = Shape.makeVertice(x-(1F*scale), y-(1F*scale), z-(1F*scale));
Vertex v8 = Shape.makeVertice(x-(1F*scale), y+(1F*scale), z-(1F*scale));

GL11.glBegin(GL11.GL_QUADS);

Face.RENDERFACE_QUAD(v1, v2, v3, v4);
Face.RENDERFACE_QUAD(v5, v6, v7, v8);
Face.RENDERFACE_QUAD(v1, v2, v5, v6);
Face.RENDERFACE_QUAD(v3, v4, v7, v8);
Face.RENDERFACE_QUAD(v2, v3, v6, v7);
Face.RENDERFACE_QUAD(v1, v4, v5, v8);

GL11.glEnd();

}

public void scale(float i){
scale=i;
}

private float scale;

}

RenderPlane.java
Code: [Select]
public class RenderPlane extends RenderShape{

public RenderPlane() {
}

public void render(float x, float y, float z){

Vertex v1 = new Vertex(x+(1F*scale), y, z+(1F*scale));
Vertex v2 = new Vertex(x-(1F*scale), y, z+(1F*scale));
Vertex v3 = new Vertex(x+(1F*scale), y, z-(1F*scale));
Vertex v4 = new Vertex(x-(1F*scale), y, z-(1F*scale));

GL11.glBegin(GL11.GL_QUADS);

Face.RENDERFACE_QUAD(v1, v2, v3, v4);

GL11.glEnd();

}

public void scale(float i){
scale=i;
}

private float scale;

}

Vertex.java
Code: [Select]
public class Vertex{

public Vertex(Float x, Float y, Float z){

_x=x;
_y=y;
_z=z;

}

public Float getX(){
return _x;
}
public Float getY(){
return _y;
}
public Float getZ(){
return _z;
}

public void setXYZ(Float x, Float y, Float z){

_x=x;
_y=y;
_z=z;

}
public Float[] getXYZ(){
return new Float[]{_x, _y, _z};
}

private Float _x;
private Float _y;
private Float _z;

}

Face.java
Code: [Select]
package org.ic3d;

import org.lwjgl.opengl.GL11;

public class Face{

public static void RENDERFACE_QUAD(Vertex v1, Vertex v2, Vertex v3, Vertex v4){

GL11.glVertex3f(v1.getX(), v1.getY(), v1.getZ());
GL11.glVertex3f(v2.getX(), v2.getY(), v2.getZ());
GL11.glVertex3f(v3.getX(), v3.getY(), v3.getZ());
GL11.glVertex3f(v4.getX(), v4.getY(), v4.getZ());

}

public static void RENDERFACE_TRI(Vertex v1, Vertex v2, Vertex v3){

GL11.glVertex3f(v1.getX(), v1.getY(), v1.getZ());
GL11.glVertex3f(v2.getX(), v2.getY(), v2.getZ());
GL11.glVertex3f(v3.getX(), v3.getY(), v3.getZ());

}
}

If anyone sees why this is not working please tell me.
The statement below is true.

The statement above is false.

*

Offline abcdef

  • ****
  • 320
Re: Meshes are not drawing.
« Reply #1 on: October 03, 2011, 11:19:42 »
Have you got the rest of your code showing the initalisation of the opengl settings? I'd check the that the camera (ie what you see on screen) is actually facing the right direction, good way is to translate along the z axis before you start rendering so everything is rendered in front of you

Re: Meshes are not drawing.
« Reply #2 on: October 03, 2011, 20:12:57 »
I have an FPS camera, and the yaw and pitch and capping and everything is working 100% accurately on the debug. I'll post the initalization code, it's a bit jumbled though, I initalize some stuff, then make some instances of other classes, then initalize some more stuff. So I'll eliminate the middle stuff.

Code: [Select]
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
//Display.setVSyncEnabled(true);
}catch(LWJGLException e){
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_TEXTURE);
GL11.glEnable(GL11.GL_3D_COLOR_TEXTURE);
GL11.glEnable(GL11.GL_ALPHA);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0F, (800F) / (600F), 0.1F, 100.0F);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

I'll also give you my FPS camera so you may be able to tell what's going on:

Code: [Select]
package org.ic3d;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;


public class FPSCameraControl{

    public Vector3f position = null;
    public float yaw = 0.0f;
    public float pitch = 0.0f;
   
    public FPSCameraControl(float x, float y, float z)
    {
        position = new Vector3f(x, y, z);
    }
   
    public void yaw(float amount)
    {
        yaw += amount;
        if(yaw>360){
        yaw=0+(yaw-360);
        }
        if(yaw<0){
        yaw=360-(0-yaw);
        }
    }
   
    public void pitch(float amount)
    {
        pitch += amount;
        if(pitch>=180){
        pitch=180;
        }
        if(pitch<=0){
        pitch=0;
        }
    }
   
    public void walkForward(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw));
    }
   
    public void walkBackwards(float distance)
    {
        position.x += distance * (float)Math.sin(Math.toRadians(yaw));
        position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
    }
   
    public void strafeLeft(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
    }
   
    public void strafeRight(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
    }
   
    public void lookThrough()
    {
        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        GL11.glTranslatef(position.x, position.y, position.z);
    }
   
    public void onUpdate()
    {
    //Calculate Sensitivity
   
    float sen = (0.02F*Settings.SENSITIVITY);
   
    //Get Center Of Screen
   
    int cx = Display.getDisplayMode().getWidth()/2;
    int cy = Display.getDisplayMode().getHeight()/2;
   
    //Get Mouse Position From Center
   
    int x = Mouse.getX()-cx;
    int y = Mouse.getY()-cy;
   
    //Apply Inverting If Set
   
    if(Settings.invert_y){
    y = -y;
    }
    if(Settings.invert_x){
    x = -x;
    }
   
    //Apply Sensitivity
   
    float _yaw = (x*sen);
    float _pitch = (y*sen);
   
    yaw(_yaw);
    pitch(_pitch);
   
    UpdatePosition();
   
    //SET MOUSE TO CENTER
   
    Mouse.setCursorPosition(cx, cy);
   
    lookThrough();
       
    }
   
    public void printROT(){
    System.out.println("Pitch : "+pitch);
    System.out.println("Yaw : "+yaw);
    System.out.println();
    System.out.println("XYZ : "+position.x+", "+position.y+", "+position.z);
    }
   
    public void UpdatePosition(){
   
    float f=0;
    float b=0;
    float r=0;
    float l=0;
    boolean moved =false;
    boolean shift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
    if(Keyboard.isKeyDown(Settings.key_forward)){
    if(shift){
    f=0.07F;
    }else{
    f=0.04F;
    }
    moved=true;
    }
    if(Keyboard.isKeyDown(Settings.key_back)){
    if(shift){
    b=0.07F;
    }else{
    b=0.04F;
    }
    moved=true;
    }
    if(Keyboard.isKeyDown(Settings.key_right)){
    if(shift){
    r=0.05F;
    }else{
    r=0.035F;
    }
    moved=true;
    }
    if(Keyboard.isKeyDown(Settings.key_left)){
    if(shift){
    l=0.05F;
    }else{
    l=0.035F;
    }
    moved=true;
    }
    walkForward(f);
    walkBackwards(b);
    strafeLeft(l);
    strafeRight(r);
    }
}

So there. the entire initalizing and the controling of the camera. If you need anything else I'll post it. If it makes it easier I can zip the entire source and upload it.
The statement below is true.

The statement above is false.

*

Offline abcdef

  • ****
  • 320
Re: Meshes are not drawing.
« Reply #3 on: October 03, 2011, 21:52:56 »
do you load

GL11.glLoadIdentity();

after

GL11.glMatrixMode(GL11.GL_MODELVIEW);

(I am assuming you do the camera stuff after this)

also do you load

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();

at the beginning of each render cycle?

And lastly do you move you camera to a negative z position to start with?

Re: Meshes are not drawing.
« Reply #4 on: October 04, 2011, 00:04:32 »
The default starting position is -10 0 -10.

The script I posted is in the exact order that all of them are called. I will supply the full code, although it has changed a bit. I made a .obj parser and was trying to use that to render cubes and spheres and stuff, using the script in a different language I was able to import the mesh no problem so the OBJ reader should work if I could get any rendering to work.

Hope this answers all questions you may have and helps people find out what's wrong.
The statement below is true.

The statement above is false.

*

Offline abcdef

  • ****
  • 320
Re: Meshes are not drawing.
« Reply #5 on: October 04, 2011, 06:25:45 »
I ran your code but I didn't have your obj's or the slick library so I commented out the font code and the below code in the while loop

Code: [Select]
//Render 5 Diagonal Cubes
cube.render( 4F,  6F,  4F);
cube.render( 2F,  6F,  2F);
cube.render( 0F,  6F,  0F);
cube.render(-2F,  6F, -2F);
cube.render(-4F,  6F, -4F);

I could see the floor you drew so it rendered the quad ok. It was just a white square to me

I changed the camera to be

FPSCameraControl fps = new FPSCameraControl(10F, 5F, 50F);

but that was to only see the whole quad.

So to me I can see the quad floor fine. I'm not sure what the problem is now?

Re: Meshes are not drawing.
« Reply #6 on: October 04, 2011, 10:50:56 »
Now that is strange. I know my computer doesn't have any problem with LWJGL because I play a few games written in it, and they render just fine. I don't get it.

Thank you though.
The statement below is true.

The statement above is false.

Re: Meshes are not drawing.
« Reply #7 on: October 04, 2011, 11:00:22 »
The problem was with the lighting. As seeing I had added no lights, and had messed up on the draw text, It had GL11.glEnable(GL11.GL_LIGHTING); everything was just pitch dark, not that they were not drawing. Thank you for your help, the process you did helped me figure out the problem by isolating what part of the script actually caused it (the draw text).

PS, you did not need to set the controller to -10 5 -50 because you can use WASD to move around. forgot to tell you that. Now I got to fix the problem that everything is inverted.
The statement below is true.

The statement above is false.