[英]Why does my triangle in OpenGL (3) doesn't render using lwjgl?
So i started to learn lwjgl recently, and quickly realized that I need to improve in OpenGL to continue.所以我最近开始学习lwjgl,很快意识到我需要在OpenGL中改进才能继续。 I followed tutorial here and tried to implement same code using java and lwjgl (code in tutorial is in C++).
我按照教程here并尝试使用java和lwjgl实现相同的代码(教程中的代码是C++)。 I successfully managed to replicate OpenGL calls from tutorial using lwjgl api, but when I start my program i see only black screen and nothing else =( No triangle, nothing. No errors in console ether.
我成功地使用 lwjgl api 成功地复制了教程中的 OpenGL 调用,但是当我启动我的程序时,我只看到黑屏,没有其他任何东西=(没有三角形,什么都没有。控制台以太没有错误。
My Window.java class where i perform OpenGL rendering (Never mind Spring annotations, i'm just using IoC container to manage objects, method run()
is called after Spring context creation): My Window.java class where i perform OpenGL rendering (Never mind Spring annotations, i'm just using IoC container to manage objects, method
run()
is called after Spring context creation):
import com.gitlab.cvazer.dnd.map.desktop.ashlesy.Orchestrator;
import com.gitlab.cvazer.dnd.map.desktop.render.Camera;
import com.gitlab.cvazer.dnd.map.desktop.util.Shaders;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Objects;
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryUtil.NULL;
@Slf4j
@Component
public class Window {
private @Autowired Shaders shaders;
private @Getter long window;
//START HERE!
public void run() {
new Thread(() -> {
log.info("Hello LWJGL " + Version.getVersion() + "!");
init();
try {
loop();
} catch (IOException e) {
e.printStackTrace();
}
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}).start();
}
private void init() {
GLFWErrorCallback.createPrint(System.err).set();
if ( !glfwInit() ) throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(800, 600, "Hello World!", NULL, NULL);
if ( window == NULL ) throw new RuntimeException("Failed to create the GLFW window");
callbacks();
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
}
private void loop() throws IOException {
GL.createCapabilities();
int vertexArray = glGenVertexArrays();
glBindVertexArray(vertexArray);
int vertexBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
float[] data = {-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f};
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);
int program = shaders.loadShaders("vertex.glsl", "fragment.glsl");
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
long lastTime = System.currentTimeMillis();
while ( !glfwWindowShouldClose(window) ) {
long delta = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0,3,GL_FLOAT, false, 0, vertexBuffer);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window); // swap the color buffers
glfwPollEvents();
}
}
}
Shaders.java class:着色器.java class:
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.lwjgl.opengl.GL30.*;
@Slf4j
@Service
public class Shaders {
public int loadShaders(String vertexFilePath, String fragmentFilePath) throws IOException {
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
String vertexCode = Files.lines(Paths.get(vertexFilePath))
.collect(Collectors.joining("\n"));
String fragmentCode = Files.lines(Paths.get(fragmentFilePath))
.collect(Collectors.joining("\n"));
compileShader(vertexShader, vertexCode);
compileShader(fragmentShader, fragmentCode);
int program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
private void compileShader(int shader, String code){
glShaderSource(shader, code);
glCompileShader(shader);
String slog = glGetShaderInfoLog(shader);
if (slog.contentEquals("")) return;
log.info(slog);
}
}
vertex.glsl file: vertex.glsl文件:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main(){
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
fragment.glsl file:片段.glsl文件:
#version 330 core
out vec3 color;
void main(){
color = vec3(1,0,0);
}
I followed part 1 (coding OpenGL calls) and 2 (coding GLSL shaders, loading them) of this tutorial, but adding shaders didn't fixed my problem我遵循了本教程的第 1 部分(编码 OpenGL 调用)和第 2 部分(编码 GLSL 着色器,加载它们),但添加着色器并没有解决我的问题
I don't think that googling further can give me answers since almost all tutorials online on topic of lwjgl use OpenGL 1 for rendering.我不认为进一步搜索可以给我答案,因为几乎所有关于 lwjgl 主题的在线教程都使用 OpenGL 1 进行渲染。
How can i make this work?我怎样才能使这项工作?
The issue is the line问题是线路
glVertexAttribPointer(0,3,GL_FLOAT, false, 0, vertexBuffer);
If a named buffer object is bound then the last parameter of glVertexAttribPointer
is treated as a byte offset into the buffer object's data store.如果命名缓冲区 object 被绑定,则
glVertexAttribPointer
的最后一个参数被视为缓冲区对象数据存储的字节偏移量。
When you use glVertexAttribPointer
then you don't have to specify the vertex buffer by a parameter.当您使用
glVertexAttribPointer
时,您不必通过参数指定顶点缓冲区。 The function associates the vertex attribute to the buffer object, which is currently bound to the target GL_ARRAY_BUFFER
. function 将顶点属性与缓冲区 object 相关联,该缓冲区当前绑定到目标
GL_ARRAY_BUFFER
。
It has to be:它一定要是:
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
See also Java Code Examples for org.lwjgl.opengl.GL20.glVertexAttribPointer()另见Java 代码示例 org.lwjgl.opengl.GL20.glVertexAttribPointer()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.