0

Hi, I've been studying OPENGL and 3D programming. I've made kind of 3D engine with basic movement, no collision detection yet. Now theres something really wrong in my code. When I go near rendered QL_QUADS framerate drops to 11-12 and allover my framerate is too low. Any suggestions?

Core.java

package net.viped.vengine;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Cursor;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Core {

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    private Camera cam;
    World world =new World();
    Cursor emptyCursor;
    public Core() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.setTitle("vEngine 0.05 || Firs 3D Engine");
//          Display.setVSyncEnabled(true);
            Display.setResizable(false);

            Display.create();
            init();

            world.createTestWorld();
            long lastTime = System.nanoTime();
            int frames = 0;
            while (!Display.isCloseRequested()) {
                tick();
                render();
                frames++;
                if (System.nanoTime() - lastTime > 1000000000) {
                    System.out.println(frames);
                    frames = 0;
                    lastTime = System.nanoTime();                   
                }
                Display.update();
            }

            Display.destroy();
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(0);
        }
    }

    public void init() {
        Mouse.setGrabbed(true);
        cam = new Camera(70, (float) (WIDTH / HEIGHT), 0.3f, 1000);

    }

    public void tick() {    
        input();
    }

    public void input() {
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            cam.move(0.1f, 1);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            cam.move(-0.1f, 1);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            cam.move(0.1f, 0);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            cam.move(-0.1f, 0);
        }
        cam.rotateX(-Mouse.getDY());
        cam.rotateY(Mouse.getDX());
    }


    public void render() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        cam.useView();

        glTranslatef(0, 0, -20);

        world.renderTestWorld();



    }

    public static void main(String[] args) {
        new Core();
    }
}

Camera.java

package net.viped.vengine;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

public class Camera {
    private float x, y, z;
    private float rx, ry, rz;

    private float fov;
    private float aspect;
    private float near;
    private float far;

    boolean flymode = true;

    float speed = 0.05f;

    public Camera(float fov, float aspect, float near, float far) {
        x = 0;
        y = 0;
        z = 0;
        rx = 0;
        ry = 0;
        rz = 0;

        this.fov = fov;
        this.aspect = aspect;
        this.near = near;
        this.far = far;
        initProjection();
    }

    private void initProjection() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, aspect, near, far);
//      glOrtho(0, Core.WIDTH, Core.HEIGHT, 0, 1, -1000);
        glMatrixMode(GL_MODELVIEW);

        glEnable(GL_DEPTH_TEST);
    }

    public void useView() {
        glRotatef(rx, 1, 0, 0);
        glRotatef(ry, 0, 1, 0);
        glRotatef(rz, 0, 0, 1);
        glTranslatef(x, y, z);
    }

    public void move(float amount, float dir) {
        z += amount * Math.sin(Math.toRadians(ry + 90 * dir));
        x += amount * Math.cos(Math.toRadians(ry + 90 * dir));
        //FLYING
        if (flymode) {
            y += amount * Math.sin(Math.toRadians(rx));
        }
    }

    public void rotateY(float amount) {
        ry += amount;

    }

    public void rotateX(float amount) {
        rx += amount;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getZ() {
        return z;
    }

    public void setZ(float z) {
        this.z = z;
    }

    public float getRx() {
        return rx;
    }

    public void setRx(float rx) {
        this.rx = rx;
    }

    public float getRy() {
        return ry;
    }

    public void setRy(float ry) {
        this.ry = ry;
    }

    public float getRz() {
        return rz;
    }

    public void setRz(float rz) {
        this.rz = rz;
    }

    public boolean isFlymode() {
        return flymode;
    }

    public void setFlymode(boolean flymode) {
        this.flymode = flymode;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }   
}

World.java

package net.viped.vengine;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.util.Random;

public class World {

    public World() {

    }

    int stars = 10000;
    Star[] starsA = new Star[stars];
    int displayListHandle;

    public void createTestWorld() {
        for (int i = 0; i < stars; i++) {
            Random rand = new Random();
            starsA[i] = new Star((rand.nextFloat() - 0.5f) * 100f, (rand.nextFloat() - 0.5f) * 100f, rand.nextInt(200) - 200);
        }

        displayListHandle = glGenLists(1);

        glNewList(displayListHandle, GL_COMPILE);
        glBegin(GL_QUADS);
        glVertex3f(-1.0f, -1.0f, 1);
        glVertex3f(1.0f, -1.0f, 1);
        glVertex3f(1.0f, 1.0f, 100);
        glVertex3f(-1.0f, 1.0f, 100);
        glEnd();
        glEndList();
    }

    public void renderTestWorld() {
        for (Star s : starsA) {
            Random rand = new Random();
            glBegin(GL_POINTS);
            {
                glVertex3f(s.x, s.y, s.z);
            }
            glEnd();

            glCallList(displayListHandle);

        }
    }

    private class Star {
        float x, y, z;

        public Star(float x, float y, float z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}
1
Contributor
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Viped
0

And again I should check the code few more times berfore sending question. I was rendering gl_quads inside for loop with the stars.

Sorry for stupid thread.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.