Hello, I am fairly new to OpenGL,
But I have decided to start learning it.
The only problem with that is…
Whenever I try to do something, such as,
Creating a square, it does not work.
It shows me a black screen.
I have been through tutorial’s on YouTube, and they are all giving me the same output:
not working, black screen.
It does allow me to change the Window Title, Size, but it will not let me do anything inside of the window itself.
Here’s All of the code I have at the moment for it,
// THIS IS THE MAIN CLASS (CUBOID)
package co.cuboidgame;
public class Cuboid {
static InfoManager info = InfoManager.getInstance();
public static void log(String message) {
System.out.println("[Cuboid] " + message);
}
public static void logMouse(int x, int y) {
System.out.println("[Cuboid] X:" + x);
System.out.println("[Cuboid] Y:" + y);
System.out.println(" ");
}
public static void main(String[] args) {
log("Loading " + info.getName() + info.getversion() + "...");
new Cuboid();
new DisplayRegisterer();
log("Closing " + info.getName() + info.getversion() + "...");
}
public Cuboid() {
}
}
// THIS IS THE INPUT LISTENER CLASS, WHICH CONTAINS THE WHILE (!Display.isCloseRequested)
package co.cuboidgame;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class DisplayRegisterer{
InfoManager info = InfoManager.getInstance();
public DisplayRegisterer() {
Display.setTitle(info.getName() + info.getVersion());
try {
Display.create();
Display.setDisplayMode(new DisplayMode(600, 400));
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 600, 400, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Cuboid.log("Successfully Loaded...");
if (glGetError() == GL_NO_ERROR) {
while (!(Display.isCloseRequested())) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE);
glVertex2f(0, 100);
glVertex2f(50, 100);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f); // Red, I think.
glVertex2f(0, 0);
glVertex2f(50, 0);
glVertex2f(50, 50);
glVertex2f(0, 50);
glEnd();
Cuboid.logMouse(Mouse.getX(), Mouse.getY());
Display.update();
Display.sync(60);
}
Display.destroy();
} else {
Cuboid.log("[Error] There seems to be an issue with the code.");
}
}
}
// THIS IS THE INFO MANAGER CLASS, JUST HANDLES THE VERSION, NAME, ETC.
package co.cuboidgame.managers;
import co.cuboidgame.Cuboid;
public class InfoManager {
String ver = “0.0.1”;
String name = “Cuboid”;
String dev = “Corey Reynolds”;
String mcuserdev = “yeroC”;
Cuboid plugin;
boolean snapshot = false;
boolean beta = false;
boolean alpha = true;
public InfoManager(Cuboid plugin) {
this.plugin = plugin;
}
public InfoManager() {
}
public static InfoManager instance = new InfoManager();
public static InfoManager getInstance() {
return instance;
}
public String getVersion() {
if (snapshot == false && beta == false && alpha == false) {
return " v" + ver;
}
if (snapshot == true && beta == true && alpha == true) {
Cuboid.log("Snapshot, Beta and Alpha settings are all set to true.");
return " v" + ver;
}
if (snapshot == true && beta == false && alpha == false) {
return " v" + ver + "-SNAPSHOT";
}
if (beta == true && snapshot == false && alpha == false) {
return " v" + ver + "-BETA";
}
if (alpha = true && snapshot == false && beta == false) {
return " v" + ver + "-ALPHA";
}
Cuboid.log("More than one version setting is enabled.");
return " v" + ver;
}
public String getName() {
return name;
}
public String getDeveloper() {
return dev;
}
public String getMCUserDev() {
return mcuserdev;
}
}
If you would like to see the window that pops up
(literally a window with a black screen)
Then feel free to message my Skype (coreyrgr) otherwise, thanks.
Help would be greatly appreciated, thanks!
Second Method that still fails to work:
// CUBOID (main class)
package co.cuboidgame;
import org.lwjgl.opengl.Display;
import co.cuboidgame.managers.InfoManager;
import co.cuboidgame.renderEngine.managers.DisplayManager;
import co.cuboidgame.renderEngine.models.Loader;
import co.cuboidgame.renderEngine.models.RawModel;
public class Cuboid {
public static void log(String message) {
System.out.println("[Cuboid] " + message);
}
static InfoManager info = InfoManager.getInstance();
public static void main(String[] args) {
log("Loading " + info.getName() + info.getVersion() + "...");
new Cuboid();
log("Closing " + info.getName() + info.getVersion() + "...");
}
public Cuboid() {
DisplayManager.createDisplay();
Loader loader = new Loader();
Renderer renderer = new Renderer();
float[] vertices = { -0.5f, 0.5f, 0f, -0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, 0.5f, 0f, -0.5f,
0.5f, 0f };
RawModel model = loader.loadToVAO(vertices);
while (!(Display.isCloseRequested())) {
Display.setTitle(info.getName() + info.getVersion());
renderer.prepare();
renderer.render(model);
DisplayManager.updateDisplay();
}
loader.cleanUp();
DisplayManager.closeDisplay();
}
}
// Display Manager
package co.cuboidgame.renderEngine.managers;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
import co.cuboidgame.managers.InfoManager;
public class DisplayManager {
private static final int WIDTH = 720;
private static final int HEIGHT = 480;
private static final int FPS_CAP = 120;
static InfoManager info = InfoManager.getInstance();
public static void createDisplay() {
ContextAttribs attribs = new ContextAttribs(3, 3).withForwardCompatible(true).withProfileCore(true);
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create(new PixelFormat(), attribs);
Display.setTitle(info.getName() + info.getVersion());
} catch (LWJGLException e) {
e.printStackTrace();
}
GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public static void updateDisplay() {
Display.sync(FPS_CAP);
Display.update();
}
public static void closeDisplay() {
Display.destroy();
}
}
// Info Manager
package co.cuboidgame.managers;
import co.cuboidgame.Cuboid;
public class InfoManager {
String ver = “0.0.1”;
String name = “Cuboid”;
String dev = “Corey Reynolds”;
String mcuserdev = “yeroC”;
Cuboid plugin;
boolean snapshot = false;
boolean beta = false;
boolean alpha = true;
public InfoManager(Cuboid plugin) {
this.plugin = plugin;
}
public InfoManager() {
}
public static InfoManager instance = new InfoManager();
public static InfoManager getInstance() {
return instance;
}
public String getVersion() {
if (snapshot == false && beta == false && alpha == false) {
return " v" + ver;
}
if (snapshot == true && beta == true && alpha == true) {
Cuboid.log("Snapshot, Beta and Alpha settings are all set to true.");
return " v" + ver;
}
if (snapshot == true && beta == false && alpha == false) {
return " v" + ver + "-SNAPSHOT";
}
if (beta == true && snapshot == false && alpha == false) {
return " v" + ver + "-BETA";
}
if (alpha = true && snapshot == false && beta == false) {
return " v" + ver + "-ALPHA";
}
Cuboid.log("More than one version setting is enabled.");
return " v" + ver;
}
public String getName() {
return name;
}
public String getDeveloper() {
return dev;
}
public String getMCUserDev() {
return mcuserdev;
}
}
// Loader
package co.cuboidgame.renderEngine.models;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Loader {
private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();
public RawModel loadToVAO(float[] positions) {
int vaoID = createVAO();
storeDataInAttributeList(0, positions);
unbindVAO();
return new RawModel(vaoID, positions.length / 3);
}
public void cleanUp() {
for (int vao : vaos) {
GL30.glDeleteVertexArrays(vao);
}
for (int vbo : vbos) {
GL15.glDeleteBuffers(vbo);
}
}
private int createVAO() {
int vaoID = GL30.glGenVertexArrays();
vaos.add(vaoID);
GL30.glBindVertexArray(vaoID);
return vaoID;
}
private void storeDataInAttributeList(int attributeNumber, float[] data) {
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
FloatBuffer buffer = storeDataInFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbindVAO() {
GL30.glBindVertexArray(0);
}
private FloatBuffer storeDataInFloatBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
}
// RawModel
package co.cuboidgame.renderEngine.models;
public class RawModel {
private int vaoID;
private int vertexCount;
public RawModel(int vaoID, int vertexCount) {
this.vaoID = vaoID;
this.vertexCount = vertexCount;
}
public int getVaoID() {
return vaoID;
}
public int getVertexCount() {
return vertexCount;
}
}
// Renderer
package co.cuboidgame;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import co.cuboidgame.renderEngine.models.RawModel;
public class Renderer {
public void prepare() {
GL11.glClearColor(1, 0, 0, 1);
}
public void render(RawModel model) {
GL30.glBindVertexArray(model.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
}
// I am so sorry for all the random code all over the place. I have no idea how to add a code block in (like on the bukkit forums) – if that’s even possible here. but, yeah, sorry.
Anyways. Thanks if you could still follow on although It’s all so messy!