Skip to main content
GameDev.net gamedev.net
🔒 Locked

SOIL

Started by phil67rpg Aug 14, 2019 at 1:16 AM 23 replies 11.8k views
Original Post
phil67rpg
phil67rpg

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();
}


phil67rpg
phil67rpg

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();
}


Shaarigan
Shaarigan

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

Green_Baron
Green_Baron
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.

phil67rpg
phil67rpg

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

Shaarigan
Shaarigan

You disabled textures in the last line of your init function and never enabled them again

fleabay
fleabay
1 hour ago, Shaarigan said:

You disabled textures in the last line of your init function and never enabled them again

You have no way of knowing that.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
Shaarigan
Shaarigan

@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

phil67rpg
phil67rpg

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();
}


phil67rpg
phil67rpg

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

phil67rpg
phil67rpg

here is my updated code I don't why it does not work, it looks like it should work.


#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;
}


fleabay
fleabay
24 minutes ago, phil67rpg said:

here is my updated code I don't why it does not work, it looks like it should work.

You have posted 5 times in this thread stating a problem but have not asked a single question. This is after many requests (even mods) that you stop doing that. For YEARS.

You post code and expect others to figure it out but if someone gives too much code you say "I want to figure it out myself".

You do projects in winforms, console, OpenGL, DirectX, different languages and you say that you complete games, but never give links or proof to your finished projects.

You have a resume that states you are in no less words a certified professional with years of experience and hold a Bachelor of Science Degree in Computer Science. You have an Associates in electronics. You have certifications in CompTIA Network+, A+, MCTS (in-progress). Your list of experience includes Assembly Language. You have lead teams of students in game development.

You even state on your resume; "Resourceful problem solver with excellent troubleshooting skills; strong follow-through abilities to ensure projects to successful conclusion "

Yet from your last thread, you don't have a firm grasp on how simple Booleans work. This and you have an electronics degree?

Who are you BS'ing? Is your resume bogus and a complete sham or ???

*BS'ing means bullshitting since from my past experience you play dumb on common jargon / can't be bothered to use Google.


🙂🙂🙂🙂🙂<←The tone posse, ready for action.
phil67rpg
phil67rpg
20 hours ago, fleabay said:

can't be bothered to use Google

I have used google but with no success, my question is how do I use texturing to draw sprites to the screen, I want to use either .png or .bmp file formats.

fleabay
fleabay
1 hour ago, phil67rpg said:

I have used google but with no success, my question is how do I use texturing to draw sprites to the screen, I want to use either .png or .bmp file formats.

How come you have OpenGL listed on your resume as an API you know if you don't know it?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
phil67rpg
phil67rpg

can I please get some help on my question not just a critique on my resume by the way I am going to stop looking for a computer programming jobs I am going to do programming just for fun.

fleabay
fleabay
42 minutes ago, phil67rpg said:

can I please get some help on my question not just a critique on my resume

I am not critiquing your resume. You are not who you say you are. You don't write like you have a Bachelors degree. Maybe you have been trolling this site for years now. Or maybe you lied on your resume. It's one or the other.

If your resume is factual then I doubt many people here are qualified to help you based on your higher education and skills acquired.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
AtomicWinter
AtomicWinter

Hello - The book "OpenGL - Build High Performance Graphics" has a section in the first chapter that goes over loading and displaying an image in OpenGL using SOIL (they also use FreeGlut like you do as an added bonus). Below is the source code for that section. I would look it over and see what they're doing differently and get some ideas from it.

https://github.com/PacktPublishing/OpenGL-Build-High-Performance-Graphics/blob/master/Module 1/Chapter01/ImageLoader/ImageLoader/main.cpp


phil67rpg
phil67rpg

well greenbaron do you have any input?

fleabay
fleabay
22 minutes ago, phil67rpg said:

well greenbaron do you have any input?

You have several threads here and elsewhere going back 9+ years where you were asking about texturing quads using OpenGL. The tutorial links and advice in those threads are very similar as what you are currently getting.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.