Read the file name.TIF with the image and put it in the buffer for the image:

public static ByteBuffer loadTexture(String path) { BufferedImage image = null; try { InputStream in = new FileInputStream(path); image = ImageIO.read(in); } catch (IOException ex) { throw new RuntimeException("Failed to load a texture file!" + System.lineSeparator() + ex.getMessage()); } if (image != null) { AffineTransform transform = AffineTransform.getScaleInstance(1f, -1f); transform.translate(0, -image.getHeight()); AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = operation.filter(image, null); int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width * height]; image.getRGB(0, 0, width, height, pixels, 0, width); ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int pixel = pixels[y * width + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); buffer.put((byte) ((pixel >> 8) & 0xFF)); buffer.put((byte) (pixel & 0xFF)); buffer.put((byte) ((pixel >> 24) & 0xFF)); } } buffer.flip(); return buffer; } else { throw new RuntimeException("File extension not supported!" + System.lineSeparator() + "The following file extensions " + "are supported: " + Arrays.toString(ImageIO.getReaderFileSuffixes())); } } 

The class responsible for rendering the model (shortened as much as possible) builds a model from an obj file in triangles:

  private final int vaoTriangles; private final int shaderProgram; private ObjModel objModel;//модель обж файла SceneRenderer(ObjModel objModel) throws Exception { this.objModel = objModel; 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); int vertexBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, Geometry.objFileObject(objModel).rewind(), GL_STATIC_DRAW); int normalBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glBufferData(GL_ARRAY_BUFFER, Geometry.getNormals(objModel).rewind(), GL_STATIC_DRAW); vaoTriangles = glGenVertexArrays(); glBindVertexArray(vaoTriangles); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, false, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glVertexAttribPointer(normalLocation, 3, 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) .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); glBindVertexArray(vaoTriangles); glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(normalLocation); glDrawArrays(GL_TRIANGLES, 0, objModel.getIdx().size()); } } 

Well, fragmentary shader, in which the color is filled:

 #version 330 core in vec3 normal_modelspace; in vec3 vertex_modelspace; out vec3 color; uniform vec3 light_worldspace; 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; color = vec3(0.0,1.0,0.0) * (cosTheta + ambient); } 
  • If there are any questions about the code, please write and write explanations - Maxim Savelyev
  • one
    What exactly is the question? Lessons / examples on the output of textures in OpenGL watched? - Kromster
  • @Kromster in the fragment shader, as I understand it, instead of the color on the output should be my texture? - Maxim Savelyev
  • This is how you decide to do it. Maybe the texture, and maybe something else. - Kromster
  • in OBJ format there are texture coordinates, where did you get them? Where is this code? I see the normals, I see the coordinates of the points, WHERE ARE THE TEXTURES COORDINATES? - FatherOfFiveChildren

0