Click here to Skip to main content
15,896,912 members

Help with modern opengl call function to create texture ?

lock&_lock asked:

Open original thread
Hi, I'm trying to work with multi-texture using SOIL. I think it's a mistake of how I call the texture. I have 2 textures now with 2 different image source (Texture 1 and Texture 2), they look like this :

Texture :
glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, textures[0]); 
(...) 

image1 = SOIL_load_image("res/img1.png",&width,&height,0,SOIL_LOAD_RGBA); 
//"res/img2.png" for image2 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE, image1); 
SOIL_free_image_data(image1); 

image 1 for Texture 1 & image 2 for Texture 2.

It’s working, and I can call them by changing between texture. Now I’m trying to change the image source for texture 1 and texture 2 as the same image (img0).

I created function changed(), using input image (img0) then I will do some pixel modification with img0 and store the results as t1 and t2 (both unsigned char). I want to use t1 as source for texture1, and t2 as the source for texture2, so I can call and switch between them later. (the code is pretty long, please let me know if you want me to include them).

What I have tried:

I tried to call the function like this :

 //create texture===================================================== 
GLuint textures[2]; 
glGenTextures(2, textures);   

image = SOIL_load_image("res/img0.png", &width, &height, 0, SOIL_LOAD_RGBA); 
if (image == NULL) exit(0); 
image1 = NULL; //(image1 = t1 later in changed() func.)
image2 = NULL; //(image2 = t2 later in changed() func.)
changed();   

//Texture 1 
glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, textures[0]); 
(...)  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1); 
SOIL_free_image_data(image1); 

//then Texture 2 with image2

But it doesn’t work, no error and no image drawn to the scene. I checked with changing the clear color buffer value, and the color buffer works.So it’s the problem with calling the function and passing my image. Any suggestion ?

[UPDATE]

I added error handling code to check, like this :
while (!(err = glGetError())) { 		
            cout << "some error message" << err << endl; 	
        }

And turn out the error is after my loading source image code :
image = SOIL_load_image("res/img0.png", &width, &height, 0, SOIL_LOAD_RGBA);

How do I properly load an image using SOIL as a source for a function ?
This is the same way I did it with GLUT and it’s working, is it different with GLFW ?

[UPDATE AGAIN]

I replaced error handling with this, and the program is still runing. Not exit, so definitely the image is there.
if (image == NULL) {
		cout << "Error loading image source." << err << endl;
		exit(0);
	}

I also tried to print the width and height of the image, and I got the correct result on the console.
cout << width << " x " << height;

But the image isn’t showing. I only need this image to be input of my function, and later my function will generate 2 image result, I will use it as texture 1 and texture 2. Help ?
Tags: C++11, Windows, OpenGL, Texture2D

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900