Jump to content

  • Log In with Google      Sign In   
  • Create Account

Opengl 3.3 load & display texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 07:08 AM

Can anyone recommend a tutorial on loading and drawing textures that might include sample code in an OpenGL 3.3 core context?

Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 14 January 2011 - 07:53 AM

Loading textures has not changed in GL3

http://www.opengl.org/wiki/Tutorials
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#3 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 08:59 AM

Loading textures has not changed in GL3

http://www.opengl.org/wiki/Tutorials


Hmm I don't find the glTexEnv function in GL3

#4 zerothrillz   Members   -  Reputation: 152

Like
0Likes
Like

Posted 14 January 2011 - 10:27 AM

Hmm I don't find the glTexEnv function in GL3


glTexEnvi is deprecated post gl 3.0.

#5 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 03:27 PM

another thing I've noticed is glEnable(GL_TEXTURE_2D),
GL_TEXTURE_2D is no longer a valid enum

#6 jariRG   Members   -  Reputation: 760

Like
0Likes
Like

Posted 14 January 2011 - 03:30 PM

another thing I've noticed is glEnable(GL_TEXTURE_2D),
GL_TEXTURE_2D is no longer a valid enum


As no longer fixed function pipeline, the GL_TEXTURE_2D (and the others) flag is pointless.
glDisable(GL_TEXTURE_2D) = dont send to the shader. :)

#7 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 03:44 PM

another thing I've noticed is glEnable(GL_TEXTURE_2D),
GL_TEXTURE_2D is no longer a valid enum




another thing I've noticed is glEnable(GL_TEXTURE_2D),
GL_TEXTURE_2D is no longer a valid enum


As no longer fixed function pipeline, the GL_TEXTURE_2D (and the others) flag is pointless.
glDisable(GL_TEXTURE_2D) = dont send to the shader. :)



Just to clarify this quote, glEnable/glDisable(GL_TEXTURE_2D) has no effect on shader pipeline. When you have a sampler at location 0, it will always sample the texture bound to glActiveTexture(GL_TEXTURE0), regardless of the enabled state of GL_TEXTURE_2D.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#8 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 05:05 PM

O-o I need to post some code to remain sane...tell me if you see anything obviously wrong

Initialization functions

Loading Vbo & Texture


Drawing and shader source

#9 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 05:28 PM

If you post that inline with code tags or on codepad I'd consider taking a look at it, but I'm currently on a VPN which doesn't allow access to filesharing sites for security reasons. There's really no reason why you need to have people your download your files, just post the relevant parts.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#10 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 05:33 PM

Initialization functions
http://codepad.org/N6G4L0mO
Loading vbo & texture
http://codepad.org/txCRarG5
Drawing + shader source
http://codepad.org/78ylAPSm

#11 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 05:38 PM

And what exactly is the nature of your problem here? Black tri's, white tri's , blank screen, ??
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#12 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 05:42 PM

Clears to white screen, no tris

Thanks for taking a look at it. I thought the code posting was broken on the new board.

#13 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 05:57 PM

I think code posting works fine with '[' code ']' tags

Couple things I notice:

1) Always, always call glGetError at least once during your draw loop. It will help you find problems.
2) Nowhere have you bound your "sampler2D tex" to texture channel 0. You have to do this with glUniform1i like any other uniform int. I believe it to be undefined otherwise, and you may be sampling the wrong texture channel. This doesn't explain why you don't at least see any polygons though.
3) I have no idea what your modelview matrix is. It's set during initialization but I don't know from what.


Also I'll recommend changing your clearcolor to something other than white. Sometimes illegal texture sampling can cause a white triangle, and this will confuse you because you won't see a polygon at all. Choose some nonblack - nonwhite random color for the clearColor.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#14 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 January 2011 - 06:30 PM

1) No errors in Draw()
2) Didn't know about that one, thanks for pointing it out. I changed the clear color to a non-white color, still no sign of tris.
3) Modelview is initialized to the identity matrix

#15 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 06:38 PM

Hmm thats unfortunate. I don't see what else exactly is the problem, so you may want to try regressing a bit here to fixed function and debug pieces of it at a time. I know it's a pain though, but it's probably the best way to figure out what part is giving you problems.

1) Can you draw your quad with no shaders, no vbos, using just your matrices and the fixed function pipeline?
2) Can you get it to work with only vertex arrays?
3) With fixed function and VAO's?
4) If you get all that, a simple shader that only outputs a single color?
5) Then try adding texturing?
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#16 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 14 January 2011 - 06:49 PM

Also I see you declare inVertex as a vec4, but define it as 3 elements only in glVertexAttribPointer. Probably want to make that consistent, I have no idea what the consequences of that could be.

Actually this seems like it might be ok.

If there is a mismatch between the incoming dimensionality and the attribute's dimensionality, OpenGL will satisfy it. If the vertex data has more components than the shader attribute uses, the extra components are ignored. If the vertex data specifies fewer than the shader attribute uses, then the unfilled-in components are 0, except for the 4th component which is set to 1.


My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#17 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 16 January 2011 - 03:35 PM

Hmm thats unfortunate. I don't see what else exactly is the problem, so you may want to try regressing a bit here to fixed function and debug pieces of it at a time. I know it's a pain though, but it's probably the best way to figure out what part is giving you problems.

1) Can you draw your quad with no shaders, no vbos, using just your matrices and the fixed function pipeline?
2) Can you get it to work with only vertex arrays?
3) With fixed function and VAO's?
4) If you get all that, a simple shader that only outputs a single color?
5) Then try adding texturing?


It must be something with the shaders and/or my matrices. I can draw from the VAO or immediate mode if I intentionally don't compile the shaders. Perhaps things are being rendered outside of the view.
My BuildOrthoProjMatrix function is based on the glOrtho function as I see it here glOrtho [size=1]
void BuildOrthoProjMat(float* amat, float al, float ar, float ab, float at, float azn, float azf){[/size]
	*((glm::mat4*)amat) = glm::mat4();
	float tx = (ar+al)/(ar-al);
	float ty = (at+ab)/(at-ab);
	float tz = (azf+azn)/(azf-azn);

	amat[0] = 2/(ar-al);
	amat[5] = 2/(at-ab);
	amat[10] = -2/(azf-azn);
	amat[12] = tx;
	amat[13] = ty;
	amat[14] = tz;
}
When I call this function with these arguments BuildOrthoProjMat((float*)&mProjectionMatrix, 0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, -1, 1);


Does this look like the proper transformed vector?
vec4 worldVec(800, 600, 0, 1);
vec4 screenVec = mProjectionMatrix * worldVec;
gives me a screenVec of (3, 3, 0, 1);

Is it supposed to give me normalized device coordinates? In that case it seems like it is outside of the view.

#18 karwosts   Members   -  Reputation: 840

Like
1Likes
Like

Posted 16 January 2011 - 09:31 PM

Hmm I think it is supposed to be NDC coordinates after projection. What is screen_width/height in your case?

I did a quick test:

glm::mat4 m = glm::gtx::matrix_projection::ortho<float>(0,800.f,0,800.f,-1.f,1.f);
glm::vec4 v(800,800,0,1);
glm::vec4 vt = m*v;

and vt comes out to be (0.9999,0.9999, 0, 1), which seems like right on the edge of the NDC like I would expect.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#19 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 16 January 2011 - 11:20 PM

800x600... I didn't know about the ortho function in glm...(i can't figure out a way to search the api documentation).
When I use this matrix instead I get the expected vector, so it must have been a problem with my matrix creation. (Is there a reason why you used the gtx instead of the gtc ortho function?)

Edit: Okay that seems to have been the problem. Thanks for the great help, karwosts.


#20 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 17 January 2011 - 12:10 AM

I wasn't aware that there was an ortho under gtc (I just started looking around until I found it). The equations for your matrix look right, have you actually verified that they are modifying the matrix correctly?

This is just speculation, but:

You have a mat4* pointer amat, which you then access by

amat[5] = blah;

Wouldn't this give you a reference to the 6th mat4 in an array of mat4s?

The proper way that I know of to access the fifth element of a mat4 would be:

(*amat)[1][1] = blah;

Maybe your way works too, but I wouldn't think it would, unless that's overloaded somehow.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS