Hello Guest

Texture problem

  • 9 Replies
  • 5833 Views
Texture problem
« 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

Code: [Select]
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);
}
}


Code: [Select]
#
# 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.
#
« Last Edit: June 06, 2018, 22:23:32 by Doryan Bessiere »
French Java Developer - 14 years
Twitter: @BDoryan_

*

Andrew Alfazy

Re: Texture problem
« Reply #1 on: June 06, 2018, 23:14:24 »
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".

Re: Texture problem
« Reply #2 on: June 07, 2018, 10:21:56 »
French Java Developer - 14 years
Twitter: @BDoryan_

*

Andrew Alfazy

Re: Texture problem
« Reply #3 on: June 07, 2018, 11:21:48 »
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)
Code: [Select]
glfwMakeContextCurrent(window);
GL.createCapabilities();
You can read this 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
« Last Edit: June 07, 2018, 11:25:14 by Andrew Alfazy »

Re: Texture problem
« Reply #4 on: June 07, 2018, 11:23:45 »
Code: [Select]
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();
}
French Java Developer - 14 years
Twitter: @BDoryan_

*

Andrew Alfazy

Re: Texture problem
« Reply #5 on: June 07, 2018, 11:30:11 »
please and the code where you do
Code: [Select]
@override
public void render(){
//some codes....
}

Re: Texture problem
« Reply #6 on: June 07, 2018, 11:33:21 »
Code: [Select]
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();
}
}
French Java Developer - 14 years
Twitter: @BDoryan_

*

Andrew Alfazy

Re: Texture problem
« Reply #7 on: June 07, 2018, 11:52:37 »
your problem is in this code PLACE
Code: [Select]
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
Code: [Select]
while(!glfwWindowShouldClose(this.window)) {and after
Code: [Select]
GL.createCapabilities();and make your
Code: [Select]
Texture object at
Code: [Select]
Starter class not on
Code: [Select]
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
Code: [Select]
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();
}
Code: [Select]
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?

Re: Texture problem
« Reply #8 on: June 07, 2018, 11:59:59 »
it's good solution and my email it's: antox.doryan@gmail.com
French Java Developer - 14 years
Twitter: @BDoryan_

Re: Texture problem
« Reply #9 on: June 07, 2018, 15:15:23 »