如何为高度贴图添加纹理?我按照TheCodingUniverse的教程#36(我认为是数字)来创建高度贴图,他使用着色器来设置X高度的X颜色,但是我想为X高度设置XX纹理而不是每个高度的颜色。我该怎么做呢?
我尝试绑定纹理,因为我正在绘制三角形条(就像我在GL_QUADS的3D方格上所做的那样),但是高度图无法渲染。然而,世界上所有其他事物仍在继续呈现。
StackOverflow抱怨我的信息太少,但除了代码示例之外,我不确定除了代码样本还有什么要告诉你的。
package Terrain;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.glu.Sphere;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.collision.shapes.StaticPlaneShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.ConstraintSolver;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import utility.EulerCamera;
import utility.ShaderLoader;
import javax.imageio.ImageIO;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL20.*;
/**
* A 3D terrain loaded from a height-map and a lookup texture. Press 'L' to reload the shader and texture files. Press
* 'P' to switch between normal, point, and wire-frame mode. Press 'F' to flatten the terrain. Click here for an image:
* https://twitter.com/i/#!/CodingUniverse/media/slideshow?url=pic.twitter.com%2FDgMdZ5jm.
*
* @author Oskar Veerhoek
*/
public class TexturedTerrain {
private static final String WINDOW_TITLE = "Terrain!";
private static final int[] WINDOW_DIMENSIONS = {1200, 650};
// private static final EulerCamera camera = new EulerCamera.Builder().setPosition(-5.4f, 19.2f,
// 33.2f).setRotation(30, 61, 0).setAspectRatio(ASPECT_RATIO).setFieldOfView(60).build();
private static EulerCamera camera = new EulerCamera.Builder()
.setFieldOfView(60)
.setNearClippingPane(0.3f)
.setFarClippingPane(500)
.setPosition(0, 25, 15)
.build();
/**
* The shader program that will use the lookup texture and the height-map's vertex data to draw the terrain.
*/
public static int shaderProgram;
/**
* The texture that will be used to find out which colours correspond to which heights.
*/
public static int lookupTexture;
public static int textureTerrain;
/**
* The display list that will contain the height-map's vertex data.
*/
public static int heightmapDisplayList;
/**
* The points of the height. The first dimension represents the z-coordinate. The second dimension represents the
* x-coordinate. The float value represents the height.
*/
public static float[][] data;
/**
* Whether the terrain should vary in height or be displayed on a grid.
*/
private static boolean flatten = false;
public static void render() {
// Clear the pixels on the screen and clear the contents of the depth buffer (3D contents of the scene)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset any translations the camera made last frame update
glLoadIdentity();
// Apply the camera position and orientation to the scene
camera.applyTranslations();
if (flatten) {
glScalef(1, 0, 1);
}
// Render the heightmap using the shaders that are being used
glCallList(heightmapDisplayList);
}
private static void input() {
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
flatten = !flatten;
}
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE){
Display.destroy();
System.exit(0);
}
if (Keyboard.getEventKey() == Keyboard.KEY_L) {
// Reload the shaders and the heightmap data.
glUseProgram(0);
glDeleteProgram(shaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTextures(lookupTexture);
glDeleteTextures(textureTerrain);
setUpShaders();
setUpHeightmap();
}
if (Keyboard.getEventKey() == Keyboard.KEY_P) {
// Switch between normal mode, point mode, and wire-frame mode.
int polygonMode = glGetInteger(GL_POLYGON_MODE);
if (polygonMode == GL_LINE) {
glPolygonMode(GL_FRONT, GL_FILL);
} else if (polygonMode == GL_FILL) {
glPolygonMode(GL_FRONT, GL_POINT);
} else if (polygonMode == GL_POINT) {
glPolygonMode(GL_FRONT, GL_LINE);
}
}
}
}
if (Mouse.isButtonDown(0)) {
Mouse.setGrabbed(true);
} else if (Mouse.isButtonDown(1)) {
Mouse.setGrabbed(false);
}
if (Mouse.isGrabbed()) {
camera.processMouse(1, 80, -80);
}
camera.processKeyboard(16, 1);
}
private static int loadTexture(String path) throws IOException {
// Create an input stream for the 'lookup texture', a texture that will used by the fragment shader to
// determine which colour matches which height on the heightmap
FileInputStream inputStream = new FileInputStream(path);
// Create a class that will give us information about the image file (width and height) and give us the
// texture data in an OpenGL-friendly manner
PNGDecoder decoder = new PNGDecoder(inputStream);
// Create a ByteBuffer in which to store the contents of the texture. Its size is the width multiplied by
// the height and 4, which stands for the amount of bytes a float is in Java.
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
// 'Decode' the texture and store its data in the buffer we just created
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
// Make the contents of the ByteBuffer readable to OpenGL (and unreadable to us)
buffer.flip();
// Close the input stream for the heightmap 'lookup texture'
inputStream.close();
// Generate a texture handle for the 'lookup texture'
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texture);
// Hand the texture data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
public static BufferedImage image(){
BufferedImage heightmapImage = null;
try{
heightmapImage = ImageIO.read(new File("res/heightmap.bmp"));
}catch (IOException e){
e.printStackTrace();
}
if (heightmapImage == null){
return null;
}
return heightmapImage;
}
public static void setUpHeightmap() {
try {
// Load the heightmap-image from its resource file
BufferedImage heightmapImage = ImageIO.read(new File("res/heightmap.bmp"));
// Initialise the data array, which holds the heights of the heightmap-vertices, with the correct dimensions
data = new float[heightmapImage.getWidth()][heightmapImage.getHeight()];
// Lazily initialise the convenience class for extracting the separate red, green, blue, or alpha channels
// an int in the default RGB color model and default sRGB colourspace.
Color colour;
// Iterate over the pixels in the image on the x-axis
for (int z = 0; z < data.length; z++) {
// Iterate over the pixels in the image on the y-axis
for (int x = 0; x < data[z].length; x++) {
// Retrieve the colour at the current x-location and y-location in the image
colour = new Color(heightmapImage.getRGB(z, x));
// Store the value of the red channel as the height of a heightmap-vertex in 'data'. The choice for
// the red channel is arbitrary, since the heightmap-image itself only has white, gray, and black.
int y = colour.getRed();;
data[z][x] = y;
//xheight.put(x, y);
//zheight.put(z, y);
}
}
lookupTexture = loadTexture("res/heightmap_lookup.png");
//textureTerrain = loadTexture("res/heightmap.png");
} catch (IOException e) {
e.printStackTrace();
}
glBindTexture(GL_TEXTURE_2D, lookupTexture);
// Generate a display list handle for the display list that will store the heightmap vertex data
heightmapDisplayList = glGenLists(1);
// TODO: Add alternative VBO rendering for pseudo-compatibility with version 3 and higher.
glNewList(heightmapDisplayList, GL_COMPILE);
// Scale back the display list so that its proportions are acceptable.
//glScalef(0.4f, 0.4f, 0.4f);
// Iterate over the 'strips' of heightmap data.
for (int z = 0; z < data.length - 1; z++) {
// Render a triangle strip for each 'strip'.
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < data[z].length; x++) {
// Take a vertex from the current strip
//glTexCoord2f(0, 0);
//GL20.glUseProgram(0);
glVertex3f(x, data[z][x], z);
// Take a vertex from the next strip
//glTexCoord2f(1, 1);
glVertex3f(x, data[z + 1][x], z + 1);
/*
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); // put before every vertex
glVertex3f(-50,0 ,-50);
glTexCoord2f(1, 1);
glVertex3f(50, 0 ,-50);
glTexCoord2f(1, 0);
glVertex3f(50, 0 ,50);
glTexCoord2f(0, 0);
glVertex3f(-50, 0 ,50);
glEnd();
*/
}
glEnd();
}
glEndList();
}
public static void setUpShaders() {
shaderProgram = ShaderLoader.loadShaderPair("res/landscape.vs", "res/landscape.fs");
glUseProgram(shaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, lookupTexture);
glUniform1i(glGetUniformLocation(shaderProgram, "lookup"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureTerrain);
glUniform1i(glGetUniformLocation(shaderProgram, "terrain"), 1);
}
private static void cleanUp(boolean asCrash) {
glUseProgram(0);
glDeleteProgram(shaderProgram);
glDeleteLists(heightmapDisplayList, 1);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTextures(lookupTexture);
System.err.println(GLU.gluErrorString(glGetError()));
Display.destroy();
System.exit(asCrash ? 1 : 0);
}
private static void setUpMatrices() {
camera.applyPerspectiveMatrix();
}
private static void setUpStates() {
camera.applyOptimalStates();
glPointSize(1);
// Enable the sorting of shapes from far to near
glEnable(GL_DEPTH_TEST);
// Set the background to a blue sky colour
glClearColor(0, 0.75f, 1, 1);
// Remove the back (bottom) faces of shapes for performance
glEnable(GL_CULL_FACE);
}
private static void update() {
Display.update();
Display.sync(60);
}
private static void enterGameLoop() {
while (!Display.isCloseRequested()) {
render();
input();
update();
//TODO
//TODO
}
}
private static void setUpDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WINDOW_DIMENSIONS[0], WINDOW_DIMENSIONS[1]));
Display.setVSyncEnabled(true);
Display.setTitle(WINDOW_TITLE);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
cleanUp(true);
}
}
public static void main(String[] args) {
setUpDisplay();
setUpStates();
setUpHeightmap();
setUpShaders();
//setUpPhysics();
setUpMatrices();
enterGameLoop();
cleanUp(false);
}
}