Schould是这样的:
glOrtho(-1, 1, 1, -1, -1, 1);
而不是:
glOrtho(0, 1000, 600, 0, -1, 1)
我的屏幕宽度为1000,高度为600。
我用OpenGL(lwjgl)在java中制作了一个2D游戏。我可以绘制一个三角形,但是在使用我的着色器时,它不会起作用。着色器没有编译任何错误,但是screeen变黑了。
我完全不知道该怎么做,我是否必须启用某些功能?
这是我在GLSL中的vertexShader代码
#version 430
layout (location = 0) in vec2 position;
void main()
{
color = vec4(1, 0, 0, 1);
gl_Position = vec4(position, -1, 1);
}
这是片段着色器:
#version 430
void main()
{
gl_FragColor = vec4(1, 1, 1, 1);
}
我最近发现如果我只运行片段着色器它可以工作,我得到指定的颜色。但是,如果我也运行顶点着色器,它都会变黑,所以问题出在顶点着色器中。
着色器类:
` import static org.lwjgl.opengl.GL20。; import static org.lwjgl.opengl.GL32。;
public class Shader {
private int program;
public Shader() {
program = glCreateProgram();
if (program == 0) {
System.err.println("ShaderProgram creation failed!!!, couldn't find valid memory location!");
}
}
public void addVertexShader(String text) {
addProgram(text, GL_VERTEX_SHADER);
}
public void addFragmentShader(String text) {
addProgram(text, GL_FRAGMENT_SHADER);
}
public void addGeometryShader(String text) {
addProgram(text, GL_GEOMETRY_SHADER);
}
public void addProgram(String shaderText, int type){
int shader = glCreateShader(type);
if (shader == 0){
System.err.println("Shader creation failed!!!");
}
glShaderSource(shader, shaderText);
glCompileShader(shader);
if(glGetShader(shader, GL_COMPILE_STATUS) == 0){
System.err.println(glGetShaderInfoLog(shader, 2048));
}
glAttachShader(program, shader);
}
public void compile(){
glLinkProgram(program);
if (glGetProgram(program, GL_LINK_STATUS) == 0){
System.err.println(glGetProgramInfoLog(program, 2048));
}
glValidateProgram(program);
if(glGetProgram(program, GL_VALIDATE_STATUS) == 0){
System.err.println(glGetProgramInfoLog(program, 2048));
}
}
public void bind(){
glUseProgram(program);
}
}`
主要java类:
`public class Main {
Mesh mesh;
Shader shader;
public Main() {
init();
mesh = new Mesh();
Vertex[] data = new Vertex[]{new Vertex(new Vector2f(500, 0)),
new Vertex(new Vector2f(1000, 600)),
new Vertex(new Vector2f(0, 600))};
mesh.addVertices(data);
shader = new Shader();
shader.addVertexShader(Util.loadShader("vertexShader.vs"));
shader.addFragmentShader(Util.loadShader("fragmentShader.fs"));
shader.compile();
renderLoop();
}
private void MainLoop() {
new Thread("Update"){
public void run(){
long lastTime = System.nanoTime();
final double ns = 100000000 / 60;
double delta = 0;
while (!Display.isCloseRequested()) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta > 1) {
delta--;
update();
}
}
}
}.start();
}
private void renderLoop() {
MainLoop();
long Timer = System.currentTimeMillis();
int fps = 0;
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
render();
Display.update();
fps++;
if(System.currentTimeMillis() - Timer > 1){
System.out.println("FPS: " + fps);
Display.setTitle("The Unknown Path || FPS: " + fps);
fps = 0;
Timer += 1000;
}
}
}
private void update() {
}
private void render() {
shader.bind();
mesh.render();
}
private void init() {
try {
Display.setDisplayMode(new DisplayMode(1000, 600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1000, 600, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
// glEnable(GL_TEXTURE_2D);
System.out.println(glGetString(GL_VERSION));
}
public static void main(String[] args) {
Main m = new Main();
System.exit(0);
}
}`
答案 0 :(得分:0)
Schould是这样的:
glOrtho(-1,1,1,-1,-1,1);
而不是:
glOrtho(0,1000,600,0,-1,1)
我的屏幕宽度为1000,高度为600。