SOLVED: stbimage not rendering properly

Started by Animus_Surge, December 03, 2019, 00:56:09

Previous topic - Next topic

Animus_Surge

Just a quick side note: I know there is probably a solution to this already, but I don't want to dig through thousands (or millions) of posts just to find it.

I am trying to render an image to a blank screen using the following code: (some of this acquired from here: http://forum.lwjgl.org/index.php?topic=6375.msg33962#msg33962)
import static org.lwjgl.opengl.GL11.*;

import com.programwings.wofrpg.util.Coordinate;
import org.lwjgl.BufferUtils;
import org.lwjgl.stb.STBImage;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class Image {

	private int id, width, height;
	private Coordinate[] coordinates;
	private String file;

	public Image(String filename, Coordinate... coordinates) {
		file = filename;
		this.coordinates = coordinates;

		IntBuffer wid = BufferUtils.createIntBuffer(1);
		IntBuffer hei = BufferUtils.createIntBuffer(1);
		IntBuffer com = BufferUtils.createIntBuffer(1);

		ByteBuffer data = STBImage.stbi_load(filename, wid, hei, com, 4);

		id = glGenTextures();
		width = wid.get();
		height = hei.get();

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
		STBImage.stbi_image_free(data);
	}

	public void bind() {
		glBindTexture(GL_TEXTURE_2D, id);
	}

	public void unbind() {
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	public void draw() {
		bind();
		glBegin(GL_QUADS);
		for(Coordinate vert : coordinates) {
			glVertex2d(vert.x, vert.y);
		}
		glEnd();

	}


}


When I run it (I just call the draw method because it works properly when rendering a shape) all that appears is a white square in the center of the screen. I have no idea why, and have no clue how to fix it.

Any help would be greatly appreciated.
Thanks!

spasi

Try inserting a call to bind() after glGenTextures().

KaiHH

One obvious bug is that you are lacking glTexCoord*() calls inside of glBegin/glEnd. So your vertices currently don't have any texture coordinates and OpenGL does not automagically perform UV unwrapping.

Animus_Surge

Hello, thank you for replying.
Quote from: spasi on December 04, 2019, 10:07:29
Try inserting a call to bind() after glGenTextures().

I don't believe that will do anything because bind() is being called in the draw() method which is called in the driver (not shown).

Quote from: KaiHH on December 04, 2019, 10:29:56
One obvious bug is that you are lacking glTexCoord*() calls inside of glBegin/glEnd. So your vertices currently don't have any texture coordinates and OpenGL does not automagically perform UV unwrapping.

Okay, yes I do that, so now the method looks like this:
public void draw() {
	bind();
	glBegin(GL_QUADS);
		glTexCoord2d(0, 0);
		glVertex2d(c1.x, c1.y);
		glTexCoord2d(1, 0);
		glVertex2d(c2.x, c2.y);
		glTexCoord2d(1, 1);
		glVertex2d(c3.x, c3.y);
		glTexCoord2d(0, 1);
		glVertex2d(c4.x, c4.y);
	glEnd();
}


It still did not work (unless I did something wrong.. I'm better at drawing shapes than pictures), so I don't know what to do now.

spasi

Quote from: Animus_Surge on December 04, 2019, 16:41:12I don't believe that will do anything because bind() is being called in the draw() method which is called in the driver (not shown).

There is no implicit binding after a call to glGenTextures(). You generate a texture ID but never initialize it. The subsequent glTexParameterf and glTexImage2D calls either generate an OpenGL error (best-case) or modify a different texture object that is currently bound (worse-case). You should create a debug OpenGL context and register a debug callback. Or use LWJGLX/debug if you're not sure how to do that.

Animus_Surge

Quote from: spasi on December 04, 2019, 16:48:58
There is no implicit binding after a call to glGenTextures(). You generate a texture ID but never initialize it. The subsequent glTexParameterf and glTexImage2D calls either generate an OpenGL error (best-case) or modify a different texture object that is currently bound (worse-case). You should create a debug OpenGL context and register a debug callback. Or use LWJGLX/debug if you're not sure how to do that.

Right. I got the lwjglx thing and it gives me a massive log... most of which I have no idea how to read. Here's the output of it:
[LWJGL] Version: 3.2.3 build 13
[LWJGL] 	 OS: Windows 10 v10.0
[LWJGL] 	JRE: 13.0.1 amd64
[LWJGL] 	JVM: Java HotSpot(TM) 64-Bit Server VM v13.0.1+9 by Oracle Corporation
[LWJGL] Loading JNI library: lwjgl
[LWJGL] 	Module: org.lwjgl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] Loading library: glfw
[LWJGL] 	Module: org.lwjgl.glfw
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\glfw.dll
[LWJGL] Java 9 stack walker enabled
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\glfw.dll
[LWJGL] Loading library: jemalloc
[LWJGL] 	Module: org.lwjgl.jemalloc
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\jemalloc.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\jemalloc.dll
[LWJGL] MemoryUtil allocator: DebugAllocator
{[window|INFO   ]: GLFW initialized.}
{[window|INFO   ]: Window initialized. Creating graphics buffers...}
[LWJGL] Loading JNI library: lwjgl_opengl
[LWJGL] 	Module: org.lwjgl.opengl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_opengl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_opengl.dll
[LWJGL] Loading library: opengl32
[LWJGL] 	Module: org.lwjgl.opengl
[LWJGL] 	opengl32.dll not found in org.lwjgl.librarypath=C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13
[LWJGL] 	Loaded from system paths: C:\WINDOWS\SYSTEM32\opengl32.dll
[LWJGL] Java 10 multiplyHigh enabled
[LWJGL] Java 9 check intrinsics enabled
[LWJGL] Java 10 memcpy enabled
[LWJGL] [GL] Using OpenGL 4.3 for error logging.
[LWJGL] Java 9 text decoding enabled
[info ][1] Initialized OpenGL context for window[1]
  Effective OpenGL version: 4.5
  OpenGL version string   : 4.5.0 - Build 24.20.100.6292
  OpenGL vendor           : Intel
  OpenGL renderer         : Intel(R) HD Graphics 620
  GL_MAX_VERTEX_ATTRIBS   : 16
  Capabilities:
        1      GL_AMD_depth_clamp_separate GL_AMD_vertex_shader_layer 
        2      GL_AMD_vertex_shader_viewport_index GL_ARB_arrays_of_arrays 
        3      GL_ARB_base_instance GL_ARB_bindless_texture GL_ARB_blend_func_extended 
        4      GL_ARB_buffer_storage GL_ARB_cl_event GL_ARB_clear_buffer_object 
        5      GL_ARB_clear_texture GL_ARB_clip_control GL_ARB_color_buffer_float 
        6      GL_ARB_compatibility GL_ARB_compressed_texture_pixel_storage 
        7      GL_ARB_compute_shader GL_ARB_conditional_render_inverted 
        8      GL_ARB_conservative_depth GL_ARB_copy_buffer GL_ARB_copy_image 
        9      GL_ARB_cull_distance GL_ARB_debug_output GL_ARB_depth_buffer_float 
       10      GL_ARB_depth_clamp GL_ARB_depth_texture GL_ARB_derivative_control 
       11      GL_ARB_direct_state_access GL_ARB_draw_buffers GL_ARB_draw_buffers_blend 
       12      GL_ARB_draw_elements_base_vertex GL_ARB_draw_indirect GL_ARB_draw_instanced 
       13      GL_ARB_enhanced_layouts GL_ARB_ES2_compatibility GL_ARB_ES3_1_compatibility 
       14      GL_ARB_ES3_compatibility GL_ARB_explicit_attrib_location 
       15      GL_ARB_explicit_uniform_location GL_ARB_fragment_coord_conventions 
       16      GL_ARB_fragment_layer_viewport GL_ARB_fragment_program 
       17      GL_ARB_fragment_program_shadow GL_ARB_fragment_shader 
       18      GL_ARB_fragment_shader_interlock GL_ARB_framebuffer_no_attachments 
       19      GL_ARB_framebuffer_object GL_ARB_framebuffer_sRGB GL_ARB_geometry_shader4 
       20      GL_ARB_get_program_binary GL_ARB_get_texture_sub_image GL_ARB_gpu_shader5 
       21      GL_ARB_gpu_shader_fp64 GL_ARB_half_float_pixel GL_ARB_half_float_vertex 
       22      GL_ARB_indirect_parameters GL_ARB_instanced_arrays GL_ARB_internalformat_query 
       23      GL_ARB_internalformat_query2 GL_ARB_invalidate_subdata 
       24      GL_ARB_map_buffer_alignment GL_ARB_map_buffer_range GL_ARB_multi_bind 
       25      GL_ARB_multi_draw_indirect GL_ARB_multisample GL_ARB_multitexture 
       26      GL_ARB_occlusion_query GL_ARB_occlusion_query2 GL_ARB_pixel_buffer_object 
       27      GL_ARB_point_parameters GL_ARB_point_sprite GL_ARB_polygon_offset_clamp 
       28      GL_ARB_post_depth_coverage GL_ARB_program_interface_query 
       29      GL_ARB_provoking_vertex GL_ARB_query_buffer_object 
       30      GL_ARB_robust_buffer_access_behavior GL_ARB_robustness GL_ARB_sample_shading 
       31      GL_ARB_sampler_objects GL_ARB_seamless_cube_map 
       32      GL_ARB_seamless_cubemap_per_texture GL_ARB_separate_shader_objects 
       33      GL_ARB_shader_atomic_counter_ops GL_ARB_shader_atomic_counters 
       34      GL_ARB_shader_bit_encoding GL_ARB_shader_draw_parameters 
       35      GL_ARB_shader_group_vote GL_ARB_shader_image_load_store 
       36      GL_ARB_shader_image_size GL_ARB_shader_objects GL_ARB_shader_precision 
       37      GL_ARB_shader_stencil_export GL_ARB_shader_storage_buffer_object 
       38      GL_ARB_shader_subroutine GL_ARB_shader_texture_image_samples 
       39      GL_ARB_shading_language_100 GL_ARB_shading_language_420pack 
       40      GL_ARB_shading_language_packing GL_ARB_shadow GL_ARB_stencil_texturing 
       41      GL_ARB_sync GL_ARB_tessellation_shader GL_ARB_texture_barrier 
       42      GL_ARB_texture_border_clamp GL_ARB_texture_buffer_object 
       43      GL_ARB_texture_buffer_object_rgb32 GL_ARB_texture_buffer_range 
       44      GL_ARB_texture_compression GL_ARB_texture_compression_bptc 
       45      GL_ARB_texture_compression_rgtc GL_ARB_texture_cube_map 
       46      GL_ARB_texture_cube_map_array GL_ARB_texture_env_add GL_ARB_texture_env_combine 
       47      GL_ARB_texture_env_crossbar GL_ARB_texture_env_dot3 GL_ARB_texture_float 
       48      GL_ARB_texture_gather GL_ARB_texture_mirror_clamp_to_edge 
       49      GL_ARB_texture_mirrored_repeat GL_ARB_texture_multisample 
       50      GL_ARB_texture_non_power_of_two GL_ARB_texture_query_levels 
       51      GL_ARB_texture_query_lod GL_ARB_texture_rectangle GL_ARB_texture_rg 
       52      GL_ARB_texture_rgb10_a2ui GL_ARB_texture_stencil8 GL_ARB_texture_storage 
       53      GL_ARB_texture_storage_multisample GL_ARB_texture_swizzle GL_ARB_texture_view 
       54      GL_ARB_timer_query GL_ARB_transform_feedback2 GL_ARB_transform_feedback3 
       55      GL_ARB_transform_feedback_instanced GL_ARB_transpose_matrix 
       56      GL_ARB_uniform_buffer_object GL_ARB_vertex_array_bgra 
       57      GL_ARB_vertex_array_object GL_ARB_vertex_attrib_64bit 
       58      GL_ARB_vertex_attrib_binding GL_ARB_vertex_buffer_object GL_ARB_vertex_program 
       59      GL_ARB_vertex_shader GL_ARB_vertex_type_10f_11f_11f_rev 
       60      GL_ARB_vertex_type_2_10_10_10_rev GL_ARB_viewport_array GL_ARB_window_pos 
       61      GL_EXT_abgr GL_EXT_bgra GL_EXT_blend_color GL_EXT_blend_equation_separate 
       62      GL_EXT_blend_func_separate GL_EXT_blend_minmax GL_EXT_blend_subtract 
       63      GL_EXT_clip_volume_hint GL_EXT_compiled_vertex_array GL_EXT_direct_state_access 
       64      GL_EXT_draw_buffers2 GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample 
       65      GL_EXT_framebuffer_object GL_EXT_geometry_shader4 GL_EXT_gpu_program_parameters 
       66      GL_EXT_gpu_shader4 GL_EXT_packed_depth_stencil GL_EXT_packed_float 
       67      GL_EXT_polygon_offset_clamp GL_EXT_secondary_color 
       68      GL_EXT_shader_framebuffer_fetch GL_EXT_shader_integer_mix GL_EXT_shadow_funcs 
       69      GL_EXT_stencil_two_side GL_EXT_stencil_wrap GL_EXT_texture_array 
       70      GL_EXT_texture_compression_s3tc GL_EXT_texture_filter_anisotropic 
       71      GL_EXT_texture_integer GL_EXT_texture_shared_exponent GL_EXT_texture_snorm 
       72      GL_EXT_texture_sRGB GL_EXT_texture_sRGB_decode GL_EXT_texture_swizzle 
       73      GL_EXT_timer_query GL_EXT_transform_feedback 
       74      GL_INTEL_conservative_rasterization GL_INTEL_fragment_shader_ordering 
       75      GL_INTEL_framebuffer_CMAA GL_INTEL_map_texture GL_INTEL_performance_query 
       76      GL_KHR_blend_equation_advanced GL_KHR_blend_equation_advanced_coherent 
       77      GL_KHR_context_flush_control GL_KHR_debug GL_KHR_texture_compression_astc_hdr 
       78      GL_KHR_texture_compression_astc_ldr GL_NV_blend_square GL_NV_conditional_render 
       79      GL_NV_primitive_restart GL_NV_texgen_reflection 
[LWJGL] Loading JNI library: lwjgl_stb
[LWJGL] 	Module: org.lwjgl.stb
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_stb.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_stb.dll


Here's the whole class that I had provided before, just edited with several revisions.
import static org.lwjgl.opengl.GL11.*;

import com.programwings.wofrpg.util.Coordinate;
import org.lwjgl.BufferUtils;
import org.lwjgl.stb.STBImage;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class Image {

	private int id, width, height;
	private Coordinate c1, c2, c3, c4;
	private String file;

	public Image(String filename, Coordinate c1, Coordinate c2, Coordinate c3, Coordinate c4) {
		file = filename;
		this.c1 = c1;
		this.c2 = c2;
		this.c3 = c3;
		this.c4 = c4;

		IntBuffer wid = BufferUtils.createIntBuffer(1);
		IntBuffer hei = BufferUtils.createIntBuffer(1);
		IntBuffer com = BufferUtils.createIntBuffer(1);

		ByteBuffer data = STBImage.stbi_load(filename, wid, hei, com, 4);

		id = glGenTextures();
		width = wid.get();
		height = hei.get();

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
		bind();
		STBImage.stbi_image_free(data);
	}

	public void bind() {
		glBindTexture(GL_TEXTURE_2D, id);
	}

	public void unbind() {
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	public void setFileName(String fileName) {
		this.file = fileName;
	}

	public void draw() {
		bind();
		glBegin(GL_QUADS);
			glTexCoord2d(-1, 1);
			glVertex2d(c1.x, c1.y);
			glTexCoord2d(1, 1);
			glVertex2d(c2.x, c2.y);
			glTexCoord2d(1, -1);
			glVertex2d(c3.x, c3.y);
			glTexCoord2d(-1, -1);
			glVertex2d(c4.x, c4.y);
		glEnd();

	}


}


Is there anything else that I may be missing?

spasi

Quote from: Animus_Surge on December 04, 2019, 17:08:11Is there anything else that I may be missing?

The bind() call must be between glGenTextures() and the first glTexParameterf().

Animus_Surge

Quote from: spasi on December 04, 2019, 17:15:38
The bind() call must be between glGenTextures() and the first glTexParameterf().

Okay, I moved it to where you specified, but I still only see a white box. The code for the constructor is here:

{
...
       public Image(String filename, Coordinate c1, Coordinate c2, Coordinate c3, Coordinate c4) {
		file = filename;
		this.c1 = c1;
		this.c2 = c2;
		this.c3 = c3;
		this.c4 = c4;

		IntBuffer wid = BufferUtils.createIntBuffer(1);
		IntBuffer hei = BufferUtils.createIntBuffer(1);
		IntBuffer com = BufferUtils.createIntBuffer(1);

		ByteBuffer data = STBImage.stbi_load(filename, wid, hei, com, 4);

		id = glGenTextures();
		width = wid.get();
		height = hei.get();
		bind();

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
		STBImage.stbi_image_free(data);
	}
...
}

KaiHH

Texture coordinates are between 0 and +1. Additionally, did you glEnable(GL_TEXTURE_2D)?
Only binding the texture does not suffice. The fixed-function pipeline won't automatically use all bound textures. They must also be enabled explicitly..

Animus_Surge

Quote from: KaiHH on December 04, 2019, 17:43:08
Texture coordinates are between 0 and +1. Additionally, did you glEnable(GL_TEXTURE_2D)?
Only binding the texture does not suffice. The fixed-function pipeline won't automatically use all bound textures. They must also be enabled explicitly..
I knew I had been missing something. It works now, thank you so much!