LWJGL Forum
Programming => OpenGL => Topic started by: Doryan Bessiere on June 06, 2018, 22:15:33
-
Hello, i have a problem with LWJGL 3. i have followed this tutorial for creating my game 2D (https://www.youtube.com/watch?v=crOzRjzqI-o) but i have created a texture class and i use this class and I have a problem when I try of the used that takes out to me the error below
Bonjour, j'ai un problème avec LWJGL 3. J'ai suivis ce tutoriel pour créer mon jeu 2D (https://www.youtube.com/watch?v=crOzRjzqI-o) mais j'ai créer une classe texture et j'utilise cette classe et j'ai un problème quand j'essaie de l'utiliser elle m'écrit dans la console l'erreur ci-dessous
package doryan_bessiere.pong.com;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.BufferUtils;
public class Texture {
protected int id;
protected int width, height;
public Texture(String fileName) {
try {
BufferedImage bImage = ImageIO.read(getClass().getResourceAsStream("/images/" + fileName + ".png"));
this.width = bImage.getWidth();
this.height = bImage.getHeight();
int[] pixels_raw = new int[width * height * 4];
pixels_raw = bImage.getRGB(0, 0, this.width, this.height, null, 0, this.width);
ByteBuffer pixels = BufferUtils.createByteBuffer(this.width * this.height * 4);
for (int j = 0; j < this.height; j++) {
for (int i = 0; i < this.width; i++) {
int pixel = pixels_raw[i * this.width + j];
pixels.put((byte) ((pixel >> 16) & 0xFF)); //RED
pixels.put((byte) ((pixel >> 8) & 0xFF)); //GREEN
pixels.put((byte) ((pixel) & 0xFF)); // BLUE
pixels.put((byte) ((pixel >> 24) & 0xFF)); // ALPHA
}
}
pixels.flip();
this.id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
} catch (IOException e) {
e.printStackTrace();
System.err.println(fileName + " texture not found !");
}
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, id);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
}
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff843d0f46d, pid=7244, tid=0x0000000000001a30
#
# JRE version: Java(TM) SE Runtime Environment (8.0_171-b11) (build 1.8.0_171-b11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [lwjgl_opengl.dll+0xf46d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Bessiere\eclipse-workspace\DoryanBessiere\[LWJGL 3] Pong\hs_err_pid7244.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
-
You didn't post the crash report can you post it.
I mean this file "C:\Users\Bessiere\eclipse-workspace\DoryanBessiere\[LWJGL 3] Pong\hs_err_pid7244.log".
-
https://pastebin.com/2nNCwerh
-
WOW you are reminding me of myself before 2 years!
I guess your problem is that you called opengl command before initialization.
so you must set Texture value after (this codes)
glfwMakeContextCurrent(window);
GL.createCapabilities();
You can read this https://www.khronos.org/opengl/wiki/Common_Mistakes#The_Object_Oriented_Language_Problem (https://www.khronos.org/opengl/wiki/Common_Mistakes#The_Object_Oriented_Language_Problem) to know more.
if you didn't solve your problem can you post your main class code
-
package doryan_bessiere.pong.com;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
public abstract class Window {
protected String title;
protected int width, height;
protected boolean resizable;
protected long window;
public Window(String title, int width, int height, boolean resizable) {
this.title = title;
this.width = width;
this.height = height;
this.resizable = resizable;
}
public void show() {
if(!glfwInit()) {
throw new IllegalStateException("Failed to initialize GLFW!");
}
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
this.window = glfwCreateWindow(this.width, this.height, this.title, 0,0);
if(this.window == 0) {
throw new IllegalStateException("Failed to create window !");
}
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(this.window, (videoMode.width() - this.width) / 2, (videoMode.height() - this.height) / 2);
glfwShowWindow(this.window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
while(!glfwWindowShouldClose(this.window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
render();
glfwSwapBuffers(window);
}
glfwTerminate();
}
public boolean getKey(int key) {
return glfwGetKey(this.width, key) == GLFW_TRUE;
}
public boolean getMouse(int button) {
return glfwGetMouseButton(this.width, button) == GLFW_TRUE;
}
public void close() {
glfwSetWindowShouldClose(window, true);
}
public abstract void render();
}
-
please and the code where you do
@override
public void render(){
//some codes....
}
-
package doryan_bessiere.pong.com;
import static org.lwjgl.opengl.GL11.*;
public class Starter {
public static void main(String[] args) {
new Window("[LWJGL 3] Pong", 640, 480, true) {
Texture texture = new Texture("head");
@Override
public void render() {
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-0.5f, 0.5f);
glTexCoord2f(0,1);
glVertex2f(0.5f, 0.5f);
glTexCoord2f(1,1);
glVertex2f(0.5f, -0.5f);
glTexCoord2f(1,0);
glVertex2f(-0.5f, -0.5f);
glEnd();
}
}.show();
}
}
-
your problem is in this code PLACE
Texture texture = new Texture("head");
to solve it make it you can make an other abstract method at your Window class and call it once before while(!glfwWindowShouldClose(this.window)) {
and after GL.createCapabilities();
and make your Texture
object at Starter
class not on new Window("[LWJGL 3] Pong", 640, 480, true) {
Texture texture = new Texture("head");
//some code
}
and give it a value in the new abstract method at Window
your final code will look like this
package doryan_bessiere.pong.com;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
public abstract class Window {
protected String title;
protected int width, height;
protected boolean resizable;
protected long window;
public Window(String title, int width, int height, boolean resizable) {
this.title = title;
this.width = width;
this.height = height;
this.resizable = resizable;
}
public void show() {
if(!glfwInit()) {
throw new IllegalStateException("Failed to initialize GLFW!");
}
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
this.window = glfwCreateWindow(this.width, this.height, this.title, 0,0);
if(this.window == 0) {
throw new IllegalStateException("Failed to create window !");
}
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(this.window, (videoMode.width() - this.width) / 2, (videoMode.height() - this.height) / 2);
glfwShowWindow(this.window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
init();
while(!glfwWindowShouldClose(this.window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
render();
glfwSwapBuffers(window);
}
glfwTerminate();
}
public boolean getKey(int key) {
return glfwGetKey(this.width, key) == GLFW_TRUE;
}
public boolean getMouse(int button) {
return glfwGetMouseButton(this.width, button) == GLFW_TRUE;
}
public void close() {
glfwSetWindowShouldClose(window, true);
}
public abstract void render();
public abstract void init();
}
public class Starter {
public static Texture texture;
public static void main(String[] args) {
new Window("[LWJGL 3] Pong", 640, 480, true) {
@Override
public void init(){
texture = new Texture("head");
}
@Override
public void render() {
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-0.5f, 0.5f);
glTexCoord2f(0,1);
glVertex2f(0.5f, 0.5f);
glTexCoord2f(1,1);
glVertex2f(0.5f, -0.5f);
glTexCoord2f(1,0);
glVertex2f(-0.5f, -0.5f);
glEnd();
}
}.show();
}
}
and there are to many solutions to fix this it's simple one.
Can you give me an email to connect with you?
-
it's good solution and my email it's: antox.doryan@gmail.com
-
I advise you to use these tutorial first:
- https://legacy.gitbook.com/book/lwjglgamedev/3d-game-development-with-lwjgl/details (https://legacy.gitbook.com/book/lwjglgamedev/3d-game-development-with-lwjgl/details)
- https://learnopengl.com/ (https://learnopengl.com/)