Newbie Question Triangle doesn't show up

Started by Kudze, February 04, 2016, 14:27:57

Previous topic - Next topic

Kudze

Ok So I'm trying to folow these tutorials: https://www.youtube.com/playlist?list=PLEETnX-uPtBXP_B2yupUKlflXBznWIlL5 I'm on episode 8th now. And he did triangle I did my triangle code too but when I start game all Fuction works (glClearScreen i mean and it prints my opengl version which is 4.5.13416) But I can't see triangle on my Screen Here's code:

P.S. I know that tutorials are on LWJGL 2 but I'm trying to follow it in LWJGL 3 I wrote my window class my own not from that tutorial so mby there's something wrong with it


My game class
package Game;

import Input.Input;
import Input.InputInterface;
import Render.Mesh;
import Math.Vector3;
import Render.Vertex;

/**
 * Created by PC on 2016-02-03.
 */
public class Game {

    private Mesh mesh;

    private long windowID;
    private Input input;

    public Game(long windowID) {
        this.windowID = windowID;

        input = new Input(windowID, new InputInterface() {

            public void onKeyboardClick(int KeyboardCode){}
            public void onKeyboardRelease(int KeyboardCode){}

            public void onMouseClick(int MouseCode){}
            public void onMouseRelease(int MouseCode){}

            public void onCursorMove(int x, int y){}

        });

        mesh = new Mesh();

        Vertex[] data = new Vertex[] {new Vertex(new Vector3(-1, -1, 0)),
                                      new Vertex(new Vector3(-1, 1, 0)),
                                      new Vertex(new Vector3(0, 1, 0))};


        mesh.addVertex(data);
    }

    public void update() {

    }

    public void input() {
        input.update();


    }

    public void render() {
        mesh.draw();
    }

}


my Mesh class

package Render;

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

public class Mesh {

    private int vbo;
    private int size;

    public Mesh() {
        vbo = glGenBuffers();
        size = 0;
    }

    public void addVertex(Vertex[] vertex) {
        size = vertex.length;
        //AddData
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertex), GL_STATIC_DRAW);
    }

    public void draw() {
        glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0, 3, GL_FLAT, false, Vertex.SIZE * 4, 0);
        glDrawArrays(GL_TRIANGLES, 0, size);

        glDisableVertexAttribArray(0);
    }

}


my Vertex Class

package Render;

import Math.Vector3;

public class Vertex {

    public static final int SIZE = 3;

    private Vector3 pos;

    public Vertex(Vector3 pos) {
        this.pos = pos;
    }

    public Vector3 getPos() {
        return pos;
    }

    public void setPos(Vector3 pos) {
        this.pos = pos;
    }
}


and my Vector3 class

package Math;

public class Vector3 {
	
	public float x = 0.0f;
	public float y = 0.0f;
	public float z = 0.0f;
	
	public Vector3(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public Vector3() {}

	public float lenght() {
		return (float)Math.sqrt(x*x+ y*y + z*z);
	}

	public float dot(Vector3 r) {
		return x * r.x + y * r.y + z * r.z;
	}

	public Vector3 Cross(Vector3 r) {
		float x_ = y * r.z - z * r.y;
		float y_ = z * r.x - x * r.z;
		float z_ = x * r.y - y * r.x;

		return new Vector3(x_,y_,z_);
	}

	public Vector3 Normalized(){
		float length = lenght();

		x /= length;
		y /= length;
		z /= length;

		return this;
	}

	public Vector3 Rotate() {
		return null;
	}

	public Vector3 add(Vector3 r) {
		return new Vector3(x + r.x, y + r.y, z + r.z);
	}

	public Vector3 add(float r) {
		return new Vector3(x + r, y + r, z + r);
	}

	public Vector3 min(Vector3 r) {
		return new Vector3(x - r.x, y - r.y, z - r.z);
	}

	public Vector3 min(float r) {
		return new Vector3(x - r, y - r, z - r);
	}

	public Vector3 mul(Vector3 r) {
		return new Vector3(x * r.x, y * r.y, z * r.z);
	}

	public Vector3 mul(float r) {
		return new Vector3(x * r, y * r, z * r);
	}

	public Vector3 div(Vector3 r) {
		return new Vector3(x / r.x, y / r.y, z / r.z);
	}

	public Vector3 div(float r) {
		return new Vector3(x / r, y / r, z / r);
	}

}


My Main game loop class

package Render;

import Game.Game;


public class Main {

    private static Window gameWindow;

    private boolean isRunning;

    private Game game;

    public static final double FRAME_CAP = 420.0d;

    public Main() {
        RenderUtil.initGraphics();
        isRunning = false;
        game = new Game(gameWindow.getWindowID());

    }

    public void start() {

        if(isRunning)
            return;

        run();

    }

    public void stop() {

        if(!isRunning)
            return;


        isRunning = false;

    }

    private void run() {

        isRunning = true;

        final double FrameTime = 1.0 / FRAME_CAP;

        long LastTime = Time.getTime();
        double unprocessedTime = 0;

        int frames = 0;
        long frameCounter = 0;


        while(isRunning) {

            boolean render = false;

            long startTime = Time.getTime();
            long passedTime = startTime - LastTime;
            LastTime = startTime;

            unprocessedTime += passedTime / (double) Time.SECOND;
            frameCounter += passedTime;

            while(unprocessedTime > FrameTime) {

                render = true;
                frames++;

                unprocessedTime -= FrameTime;

                if(!gameWindow.ShouldClose())
                    stop();


                game.input();
                game.update();

                if(frameCounter >= Time.SECOND) {
                    System.out.println(frames);
                    frames = 0;
                    frameCounter = 0;
                }
            }

            if(render)render();
            else {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

        dispose();

    }

    private void render() {
        RenderUtil.clearScreen();
        game.render();
        gameWindow.update();

    }

    private void dispose() {

        gameWindow.exitDisplay();

    }


    public static void main(String[] args) {

        gameWindow = new Window(1280, 720, "Spray&Pray PC version!", 0);

        Main game = new Main();
        game.start();


    }

}


And my window class I spent tons of time in it :3

package Render;

import Input.Input;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;

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

public class Window {
	
	private GLFWErrorCallback error;
	private long windowID;
	private String windowName;
	private int windowWidth;
	private int windowHeight;
	private int isFullScreen;
	//isFullScreen Details:
	// 0 - windowed
	// 1 - Borderless
	// 2 - FullScreen
	private long monitor;
	
	public Window(int width, int height, String windowName, int isFullScreen) {
		this.windowName = windowName;
		this.windowWidth = width;
		this.windowHeight = height;
		this.isFullScreen = isFullScreen;
		
		initDisplay();
		
	}
	
	public long getWindowID() { return this.windowID; }
	public int getWindowWidth() { return this.windowWidth; }
	public int getWindowHeight() { return this.windowHeight; }
	public String getWindowName() { return this.windowName; }
	public int getWindowIsFullScreen() { return this.isFullScreen; }
	public long getMonitorID() { return this.monitor; }
	
	public void setWindowSize(int width, int height) {
		windowWidth = width;
		windowHeight = height;
		glfwSetWindowSize(windowID, windowWidth, windowHeight);
	}
	
	public void setWindowName(String windowName) {
		this.windowName = windowName;
		glfwSetWindowTitle(windowID, this.windowName);
	}
	
	public void setWindowIsFullScreen(int isFullScreen) {
		//Simply recreates window with Same values expect FullScreen
		this.isFullScreen = isFullScreen;
		
		glfwDestroyWindow(windowID);
		
		initDisplay();
	}

	public boolean ShouldClose() { return glfwWindowShouldClose(this.windowID) == GL_FALSE; }
	
	private void initDisplay() throws IllegalStateException {
		error = GLFWErrorCallback.createPrint(System.err);
		glfwSetErrorCallback(error);
		
		int glfwResult = glfwInit();
		if(glfwResult == GL_FALSE) {
			throw new IllegalStateException("GLFW initilization wasn't succesful!");
		} else {
			System.out.println("GLFW is enabled!");
		}
		
		//Variables that needs glfw turned on
		this.monitor = glfwGetPrimaryMonitor();
		
		//Workaround that window won't be resizeable
		glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
		
		
		//creates window
		if(this.isFullScreen == 1) {
			//Creates borderless
			
			glfwWindowHint(GLFW_DECORATED, GL_FALSE);
			
			this.windowID = glfwCreateWindow(this.windowWidth, this.windowHeight, this.windowName, MemoryUtil.NULL, MemoryUtil.NULL);
			
			//Sets position in middle
			
		}
		if(this.isFullScreen == 2) {
			//Creates fullscreen version
			this.windowID = glfwCreateWindow(this.windowWidth, this.windowHeight, this.windowName, this.monitor , MemoryUtil.NULL);
		}
		if(this.isFullScreen == 0){
			//Creates windowed version
			this.windowID = glfwCreateWindow(this.windowWidth, this.windowHeight, this.windowName, MemoryUtil.NULL, MemoryUtil.NULL);
		}
		
		if(windowID == MemoryUtil.NULL) {
			throw new IllegalStateException("Window failed to launch!");
		} else {
			System.out.println("Window: '" + this.windowName + "' is enabled!");
		}
		
		//Nustato thread�
		glfwMakeContextCurrent(this.windowID);
		//Frameratas
		glfwSwapInterval(1);
		
		//Perstato vaizdo viet� jeigu yra borderless
		if(this.isFullScreen == 1) {
			setMiddlePosition();
		}
		
		glfwShowWindow(this.windowID);
		
		initOpenGL();
		
	}
	
	private void initOpenGL() {
		GL.createCapabilities();
		
		System.out.println("OpenGL version: " + glGetString(GL_VERSION));
	}

	public void update() {
		//Swaps Frames
		glfwSwapBuffers(windowID);
	}
	
	public void exitDisplay() {
		glfwDestroyWindow(windowID);
		glfwTerminate();
	}
	
	private void setMiddlePosition() {
		GLFWVidMode monitor = glfwGetVideoMode(this.monitor);
		
		int x = (monitor.width()/2)-(this.windowWidth/2);
		int y = (monitor.height()/2)-(this.windowHeight/2);
		
		glfwSetWindowPos(this.windowID, x, y);
	}

}


I'm sure that render class get's called because i did some debug with System.out.println

Joona

Hello,

I spotted at least one mistake that might be the cause:
glVertexAttribPointer(0, 3, GL_FLAT, false, Vertex.SIZE * 4, 0);

It's supposed to be GL_FLOAT, not flat ;)

Also, as Benny says in the eight video, you may need shaders (from the next video) to actually produce anything on the screen.

Kudze

Thank you! :) I changed from GL_FLAT to GL_FLOAT and it worked ;D I don't know how it slipped :)