Trouble with OpenGL with SOIL

Side Note:
I downloaded the SOIL file, went into the VC8 solution, then compiled it in debug and release. Took the debug SOIL.lib and placed it in the lib folder; which replaced a SOIL.lib that was already their. I took the newly compiled SOIL.lib, and placed in my linker in VS 2012. Also, the image that is used is in the project folder where the source files are located. Hopefully I did that right.

My screen appears black when I'm executing my program. I do not think there is anything wrong with the shaders or program linkage as my command prompt is completely empty. During debug though my variable unsigned char * image spits out weird values:

debug watch1:
 
image :   +		image	0x05a88040 "•~p‘wj’{mŽ{j‹xg©|¹Ÿˆ´œ€¯“}¦Žv«{¬{±–°š‚­•{¨Šp¨v †m–zd~j €kšzcž~e¢ƒg©†h¡~^¯Žm..}  

(There is more, but I think you get the idea)

For anyone who has used SOIL before is this normal? I have never used it so this is all new to me. I am suspecting SOIL is the problem, but I'm not exactly sure. Here is the source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <SFML\Graphics.hpp>
#include <GL\glew.h>
#include "SOIL.h"


GLuint compiler_shader()
{
	GLuint vertex_shader;
	GLuint fragment_shader;
	GLuint program;

	static const GLchar * vertex_shader_source[] = 
	{
		"#version 400 core								\n"
		"											    \n"
		"layout (location = 0) in vec3 position;		\n"
		"layout (location = 1) in vec3 color;		    \n"
		"layout (location = 2) in vec2 texCoord;		\n"
		"												\n"
		"out vec3 ourColor;								\n"
		"out vec2 TexCoord;								\n"
		"												\n"
		"void main()									\n"
		"{												\n"
		"   gl_Position = vec4(position, 1.0f);			\n"
		"	ourColor = color;							\n"
		"	TexCoord = texCoord; 						\n"
		"												\n"
		"}                                              \n"
	};

	static const GLchar * fragment_shader_source[] = 
	{
		"#version 400 core		                                \n"
		"		                                                \n"
		"in vec3 outColor;		                                \n"
		"in vec2 TexCoord;		                                \n"
		"                                                       \n"
		"out vec4 color;                                        \n"
		"                                                       \n"
		"uniform sampler2D ourTexture1;                         \n"
		"                                                       \n"
		"                                                       \n"
		"void main()                                            \n"
		"{                                                      \n"
		"  color = texture(ourTexture1, TexCoord);				\n"
		"}                                                      \n"
		"                                                       \n"
	};

	vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertex_shader, 1, vertex_shader_source, nullptr);
	glCompileShader(vertex_shader);

   GLint success1;
   GLchar info1[520];
   glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success1);

   if(!success1)
   {
	   glGetShaderInfoLog(vertex_shader, 512, nullptr, info1);
		for(int i = 0; i < 502; i++)
			std::cout << info1 << std::endl;
   }

	fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragment_shader, 1, fragment_shader_source, nullptr);
	glCompileShader(fragment_shader);

   GLint success2;
   GLchar info2[520];
   glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success2);

   if(!success2)
   {
	   glGetShaderInfoLog(fragment_shader, 512, nullptr, info2);
		for(int i = 0; i < 502; i++)
			std::cout << info2 << std::endl;
   }



	program = glCreateProgram();
	glAttachShader(program, vertex_shader);
	glAttachShader(program, fragment_shader);
	glLinkProgram(program);

	GLint success3;
	GLchar info3[512];
	glGetProgramiv(program, GL_LINK_STATUS, &success3);

	if(!success3)
	{
		glGetProgramInfoLog(program, 512, nullptr, info3);
		for(int i = 0; i < 512; i++)
			std::cout << "Program: "  << info3 << std::endl;
	}

	glDetachShader(program, vertex_shader);
	glDetachShader(program, fragment_shader);

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);

	return program;

}
int main()
{
	const unsigned WindowX = 600;
	const unsigned WindowY = 600;
	sf::RenderWindow Window(sf::VideoMode(WindowX, WindowY), "OpenGL Textures", sf::Style::Default, sf::ContextSettings(32));

	if(GLEW_OK != glewInit())
		std::cout << "Error! Could not load Glew" << std::endl;

	GLuint render_program = compiler_shader();

	GLfloat vertices[] = {

		// Position              // Color
		0.5f,  0.5f, 0.0f,     1.0f, 0.0f, 0.0f,       1.0f, 1.0f,    // Top Right  
		0.5f, -0.5f, 0.0f,     0.0f, 1.0f, 0.0f,       1.0f, 0.0f,    // Bottom Right
	   -0.5f, -0.5f, 0.0f,     0.0f, 0.0f, 1.0f,       0.0f, 0.0f,    // Bottom Left
	   -0.5f,  0.5f, 0.0f,     0.0f, 1.0f, 0.0f,       0.0f, 1.0f     // Top Left

	};

	GLfloat indices[] = {
	
			0, 1, 3,
			1, 2, 3
	
	};

	GLuint VAO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);

		GLuint VBO;
		glGenBuffers(1, &VBO);
		glBindBuffer(GL_ARRAY_BUFFER, VBO);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


		GLuint EBO;
		glGenBuffers(1, &EBO);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
		
		// Position attributes
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) 0);
		glEnableVertexAttribArray(0);

		// Color attributes
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *) (3 * sizeof(GLfloat)));
		glEnableVertexAttribArray(1);

		// Tex Coordinates attributes
		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *) (6 * sizeof(GLfloat)));
		glEnableVertexAttribArray(2);

	glBindVertexArray(0);
		//--------------
		// Texture
		//--------------
		GLuint Texture1;
		glGenTextures(1, &Texture1);
		glBindTexture(GL_TEXTURE_2D, Texture1);
		// Set Texture parameters 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		// Set Texture filtering
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


		int tex_width;
		int tex_height;
		unsigned char * image = SOIL_load_image("container.jpg",  &tex_width, &tex_height, 0, SOIL_LOAD_RGB);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
		glGenerateMipmap(GL_TEXTURE_2D);
		SOIL_free_image_data(image);
		glBindTexture(GL_TEXTURE_2D, 0);


	sf::Clock clock;
	bool stop = false;
	while(Window.isOpen())
	{
		Window.clear();

		sf::Time dt = clock.restart();

		sf::Event Event;
		while(Window.pollEvent(Event))
		{
			switch(Event.type)
			{
			case sf::Event::Closed:
				Window.close();
				stop = true;
				break;

			case sf::Event::KeyPressed:
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
				{
					Window.close();
					stop = true;
				}
				break;

			}

		}

		if(stop)
			break;

		glUseProgram(render_program);
		glBindVertexArray(VAO);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		glBindVertexArray(0);
		
		Window.display();
	}
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);

	return 0;
}


Also, does anybody know of any OpenGL debugging tools? Tools that you have personally used and recommend.
Last edited on
I haven't use SOIL or something.

During debug though my variable unsigned char * image spits out weird values:
This is normal for a binary image.

You have serveral maginc numbers like 512, 520, 502, etc.
Are these values correct? Better use constants instead.
Those numbers are for debug purposes. They are the max size for the arrays.
Aside, from SOIL, do you think there is a problem with the logic in the vertex or fragment shaders. Nothing jumps out at me at the moment.
nothing sticks out looking at it, but I've never seen a shader loaded into an array of GLchars. I'm not saying it's wrong, just that I've not seen it before. I have seen them loaded into a single GLchar* or string. So, do you see a red square if you change your output color in your fragment shader to be color = vec4(1.0, 0.0, 0.0, 1.0);? This would be line 47 above.
Topic archived. No new replies allowed.