Texturing quad using SOIL

#1
#include <freeglut.h>
#include <iostream>
#include<SOIL.h>

using namespace std;

GLuint textureBrick; 

GLuint loadTex(const char* texname)                                    
{
	/* load an image file directly as a new OpenGL texture */
	GLuint texture = SOIL_load_OGL_texture
	(
		texname,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
	);
	return texture;
}

void init()
{
	textureBrick = loadTex("C:\\Users\\Owner\\Desktop\\img.bmp");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glBindTexture(GL_TEXTURE_2D, textureBrick);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2i(1, 1);
	glVertex2i(10, 10);
	glTexCoord2i(1, 0);
	glVertex2i(10, -10);
	glTexCoord2i(0, 0);
	glVertex2i(-10, -10);
	glTexCoord2i(0, 1);
	glVertex2i(-10, 10);
	glEnd();
	glPopMatrix();
	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat aspectRatio;
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
		glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}


int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(400, 300);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Combat");
	glutDisplayFunc(renderScene);
	glutReshapeFunc(ChangeSize);
	init();
	glutMainLoop();
	return 0;
}

I am trying to texture a quad using SOIL. All I get is a small white square.

#2

You need to be checking for OpenGL Errors. If you were, your driver might very well point out the exact line(s) that are causing the problem.

For the above specifically, GL_LINEAR_MIPMAP_LINEAR is only a valid setting for GL_TEXTURE_MIN_FILTER, and even then only if your texture has MIPmaps defined (**). A relevant example from the wiki.

Try using GL_LINEAR for both of the above.

** I suspect that your texture does not contain MIPmaps, …since you haven’t provided the SOIL flag SOIL_FLAG_MIPMAPS to SOIL_load_OGL_texture().

#3

I forgot how to post code, I made the changes as suggested. here is my updated code. but it still draws a little white square.

GLuint loadTex(const char* texname)                                    
{
	/* load an image file directly as a new OpenGL texture */
	GLuint texture = SOIL_load_OGL_texture
	(
		texname,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS
	);
	return texture;
}

void init()
{
	textureBrick = loadTex("img.bmp");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glGenTextures(1, &textureBrick);
	glBindTexture(GL_TEXTURE_2D, textureBrick);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glBegin(GL_QUADS);
	glTexCoord2i(0, 0);
	glVertex2i(10, 10);
	glTexCoord2i(1, 0);
	glVertex2i(10, -10);
	glTexCoord2i(1, 1);
	glVertex2i(-10, -10);
	glTexCoord2i(0, 1);
	glVertex2i(-10, 10);
	glEnd();
	glPopMatrix();
	glutSwapBuffers();
}
#4

Trace what’s going on with textureBrick.

In init() -> loadTex() -> SOIL_load_texture(), you’re having SOIL load an image, create a new OpenGL texture handle, and upload the loaded image data to this GL texture.

Then in renderScene(), every single frame, you’re telling GL to:

  1. create a new GL texture handle,
  2. overwrite the previous one stored in textureBrick (which the first time renderScene() is called is a GL texture handle for the one you had SOIL create) with this new handle,
  3. bind this new empty GL texture (which has no image data attached to it),
  4. set a few parameters on it, and then
  5. try to render with this empty texture.
#5

I put in glEnable(GL_TEXTURE_2D) and it worked thanks for all the help.

#6

well now I need to animate a sprite sheet, where do I start?

#7

You can get ideas from this book: Build your own 2D Game Engine. OpenGL and WebGL are very similar. For example, I made this example that draws a font using sprite, and C#/OpenTK/OpenGL3.1. Every character is a separated sprite. Drawing text is very important for games and interactive applications.

LineSegment_theFirstPoint