import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL15.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
public class man {
private static final int VERTEX_COUNT = 128;
private static final float SIZE = 1;
public static int count;
public static int vid;
public static int iid;
public static int program;
public static float fov = 70f;
public static float near = 0.1f;
public static float far = 1000f;
public static Matrix4f modelMatrix = new Matrix4f() ;
public static Matrix4f view = new Matrix4f();
public static Matrix4f projectionmatrix = new Matrix4f();
public static GLFWCursorPosCallback mouseCallback;
public static boolean t = true;
public static void main(String[] argv) throws IOException {
glfwInit();
int prog = 0 , id=0;
long window = glfwCreateWindow(1920,
1080, "HI", 0 , 0);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
// glfwSetCursorPosCallback(window, mouseCallback = new mouse());
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
new man().createShader();
new man().bind();
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glLoadIdentity();
glEnable(GL_PROJECTION_MATRIX);
glEnable(GL_PROJECTION);
new man().createprojection();
new man().createview();
glUseProgram(program);
new man().loadtoshader();
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vid);
glVertexPointer(3, GL_FLOAT , 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iid);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisable(GL_VERTEX_ARRAY);
}
}
public void createShader() throws IOException
{
StringBuilder vertex = new StringBuilder();
StringBuilder frag = new StringBuilder();
BufferedReader vert ,fragment;
vert = new BufferedReader(new FileReader("D:/work/opengl2/src/opengl2/vertexShader.txt"));
fragment = new BufferedReader(new FileReader("D:/work/opengl2/src/opengl2/frageShader.txt"));
//vertex Shader
String line, line2;
while ( (line = vert.readLine()) != null)
{
vertex.append(line).append('\n');
}
while ( (line2 = fragment.readLine()) != null)
{
frag.append(line2).append('\n');
}
//create and comile shaders
int vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glShaderSource(vertexShader, vertex);
GL20.glCompileShader(vertexShader);
int fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(fragmentShader, frag);
GL20.glCompileShader(fragmentShader);
program = GL20.glCreateProgram();
GL20.glAttachShader(program, vertexShader);
GL20.glAttachShader(program, fragmentShader);
GL20.glBindAttribLocation(program, 0 , "postion");
GL20.glLinkProgram(program);
if (GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) != 1)
{
System.err.println(GL20.glGetProgramInfoLog(program));
System.exit(1);
}
GL20.glValidateProgram(program);
}
public void bind()
{
float[] vertices = new float[128 * 128 * 3];
int[] indices = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)];
//generaete terrain
int vertexPointer = 0;
for(int i=0;i<VERTEX_COUNT;i++){
for(int j=0;j<VERTEX_COUNT;j++){
vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * SIZE;
vertices[vertexPointer*3+1] = 0;
vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
vertexPointer++;
}
}
int pointer = 0;
for(int gz=0;gz<VERTEX_COUNT-1;gz++){
for(int gx=0;gx<VERTEX_COUNT-1;gx++){
int topLeft = (gz*VERTEX_COUNT)+gx;
int topRight = topLeft + 1;
int bottomLeft = ((gz+1)*VERTEX_COUNT)+gx;
int bottomRight = bottomLeft + 1;
indices[pointer++] = topLeft;
indices[pointer++] = bottomLeft;
indices[pointer++] = topRight;
indices[pointer++] = topRight;
indices[pointer++] = bottomLeft;
indices[pointer++] = bottomRight;
}
}
//end generate terrain
FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length);
buffer.put(vertices);
buffer.flip();
count = indices.length ;
vid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vid);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
iid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, iid);
IntBuffer indbuf = BufferUtils.createIntBuffer(indices.length);
indbuf.put(indices);
indbuf.flip();
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indbuf, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
public void createprojection()
{
float aspect = (float) 1920/1080;
float y_scale = (float) (1/Math.tan(Math.toRadians(fov/2f)));
float x_scale = y_scale / aspect;
float frustum_length = far - near;
projectionmatrix.m00 = x_scale;
projectionmatrix.m11 = y_scale;
projectionmatrix.m22 = -((far + near) / frustum_length);
projectionmatrix.m23 = -1;
projectionmatrix.m32 = -((2 * near * far) / frustum_length);
projectionmatrix.m33 = 0;
}
public void createview()
{
Vector3f modelPos = null;
Vector3f modelAngle = null;
Vector3f modelScale = null;
Vector3f camera = new Vector3f(0,1f,-2f);
modelPos = new Vector3f(0,0,0);
modelAngle = new Vector3f(0,0,0);
modelScale = new Vector3f(1, 1, 1);
modelMatrix.setIdentity();
Matrix4f.scale(modelScale, modelMatrix, modelMatrix);
Matrix4f.translate(modelPos, modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.z), new Vector3f(0, 0, 1),
modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.y), new Vector3f(0, 1, 0),
modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(modelAngle.x), new Vector3f(1, 0, 0),
modelMatrix, modelMatrix);
Matrix4f.translate(camera, view, view);
}
public void loadtoshader()
{
int loc1 = glGetUniformLocation(program, "projection");
FloatBuffer matrixx = BufferUtils.createFloatBuffer(16);
projectionmatrix.store(matrixx);
matrixx.flip();
glUniformMatrix4fv(loc1, false,matrixx);
int loc2 = glGetUniformLocation(program, "view");
FloatBuffer matrixx2 = BufferUtils.createFloatBuffer(16);
view.store(matrixx2);
matrixx2.flip();
glUniformMatrix4fv(loc2, false,matrixx2);
int loc3 = glGetUniformLocation(program, "model");
FloatBuffer matrixx3 = BufferUtils.createFloatBuffer(16);
modelMatrix.store(matrixx3);
matrixx3.flip();
glUniformMatrix4fv(loc3, false,matrixx3);
}
}
class mouse extends GLFWCursorPosCallback
{
@Override
public void invoke(long arg0, double x, double y) {
// TODO Auto-generated method stub
System.out.println(x+ " "+ y );
}
}
你好!
我想在lwjgl中生成一个简单的平坦地形,但是这段代码不会产生任何东西。我在本章中是非常新的,所以如果你能解释为什么这段代码什么都不做,那就去吧!我使用ThinMatrix共享的代码生成地形,然后我将顶点上传到缓冲区,然后在主游戏循环中渲染它们。当我编译它时,它显示我一个黑屏。我已经搜索了很多教程,但我找不到任何可以帮助我的东西。
答案 0 :(得分:1)
我发现您的代码有几个问题:
问题1:您正在循环的每次迭代中生成man
类的新实例。在循环外部创建对象的单个实例,并在循环中使用它。
问题2:您正在使用旧的静态管道OpenGL。在ThinMatrix的教程之后,您应该使用OpenGL 3或更高版本。一切都应该通过着色器,而不是使用GL_PROJECTION_MATRIX
等等。
您的程序应如下所示:
man instance = new man()
while (!glfwWindowShouldClose(window))
{
RENDER THE TERRAIN HERE
glfwPoll
glfwSwapBuffers
glClear
}
如果你想要一些示例代码,我有一个应该有帮助的存储库here,就像我以前观看ThinMatrix教程一样。
编辑:解决方案
顶点着色器代码应该读取:
gl_Position = projection * view * model * position;
答案 1 :(得分:0)
我的着色器:片段
Character
顶点:
LazyIterable<Character> lazyIterable = abc.asLazy().collect(Character::valueOf);
List<Character> list = lazyIterable.toList();
Set<Character> set = lazyIterable.toSet();
Bag<Character> set = lazyIterable.toBag();
我更新了我的java代码,因此它根据顶点位置呈现不同的颜色。