Skip to content

Instantly share code, notes, and snippets.

@Flandre923
Created December 15, 2023 13:37
Show Gist options
  • Save Flandre923/e0e1c5eb1fbfd712c27b54a3c3b6dc35 to your computer and use it in GitHub Desktop.
Save Flandre923/e0e1c5eb1fbfd712c27b54a3c3b6dc35 to your computer and use it in GitHub Desktop.
OpengGL10基础光照-漫反射
#version 330 core
in vec3 FragPos;
in vec3 Normal;
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;
void main()
{
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 result = (ambient + diffuse) * objectColor;
FragColor = vec4(result, 1.0);
}
package org.example;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import java.io.IOException;
import java.nio.FloatBuffer;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
public class Main {
private static final int SCR_WIDTH = 800;
private static final int SCR_HEIGHT = 600;
static final String vertexShaderSource = "vertex.glsl";
static final String fragmentShaderSource = "fragment.glsl";
private static float deltaTime = 0.0f;
private static float lastFrame = 0.0f;
private static float lastX = SCR_WIDTH/2;
private static float lastY = SCR_HEIGHT/2;
private static boolean firstMouse = true;
private static Camera camera = new Camera(new Vector3f(0,0,3),new Vector3f(0,1,0),0,0);
private static Vector3f lightPos = new Vector3f(1.2f,1.0f,2.0f);
public static void main(String[] args) throws IOException {
// glfw: initialize and configure
// ------------------------------
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
// GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
// glfw window creation
// --------------------
long window = GLFW.glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, 0);
if(window==0){
System.err.println("Failed to create GLFW window");
GLFW.glfwTerminate();
return;
}
GLFW.glfwMakeContextCurrent(window);
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED);
GLFW.glfwSetFramebufferSizeCallback(window,(window1, width, height) -> glViewport(0,0,width,height));
GLFW.glfwSetCursorPosCallback(window, Main::mouseCallback);
GLFW.glfwSetScrollCallback(window, Main::scrollCallback);
// creates the necessary function pointers for OpenGL's functions, making them accessible and usable within your Java code.
GL.createCapabilities();
//开日Z缓冲
glEnable(GL_DEPTH_TEST);
MyShader shaderProgram = new MyShader(vertexShaderSource,fragmentShaderSource);
MyShader lightShader = new MyShader("lightcube.vert","lightcube.frag");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float[] vertices = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 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Arrays();
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 6 * Float.BYTES, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 6 * Float.BYTES, 3 * Float.BYTES);
glEnableVertexAttribArray(1);
int lightCubeVAO = glGenVertexArrays();
glBindVertexArray(lightCubeVAO);
// we only need to bind to the VBO (to link it with glVertexAttribPointer), no need to fill it; the VBO's data already contains all we need (it's already bound, but we do it again for educational purposes)
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 6 *Float.BYTES, 0);
glEnableVertexAttribArray(0);
while(!GLFW.glfwWindowShouldClose(window)){
// input
// -----
processInput(window);
float currentFrame = (float) GLFW.glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// render
// -----
glClearColor(0.2f,0.2f,0.2f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram.use();
shaderProgram.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
shaderProgram.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
shaderProgram.setVec3("lightPos", lightPos.x,lightPos.y,lightPos.z);
Matrix4f projection = new Matrix4f().perspective((float) Math.toRadians(camera.zoom),SCR_WIDTH/SCR_HEIGHT,0.1f,100f);
Matrix4f view = camera.getViewMatrix();
shaderProgram.setMat4("projection", projection);
shaderProgram.setMat4("view",view);
shaderProgram.setMat4("model",new Matrix4f());
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0 , 36);
lightShader.use();
lightShader.setMat4("projection",projection);
lightShader.setMat4("view",view);
lightShader.setMat4("model",new Matrix4f().translate(lightPos).scale(0.2f));
glBindVertexArray(lightCubeVAO);
glDrawArrays(GL_TRIANGLES, 0 , 36);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(VAO);
glDeleteBuffers(VBO);
glDeleteProgram(shaderProgram.ID);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
GLFW.glfwTerminate();
}
public static void processInput(long window){
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_ESCAPE) == GLFW.GLFW_PRESS) {
GLFW.glfwSetWindowShouldClose(window, true);
}
float cameraSpeed = 2.5f * deltaTime;// adjust accordingly
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_W) == GLFW.GLFW_PRESS)
camera.processKeyboard(CameraMovement.FORWARD, deltaTime);
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_S) == GLFW.GLFW_PRESS)
camera.processKeyboard(CameraMovement.BACKWARD, deltaTime);
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_A) == GLFW.GLFW_PRESS)
camera.processKeyboard(CameraMovement.LEFT, deltaTime);
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_D) == GLFW.GLFW_PRESS)
camera.processKeyboard(CameraMovement.RIGHT, deltaTime);
}
public static void mouseCallback(long window,double xpos,double ypos){
if(firstMouse) // 这个bool变量初始时是设定为true的
{
lastX = (float) xpos;
lastY = (float) ypos;
firstMouse = false;
}
float xoffset = (float) (xpos - lastX);
float yoffset = (float) (lastY - ypos); // 注意这里是相反的,因为y坐标是从底部往顶部依次增大的
lastX = (float) xpos;
lastY = (float) ypos;
camera.processMouseMovement(xoffset,yoffset,true);
}
public static void scrollCallback(long window, double xoffset, double yoffset)
{
camera.processMouseScroll((float) yoffset);
}
}
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = aNormal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment