gpt4 book ai didi

Java渲染LWJGL 3.0.0

转载 作者:行者123 更新时间:2023-12-01 18:05:34 24 4
gpt4 key购买 nike

在这段代码中,我尝试在屏幕上渲染一个简单的三角形,但它只显示红色背景,我已经尝试了我所知道的所有调试方法,但我只是不明白为什么它不起作用

希望有人能帮助我,先谢谢了。

public float vertices[] = new float[]
{
-0.5f,-0.5f,0.0f,1.0f,

0.0f,1.0f,0.0f,1.0f,

0.0f,0.5f,0.0f,1.0f,

0.0f,1.0f,0.0f,1.0f,

0.5f,-0.5f,0.0f,1.0f,

0.0f,1.0f,0.0f,1.0f
};



public TEST()
{

}


public int start() throws IOException
{
glfwInit();

long window = glfwCreateWindow(1000, 1000, "HAHA", NULL, NULL);
glfwMakeContextCurrent(window);
glfwShowWindow(window);
GLFWKeyCallback keycallback = new GLFWKeyCallback()
{

@Override
public void invoke(long window, int key, int scancode, int action, int mods)
{
if(key==GLFW_KEY_ESCAPE&&action==GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
};
glfwSetKeyCallback(window,keycallback );
GLFWErrorCallback errorcallback = new GLFWErrorCallback()
{

@Override
public void invoke(int error, long description)
{
System.out.println("ERROR CODE: " + error + " ERROR DESCRITPION: "+description);
}
};
glfwSetErrorCallback(errorcallback);


GL.createCapabilities();

glViewport(0, 0, 1000, 1000);
//////////////////////////
int VAO = glGenVertexArrays();
int VBO = glGenBuffers();
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

FloatBuffer buffer = FloatBuffer.allocate(vertices.length);
buffer.clear();
buffer.put(vertices);
buffer.flip();
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

glVertexAttribPointer(0, 4, GL_FLOAT, false, 8, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, false, 8, 4);
glEnableVertexAttribArray(1);

buffer.clear();
buffer=null;
System.gc();

glBindVertexArray(0);
///////////////////////
///////////////////////
LoadFile loader = new LoadFile("res/shader.vs", "res/shader.frag");

int vertexshader = glCreateShader(GL_VERTEX_SHADER);
int fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(vertexshader, loader.readVertex());
glShaderSource(fragmentshader, loader.readFragment());

glCompileShader(vertexshader);
glCompileShader(fragmentshader);

System.out.println(glGetShaderInfoLog(vertexshader));
System.out.println(glGetShaderInfoLog(fragmentshader));


int program = glCreateProgram();

glAttachShader(program, vertexshader);
glAttachShader(program, fragmentshader);

glLinkProgram(program);

glDeleteShader(vertexshader);
glDeleteShader(fragmentshader);

System.out.println(glGetProgramInfoLog(program));
///////////////////////////

boolean running=true;
while(running&&glfwWindowShouldClose(window)==GLFW_FALSE)
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

glUseProgram(program);

glBindVertexArray(VAO);

glDrawArrays(GL_TRIANGLES, 0, 3);

glBindVertexArray(0);

glUseProgram(0);

glfwSwapInterval(1);
glfwSwapBuffers(window);
}

System.out.println(glGetError());
glfwTerminate();

return 0;
}

我的顶点着色器

#version 330 core

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 colorin;

out vec4 colorout;

void main()
{
gl_Position = position;
colorout=colorin;
}

我的片段着色器

#version 330 core

out vec4 color;

in vec4 colorout;

void main()
{
color = colorout;
}

最佳答案

编辑:我刚刚看到这是 1 天前的问题..但是哦好吧..

我在这里有我的怀疑:

FloatBuffer buffer = FloatBuffer.allocate(vertices.length);
buffer.clear();
buffer.put(vertices);
buffer.flip();
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

您是否尝试过用 lwjgl 库本身的 BufferUtils 替换它?结果会是这样的:

FloatBuffer buff = BufferUtils.createFloatBuffer(vertices.length);
buff.put(vertices);
buff.flip();

或者,如果您想要手动版本..

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * Float.BYTES);
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
floatBuffer.put(vertices);
floatBuffer.flip();

关于Java渲染LWJGL 3.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com