Java Can';t使纹理均匀可变,以便在LWJGL 3中工作
所以我一直在网上学习大量的教程,并试图用OpenGL创建自己的图形引擎。问题是我无法使纹理工作。我可以在屏幕上渲染正方形并更改颜色,但我不能显示纹理 这是主要的方法:Java Can';t使纹理均匀可变,以便在LWJGL 3中工作,java,opengl,textures,shader,lwjgl,Java,Opengl,Textures,Shader,Lwjgl,所以我一直在网上学习大量的教程,并试图用OpenGL创建自己的图形引擎。问题是我无法使纹理工作。我可以在屏幕上渲染正方形并更改颜色,但我不能显示纹理 这是主要的方法: public class Main implements Runnable{ public int HEIGHT = 400; public int WIDTH = 400; private boolean running; private Thread thread; private long window; Level
public class Main implements Runnable{
public int HEIGHT = 400;
public int WIDTH = 400;
private boolean running;
private Thread thread;
private long window;
Level level;
public Main(){
}
public void start(){
running = true;
thread = new Thread(this, "Game");
thread.start();
}
public void stop(){
try {
thread.join();
running = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void init(){
if(glfwInit() != GL_TRUE){
System.err.println("Couldn't load the window");
return;
}
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, "Game!!", NULL, NULL);
if(window == NULL){
System.err.println("Couldn't create the window");
return;
}
ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - WIDTH) / 2, (GLFWvidmode.height(vidmode) - HEIGHT) / 2);
glfwMakeContextCurrent(window);
glfwShowWindow(window);
GLContext.createFromCurrent();
Input input = new Input();
glfwSetKeyCallback(window, input);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
System.out.println("OpenGL: " + glGetString(GL_VERSION));
Shader.loadAll();
Matrix4f pr_matrix = Matrix4f.orthographic(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
Shader.BG.setUniformMat4f("pr_matrix", pr_matrix);
Shader.BG.setUniform1i("tex", 1);
level = new Level();
}
public void run() {
init();
long lastTime = System.nanoTime();
double delta = 0.0;
double ns = 1000000000.0 / 60.0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1.0) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
if (glfwWindowShouldClose(window) == GL_TRUE)
running = false;
}
}
private void update(){
glfwPollEvents();
if(Input.isKeyDown(GLFW_KEY_ESCAPE)){
running = false;
}
}
private void render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int error = glGetError();
if (error != GL_NO_ERROR)
System.out.println(error);
glfwSwapBuffers(window);
level.render();
}
public static void main(String arg[]){
new Main().start();
}
}
这是级别方法:
public class Level {
private VertexArray background;
private Texture bgTexture;
public Level() {
float[] vertices = new float[] {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f
};
byte[] indices = new byte[] { 0, 1, 3, 3, 1, 2 };
float[] tcs = new float[] {
0f, 0f,
0, 1.0f,
1.0f, 1.0f,
1.0f, 0
};
background = new VertexArray(vertices, indices, tcs);
bgTexture = new Texture("res/bird.png");
}
public void render() {
Shader.BG.enable();
bgTexture.bind();
background.bind();
background.draw();
bgTexture.unbind();
Shader.BG.disable();
}
这里是纹理类:
public class Texture {
private int width, height;
private int texture;
public Texture(String path) {
texture = load(path);
}
private int load(String path) {
int[] pixels = null;
try {
BufferedImage image = ImageIO.read(new FileInputStream(path));
width = image.getWidth();
height = image.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int[] data = new int[width * height];
for (int i = 0; i < width * height; i++) {
int a = (pixels[i] & 0xff000000) >> 24;
int r = (pixels[i] & 0xff0000) >> 16;
int g = (pixels[i] & 0xff00) >> 8;
int b = (pixels[i] & 0xff);
data[i] = a << 24 | b << 16 | g << 8 | r;
}
int result = glGenTextures();
glBindTexture(GL_TEXTURE_2D, result);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, BufferUtils.createIntBuffer(data));
glBindTexture(GL_TEXTURE_2D, 0);
return result;
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, texture);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
}
这里是着色器:
顶点:
#version 400 core
in vec4 position;
layout (location = 1) in vec2 tc;
uniform mat4 pr_matrix;
out vec2 tc_vr;
void main(void)
{
gl_Position = pr_matrix * position;
tc_vr = tc;
}
片段:
#version 400 core
out vec4 color;
in vec2 tc_vr;
uniform sampler2D tex;
void main(void)
{
color = texture(tex, tc_vr);
}
非常感谢你的帮助 您正在将
tex
uniform设置为1,这意味着您使用纹理单元1。但是我没有看到任何东西将纹理绑定到纹理单元1。您可以尝试将统一设置为0,这是默认的纹理单位。@RetoKoradi非常感谢!它解决了问题,我没有意识到我没有在任何地方声明纹理单元。
#version 400 core
out vec4 color;
in vec2 tc_vr;
uniform sampler2D tex;
void main(void)
{
color = texture(tex, tc_vr);
}