Sign in to follow this  
tmason

OpenGL Texture Loading "Per Triangle" versus all of the Triangles Called with glDrawElements

Recommended Posts

Hello,

 

I have this weird problem that I assume is really simple to fix but I can't figure it out. Basically my textures are being loaded per "triangle" when I use them in OpenGL.

 

Thank you in advance for any assistance you can provide.

 

Here is how I am loading them into OpenGL: 

glGenTextures(1, &TextureObject_ID);   // section being edited by CKoeber to pull in material files
		glActiveTexture(GL_TEXTURE0 + 1);
		glBindTexture(GL_TEXTURE_2D, TextureObject_ID);
		GLsizei iFormat = BytesPerPixel == 24 ? GL_BGR : BytesPerPixel == 8 ? GL_LUMINANCE : 0;
		GLsizei iInternalFormat = BytesPerPixel == 24 ? GL_RGB : GL_DEPTH_COMPONENT;
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
		             WidthOfTexture, HeightOfTexture, 0, iFormat,
		             GL_UNSIGNED_BYTE, bytesDataPointer);
		glGenerateMipmap(GL_TEXTURE_2D);
		FreeImage_Unload(loadedImage);
		glGenSamplers(1, &TextureSamplerObject_ID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		setFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);
		HasMipsBeenGenerated = true;
		glActiveTexture(GL_TEXTURE0 + 0);
		glBindTexture(GL_TEXTURE_2D, NULL);

I am sending my UV coordinates to OpenGL like so:

		glBufferSubData(GL_ARRAY_BUFFER, UVOffset, UVBufferSize, UVs);
		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
		glEnableVertexAttribArray(2);

I am buffering my vertices and normals in similar fashion as I am drawing fine.

 

Also,

 

Here is the relevant section in the graphics card:

/*

In Fragment Shader...

*/

uniform sampler2D MainTextureSampler; 
in vec2 TextureCoordinates;
out vec4 finalColor;

void Main() {

    vec4 MaterialTextureColor = texture2D(MainTextureSampler, TextureCoordinates);
    finalColor = MaterialTextureColor;

}

Below is an example image:

 

Texture_Per_Triangle.png

Share this post


Link to post
Share on other sites

This looks pretty much like broken tex coordinates for this triangle.

 

Do you generate the texture coordinates yourself or are you loading them from a model?

If you generate them your self, check that corresponding vertices in each triangle have the same texture coordinates assigned.

 

If the vertexbuffer of this quad looks like this  [V1/V2/V3/V1/V3/V4]

make sure the buffer for the texture coordinates is layouted the same way [T1/T2/T3/T1/T3/T4]

 

 

Share this post


Link to post
Share on other sites

This looks pretty much like broken tex coordinates for this triangle.

 

Do you generate the texture coordinates yourself or are you loading them from a model?

If you generate them your self, check that corresponding vertices in each triangle have the same texture coordinates assigned.

 

If the vertexbuffer of this quad looks like this  [V1/V2/V3/V1/V3/V4]

make sure the buffer for the texture coordinates is layouted the same way [T1/T2/T3/T1/T3/T4]

 

Thanks,

 

I am loading the UVs from a file. I was wondering how to generate them myself but I want to use them from the file if I can.

 

If I have to, how would I generate them?

 

Thank you for your time.

Share this post


Link to post
Share on other sites

Then maybe it would help to post the code where you are loading the file (what format?) and you draw call to open gl.

 

It's just a hunch, but if you have back face culling disabled and use the wrong winding order of faces, it might just generate this effect.

maybe playing a little around with glFrontFace() and glCullFace() will help.

 

 

If I have to, how would I generate them?

 

Don't even think about that then biggrin.png

Share this post


Link to post
Share on other sites

Then maybe it would help to post the code where you are loading the file (what format?) and you draw call to open gl.


Here is the way I am loading the texture (using FreeImage and JPEG files):

FIBITMAP *loadedImage = NULL;
		FREE_IMAGE_FORMAT CurrentFormat = FIF_UNKNOWN;
		CurrentFormat = FreeImage_GetFileType(MaterialFile.c_str(), 0);

		if (CurrentFormat == FIF_UNKNOWN) {
			FREE_IMAGE_FORMAT CurrentFormat = FreeImage_GetFIFFromFilename(MaterialFile.c_str());
		}

		if (CurrentFormat == FIF_UNKNOWN) {
			return false;
		}

		if (FreeImage_FIFSupportsReading(CurrentFormat)) {
			loadedImage = FreeImage_Load(CurrentFormat, MaterialFile.c_str());
		}

		if (!loadedImage) {
			return false;
		}

		BYTE* bytesDataPointer = FreeImage_GetBits(loadedImage);
		WidthOfTexture = FreeImage_GetWidth(loadedImage);
		HeightOfTexture = FreeImage_GetHeight(loadedImage);
		BytesPerPixel = FreeImage_GetBPP(loadedImage);

		if (WidthOfTexture == 0 || HeightOfTexture == 0 || BytesPerPixel == 0) {
			return false;
		}

		glGenTextures(1, &TextureObject_ID);   // section being edited by CKoeber to pull in material files
		glActiveTexture(GL_TEXTURE0 + 1);
		glBindTexture(GL_TEXTURE_2D, TextureObject_ID);
		GLsizei iFormat = BytesPerPixel == 24 ? GL_BGR : BytesPerPixel == 8 ? GL_LUMINANCE : 0;
		GLsizei iInternalFormat = BytesPerPixel == 24 ? GL_RGB : GL_DEPTH_COMPONENT;
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
		             WidthOfTexture, HeightOfTexture, 0, iFormat,
		             GL_UNSIGNED_BYTE, bytesDataPointer);
		glGenerateMipmap(GL_TEXTURE_2D);
		FreeImage_Unload(loadedImage);
		glGenSamplers(1, &TextureSamplerObject_ID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		setFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);
		HasMipsBeenGenerated = true;
		glActiveTexture(GL_TEXTURE0 + 0);
		glBindTexture(GL_TEXTURE_2D, NULL);
And here is the way I am drawing the OpenGL quad with materials:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
glUseProgram(g_StandardShaderProgram_ID);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);
glUniform1i(GetEnableWireframeID(), 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

for (GLuint i = 0; i < CurrentMeshCache.size(); i++) {

/*

Inside my object that has my mesh/quad/material data...

/*

glBindVertexArray(Pointer_VAO);

for (int i = 0; i < ModelMatrixInstances.size(); i++) {
	glm::mat4 ModelViewMatrix = CurrentOpenGLController->GetViewMatrix() * ModelMatrixInstances[i];
	glm::mat3 NormalMatrix = glm::transpose(glm::inverse(glm::mat3(ModelViewMatrix)));
	glm::mat4 ModelViewProjectionMatrix = CurrentOpenGLController->GetProjectionViewMatrix() * ModelMatrixInstances[i];
	glBindBuffer(GL_ARRAY_BUFFER, MVP_VBO);
	glBufferSubData(GL_ARRAY_BUFFER, (i * (sizeof(glm::mat4))), sizeof(glm::mat4), glm::value_ptr(ModelViewProjectionMatrix));
	glBindBuffer(GL_ARRAY_BUFFER, NormalMatrix_VBO);
	glBufferSubData(GL_ARRAY_BUFFER, (i * (sizeof(glm::mat3))), sizeof(glm::mat3), glm::value_ptr(NormalMatrix));
}

glUniform4fv(CurrentOpenGLController->GetMaterialColorID(), 1, Materials[0].ObjectColor);
glUniform4fv(CurrentOpenGLController->GetAmbientMeshColorID(), 1, Materials[0].AmbientColor);
glUniform4fv(CurrentOpenGLController->GetEmmissiveMeshColorID(), 1, Materials[0].EmissiveColor);
glUniform4fv(CurrentOpenGLController->GetSpecularMeshColorID(), 1, Materials[0].SpecularColor);
glUniform4fv(CurrentOpenGLController->GetDiffuseMeshColorID(), 1, Materials[0].DiffuseColor);
glUniform1fv(CurrentOpenGLController->GetMeshShininessID(), 1, &Materials[0].Shininess);

if (Materials[0].HasMaterialFile == true) {
	GLfloat ObjectHasMaterial = 1.0f;
	glUniform1fv(CurrentOpenGLController->GetObjectHasMaterialFileID(), 1, &ObjectHasMaterial);
	glUniform1i(CurrentOpenGLController->GetMainTextureID(), 1);
	Materials[0].bindTexture(1);
}
else {
	GLfloat ObjectDoesNotHaveMaterial = 0.0f;
	glUniform1fv(CurrentOpenGLController->GetObjectHasMaterialFileID(), 1, &ObjectDoesNotHaveMaterial);
	glBindTexture(GL_TEXTURE_2D, NULL);
}

glDrawElementsInstanced(GL_TRIANGLES, TotalVertexCount,
	GL_UNSIGNED_INT, NULL, NumberOfChildItems + 1);

if (Materials[0].HasMaterialFile == true) {
	glActiveTexture(GL_TEXTURE0 + NULL);
	glBindTexture(GL_TEXTURE_2D, NULL);
}

/*

Now out of my object...

*/
}

glBindVertexArray(0);

Share this post


Link to post
Share on other sites

It's just a hunch, but if you have back face culling disabled and use the wrong winding order of faces, it might just generate this effect.
maybe playing a little around with glFrontFace() and glCullFace() will help.


Tried this; same results. Thank you for the suggestion though.

Share this post


Link to post
Share on other sites

If I have to, how would I generate them?

 
Don't even think about that then biggrin.png


Darn, it's OK, I can learn smile.png

Share this post


Link to post
Share on other sites

Darn, it's OK, I can learn

 

 

Naa, thats not what I mean. This would just be a hack and not a solution to your problem in the first place.

 

I am sorry, I think I was not clear enough, but with file I didn't mean the image but the geometry you are loading. I think you have to look for the error in how you set up your vertex/normal/texcoord/index buffers.

 

Have you ever tried to render a simpler scene (just one quad maybe?) to make it easier to narrow down the error?

 

 

Do you really want to draw glDrawElementsInstanced ()???   this would mean that you would render the same geometry over and over again and you need an instance buffer with appropriate glVertexAttribDivisor() set (have a look at instanced rendering then: http://en.wikipedia.org/wiki/Geometry_instancing)

Second I noticed, that you call glDrawElements* with TotalVertexCount  I dont know what this variable contains but you should call this function with the number of indices not vertices.

Share this post


Link to post
Share on other sites
From what type of file are you loading the model data?
Some file formats are obnoxious and use 1-based indices or have other quirks when indexing into UV/normal/vertex pools.


L. Spiro

Share this post


Link to post
Share on other sites

From what type of file are you loading the model data?
Some file formats are obnoxious and use 1-based indices or have other quirks when indexing into UV/normal/vertex pools.


L. Spiro


I am loading the data from an FBX file.

Specifically, I am using Autodesk's FBX SDK.

Share this post


Link to post
Share on other sites

So you are covering all 4 methods for creating indices into the pools, right?


L. Spiro


Hello,

Perhaps I am an amateur but I don't get this. "Creating indices into the pools"?

I don't know what that means.

Thank you for your time. Edited by tmason

Share this post


Link to post
Share on other sites
Look at any sample that runs over the faces of each mesh.
You have FbxGeometryElement::eByControlPoint or FbxGeometryElement::eByPolygonVertex.
In both cases you have either GetReferenceMode() == FbxLayerElement::eDirect or FbxLayerElement::eIndexToDirect.

This snippet gets the proper normal index (ffvThisVert.u32NormalIndex) under these considerations:
	// Go over each vertex in the face.
	for ( u32 I = 0UL; I < ui32PolyTotal; ++I ) {
		FC_FACE::FC_FACE_VERTEX ffvThisVert;
		u32 u32VertIndex = piFacePool[ui32PolyStart+I];
		ffvThisVert.u32VertexIndex = u32VertIndex;

		if ( m_u32Flags & FC_NORMALS ) {
			ffvThisVert.u32NormalIndex = 0UL;
			const FbxLayerElementNormal * plenNormals = m_pfmMesh->GetLayer( 0 )->GetNormals();
			switch ( plenNormals->GetMappingMode() ) {
				case FbxGeometryElement::eByControlPoint : {
					// The normal index is based on the vertex index.
					if ( plenNormals->GetReferenceMode() == FbxLayerElement::eDirect ) {
						// The normal index is the same as the vertex index.
						ffvThisVert.u32NormalIndex = u32VertIndex;
					}
					else if ( plenNormals->GetReferenceMode() == FbxLayerElement::eIndexToDirect ) {
						// The normal index is found by plugging the vertex index into an array of indices.
						ffvThisVert.u32NormalIndex = plenNormals->GetIndexArray().GetAt( u32VertIndex );
					}
					if ( ffvThisVert.u32NormalIndex >= static_cast<u32>(plenNormals->GetDirectArray().GetCount()) ) {
						std::cout << "Invalid normal index." << std::endl;
					}
					break;
				}
				case FbxGeometryElement::eByPolygonVertex : {
					// The normal index is based off _favAddVert.u32IndexCount, which is
					//	incremented once for each vertex over which we loop.
					if ( plenNormals->GetReferenceMode() == FbxLayerElement::eDirect ) {
						// It is just _favAddVert.u32IndexCount.
						ffvThisVert.u32NormalIndex = _favAddVert.u32IndexCount;
					}
					else if ( plenNormals->GetReferenceMode() == FbxLayerElement::eIndexToDirect ) {
						// The normal index is found by plugging _favAddVert.u32IndexCount into an array of indices.
						ffvThisVert.u32NormalIndex = plenNormals->GetIndexArray().GetAt( _favAddVert.u32IndexCount );
					}
					break;
				}
				default : {
					std::cout << "Unsupported normal mapping mode." << std::endl;
				}
			}
		}
Vertices, vertex, colors, and UV coordinates all work the same way, though you can go over more layers if you want more UV sets (or you can just ignore all but the base layer).


L. Spiro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627722
    • Total Posts
      2978804
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now