Jump to content
  • Advertisement

Recommended Posts

I am trying to use SOIL with OpenGL to make textures, my code compiles but it does not show anything on my screen. here is my code so far.

int width=50, height=50;
unsigned char* image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGB);

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glRectf(90.0f, 20.0f, 130.0f, 10.0f);
	glRectf(100.0f, 10.0f, 120.0f, -10.0f);
	glRectf(80.0f, 5.0f, 100.0f, -5.0f);
	glRectf(90.0f, -10.0f, 130.0f, -20.0f);
	glutSwapBuffers();
}

 

Share this post


Link to post
Share on other sites
Advertisement

I  have  updated my code

GLuint linktex; 

void drawSprite(GLint left, GLint right, GLint bottom, GLint top, GLuint texture)
{
	glColor3f(1.0f, 0.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, texture);
	glBegin(GL_QUADS);
	glTexCoord2i(1, 1);
	glVertex2i(right, top);
	glTexCoord2i(1, 0);
	glVertex2i(right, bottom);
	glTexCoord2i(0, 0);
	glVertex2i(left, bottom);
	glTexCoord2i(0, 1);
	glVertex2i(left, top);
	glEnd();
}

void init()
{
	glEnable(GL_TEXTURE_2D);
	GLuint linktex = SOIL_load_OGL_texture("img.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glDisable(GL_TEXTURE_2D);
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	drawSprite(50, 50, 50, 50, linktex);
	glutSwapBuffers();
}

 

Share this post


Link to post
Share on other sites

If you call your drawSprite function with these coords, you'll end up with a zero sized quad at x50:y50.You need to setup a camera matrix first and then use 3D coords or create a custom matrix by yourself and use 2D coords if not already done in your code.

Then you need to set your quad to a valid region on screen, either

  • -1, 1, 1
  • 1, 1, 1
  • 1, -1, 1
  • -1, -1, 1

or whatever your custom matrix is about. Setting it like above places a quad in front of your implicite 'camera' moved 1 unit away from it so it is inside your view region and you can see it. If anything worked, you should see a white quad. If not, try to turn it arround 180 ° because it may be backside flipped.

The problem is that even the legacy GL needs a view and a projection matrix to have the graphic driver transfrom your coordinates to screen coordinates properly. If you don't do this, the default memory is filled with zeros and anything multiplied with zero is still zero.

Those matrices are 4x4 component matrices, even for 2D so what you have to do is create a proper Perspective or Orthographic matrix first before you could start drawing anything

Share this post


Link to post
Share on other sites
Posted (edited)
8 hours ago, phil67rpg said:

I  have  updated my code


...
	glEnable(GL_TEXTURE_2D);
..
	glDisable(GL_TEXTURE_2D);
...

 

For anybody else, just to not have aspiring beginners 😉 copy this: this results in an error in a debug context. glEnable() is for the configurable parts of the pipeline (like depth/stencil/scissor tests) and some other capabilites. Textures otoh are bound to targets and/or shader uniform units.

And that's it from my side. I don't like to be chased around from problem to problem to no purpose and without results.

Edited by Green_Baron

Share this post


Link to post
Share on other sites
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	drawSprite(-50, 50, -50, 50, linktex);
	glutSwapBuffers();
}

I am able to draw a big red square but I want to draw a texture like a sprite 

Share this post


Link to post
Share on other sites

@fleabay this is what he wrotes here and as he changed only the render function, I assume the rest of the code stays as it was. The red in the red square comes from glColor3f(1.0f, 0.0f, 0.0f); he hardcoded into both, the render and drawSprite functions

Share this post


Link to post
Share on other sites

well I have adjusted my code, according to shaarigan suggested but it still does not work. it draws a big white square.

GLuint linktex; 

void drawSprite(GLint left, GLint right, GLint bottom, GLint top, GLuint texture)
{
//	glColor3f(1.0f, 0.0f, 0.0f);
	glGenTextures(1, &linktex);
	glBindTexture(GL_TEXTURE_2D, texture);
	glBegin(GL_QUADS);
	glTexCoord2i(1, 1);
	glVertex2i(right, top);
	glTexCoord2i(1, 0);
	glVertex2i(right, bottom);
	glTexCoord2i(0, 0);
	glVertex2i(left, bottom);
	glTexCoord2i(0, 1);
	glVertex2i(left, top);
	glEnd();
}

void init()
{
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glFrontFace(GL_CW);
	GLuint linktex = SOIL_load_OGL_texture("img.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//	glDisable(GL_TEXTURE_2D);
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//	glColor3f(1.0f, 0.0f, 0.0f);
	drawSprite(-50, 50, -50, 50, linktex);
	glutSwapBuffers();
}

 

Share this post


Link to post
Share on other sites

can I please get some help on my texturing problem, I  have also googled my  problem but with no success.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!