Hello, I couldn’t find the answer to such a question on the Internet: I read the obj file, form a model using it, display it on the screen - everything is fine (without textures). Then I read the name of the texture from the mtl file, then by the name I find the coordinates of the textures in the obj file - made ( faces ). In my obj file - 2 textures . One texture is inserted without problems, but so-called "holes" appear on the figure. When I substitute the second texture (to remove these “holes”), I have a texture change. (The first texture has its own texture coordinates, and the second has its own).
I created such a vertex shader:
#version 330 layout(location = 0) in vec3 vertexPos; layout(location = 1) in vec3 normal; layout (location = 2) in vec2 texCoord1; layout (location = 3) in vec2 texCoord2; out vec3 normal_modelspace; out vec3 vertex_modelspace; out vec2 TexCoord1; out vec2 TexCoord2; uniform mat4 P; uniform mat4 V; uniform mat4 M; void main() { TexCoord1 = texCoord1; TexCoord2 = texCoord2; vertex_modelspace = (M * vec4(vertexPos.xyz, 1.0)).xyz; gl_Position = P * V * vec4(vertex_modelspace.xyz, 1.0); normal_modelspace = (M * vec4(normal.xyz, 1.0)).xyz; vec3 vertexPosition_cameraspace = (V * M * vec4(vertexPos,1)).xyz; vec3 EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace; } Next, the fragment shader:
#version 330 core in vec3 normal_modelspace; in vec3 vertex_modelspace; in vec2 TexCoord1; in vec2 TexCoord2; out vec4 color; uniform vec3 light_worldspace; uniform int f; uniform sampler2D ourTexture1; uniform sampler2D ourTexture2; void main() { vec3 n = normalize(normal_modelspace); vec3 l = normalize(light_worldspace - vertex_modelspace); float cosTheta = clamp( dot( n, l), 0,1 ); float ambient = 0.05; if(f==1){ color = texture(ourTexture1, TexCoord1); } else color = texture(ourTexture2, TexCoord2); } Here f is an adapter between texture coordinates.
Below is the rendering class:
public SceneRenderer(ObjModel objModel, List<ObjTexture> objTextureList) throws Exception { glEnable(GL_DEPTH_TEST); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glPointSize(10); String fragmentSource = IOUtils.toString(getClass().getResourceAsStream("shader.frag")); String vertexSource = IOUtils.toString(getClass().getResourceAsStream("shader.vert")); shaderProgram = Util.createShaderProgram(vertexSource, fragmentSource); String simpleVertexSource = IOUtils.toString(getClass().getResourceAsStream("simple.vert")); String simpleFragmentSource = IOUtils.toString(getClass().getResourceAsStream("simple.frag")); simpleProgram = Util.createShaderProgram(simpleVertexSource, simpleFragmentSource); vaoTriangles = glGenVertexArrays(); glBindVertexArray(vaoTriangles); int vertexBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, Geometry.getVertexCoord(objModel).rewind(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); int normalBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glBufferData(GL_ARRAY_BUFFER, Geometry.getNormals(objModel).rewind(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); ObjTexture t1 = objTextureList.get(0); List<MultiIndex> multiIndices1 = objModel.getFaces(t1.getNameTexture()); vertexCount1 = multiIndices1.size(); int textureBuffer1 = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, textureBuffer1); glBufferData(GL_ARRAY_BUFFER, Geometry.getTexture(objModel, multiIndices1).rewind(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, textureBuffer1); glVertexAttribPointer(2, 2, GL_FLOAT, false, 0, 0); ObjTexture t = objTextureList.get(1); List<MultiIndex> multiIndices2 = objModel.getFaces(t.getNameTexture()); int textureBuffer2 = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, textureBuffer2); glBufferData(GL_ARRAY_BUFFER, Geometry.getTexture(objModel, multiIndices2).rewind(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, textureBuffer2); glVertexAttribPointer(3, 2, GL_FLOAT, false, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } void render() throws Exception { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer FloatBuffer vMatrix = BufferUtils.createFloatBuffer(16); new Matrix4f() .lookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f).get(vMatrix); FloatBuffer mMatrix = BufferUtils.createFloatBuffer(16); new Matrix4f().translate(modelPosition) .rotateX(modelRotation.x) .rotateY(modelRotation.y) .rotateZ(modelRotation.z) .scale(modelScale) .get(mMatrix); glUseProgram(shaderProgram); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "P"), false, pMatrix); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "V"), false, vMatrix); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "M"), false, mMatrix); glUniform3f(glGetUniformLocation(shaderProgram, "light_worldspace"), lightPosition.x, lightPosition.y, lightPosition.z); glUniform1i(glGetUniformLocation(shaderProgram, "f"), 1); if (texture1 == null) { texture1 = new Texture("src/main/resources/images/WOLF.TIF"); //glActiveTexture(GL_TEXTURE1); //glBindTexture(GL_TEXTURE_2D, texture1.getId()); //glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture1"),texture1.getId()); } glBindVertexArray(vaoTriangles); glEnableVertexAttribArray(vertexLocation); //glEnableVertexAttribArray(3); glEnableVertexAttribArray(2); glEnableVertexAttribArray(normalLocation); glDrawArrays(GL_TRIANGLES, 0, vertexCount1); } With one texture, everything is fine. Now I'm trying to add another texture in the render method:
if(texture2==null){ texture2 = new Texture("src/main/resources/images/EAGLE2.TIF"); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2.getId()); glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture2"),texture2.getId()); } Error starts:
A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff9b7036d39, pid=3728, tid=1000 The method Texture itself after reading the texture in bytes contains the following lines:
buf.flip(); int textureId = glGenTextures(); glBindTexture(GL_TEXTURE_2D, textureId); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); glGenerateMipmap(GL_TEXTURE_2D);