Hi all.
Just starting out, been at this for some time now and I am having problems with drawing shapes on my newly created window. I am using LWJGL 3 stable on Fedora Linux.
The entire code is down bellow, I am not expecting anyone to go through the whole thing, but I figured I would post it anyway just in case.
Problem:
In MasterRenderer class in method render() if I put GL_POINTS instead of GL_TRIANGLES I get a dot in the middle of the screen, if I leave GL_TRIANGLES I get nothing as nothing is shown, there are no errors, I made sure that all methods are being called in proper order or I am pretty sure of it in any case, inserted a tone of print outs and just went one method at a time to make sure that things are getting initialized and called.
I 've been following this tutorial:
Could you please help me out, I am just starting.
Thank you all for your time and effort.
Window.java
package com.base.engine;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Window
{
private long window;
public Window(int width, int height, String title)
{
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
glfwWindowHint(GLFW_DECORATED, GL_TRUE);
glfwWindowHint(GLFW_FOCUSED, GL_TRUE);
window = glfwCreateWindow(width, height, title, NULL, NULL);
glfwMakeContextCurrent(window);
}
public void dispose()
{
glfwDestroyWindow(window);
}
public void hide()
{
glfwHideWindow(window);
}
public void render()
{
glfwSwapBuffers(window);
}
public void show()
{
glfwShowWindow(window);
}
public void setTitle(String title)
{
glfwSetWindowTitle(window, title);
}
public boolean shouldClose()
{
if(glfwWindowShouldClose(window))
return true;
return false;
}
}
MainComponent.java
package com.base.engine;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.glGetError;
import static org.lwjgl.glfw.Callbacks.*;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
public class MainComponent
{
private GLFWErrorCallback errorCallback;
private MasterRenderer renderer;
private Window window;
private boolean running = false;
public MainComponent()
{
init();
renderer = new MasterRenderer();
}
private void input()
{
glfwPollEvents();
if(window.shouldClose())
stop();
}
private void update()
{
}
private void render()
{
renderer.render();
window.render();
}
private void run()
{
long lastTime = System.nanoTime();
long curTime = lastTime;
long timer = System.currentTimeMillis();
double ns = 1000000000 / 60;
double delta = 0.0;
int fps = 0;
int ups = 0;
while(running)
{
curTime = System.nanoTime();
delta += (curTime - lastTime) / ns;
lastTime = curTime;
while(delta >= 1.0)
{
input();
update();
ups++;
delta--;
}
render();
fps++;
if(System.currentTimeMillis() > timer + 1000)
{
window.setTitle("Tutorial - " + "ups: " + ups + " | fps: " + fps);
ups = 0;
fps = 0;
timer += 1000;
}
}
cleanUp();
}
public void start()
{
if(running)
return;
running = true;
run();
}
public void stop()
{
if(!running)
return;
running = false;
}
private void init()
{
glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
glfwInit();
window = new Window(1280, 720, "Tutorial");
GL.createCapabilities();
}
private void cleanUp()
{
window.hide();
renderer.cleanUp();
window.dispose();
}
public int test()
{
int b = 2;
int j = "JYY".hashCode() % 3000;
int h = "QPM".hashCode() % 3000;
for (int s = 0; s <= h; s++)
b = (b ^ s) % j;
return b;
}
public static void main(String[] args)
{
MainComponent main = new MainComponent();
//main.start();
System.out.println(main.test());
}
}
MasterRenderer.java
package com.base.engine;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
public class MasterRenderer
{
private Model model;
public MasterRenderer()
{
init();
model = new Model();
Vertex[] vertices = {
new Vertex( -1,-1,0),
new Vertex( 1,-1,0),
new Vertex( 0,1,0)
};
model.bufferVertices(vertices);
}
public void render()
{
prepare();
glBindBuffer(GL_ARRAY_BUFFER, model.getVBO());
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
glDrawArrays(GL_POINTS, 0, model.getSize());
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void prepare()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
private void init()
{
glEnable(GL_DEPTH_TEST);
}
public void cleanUp()
{
}
}
Model.java
package com.base.engine;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
public class Model
{
private int vbo;
private int size;
public int getS()
{
return size;
}
public Model()
{
vbo = glGenBuffers();
size = 0;
}
public void bufferVertices(Vertex[] vertices)
{
FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length * Vertex.SIZE);
for(Vertex vertex : vertices)
{
buffer.put(vertex.getX());
buffer.put(vertex.getY());
buffer.put(vertex.getZ());
}
buffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
size = vertices.length;
}
public int getVBO() {
return vbo;
}
public int getSize() {
return size;
}
}
Vertex.java
package com.base.engine;
public class Vertex
{
public static final int SIZE = 3;
private float x;
private float y;
private float z;
public Vertex(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
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;
}
}