Skip to content

Commit 39b0244

Browse files
committed
Add simple textured quad with OpenGL 3.0
1 parent c3c5af1 commit 39b0244

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 130
2+
3+
uniform sampler2D tex;
4+
5+
in vec2 texCoordsVarying;
6+
out vec4 color;
7+
8+
void main() {
9+
color = texture(tex, texCoordsVarying);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 130
2+
3+
in vec3 position;
4+
in vec2 texCoords;
5+
out vec2 texCoordsVarying;
6+
7+
void main() {
8+
texCoordsVarying = texCoords;
9+
gl_Position = vec4(position, 1.0);
10+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright LWJGL. All rights reserved.
3+
* License terms: http://lwjgl.org/license.php
4+
*/
5+
package org.lwjgl.demo.opengl.textures;
6+
7+
import org.lwjgl.BufferUtils;
8+
import org.lwjgl.glfw.*;
9+
import org.lwjgl.opengl.GL;
10+
import org.lwjgl.opengl.GLCapabilities;
11+
import org.lwjgl.opengl.GLUtil;
12+
import org.lwjgl.system.Callback;
13+
14+
import java.io.IOException;
15+
import java.nio.ByteBuffer;
16+
import java.nio.FloatBuffer;
17+
import java.nio.IntBuffer;
18+
19+
import static org.lwjgl.demo.opengl.util.DemoUtils.*;
20+
import static org.lwjgl.glfw.GLFW.*;
21+
import static org.lwjgl.opengl.GL11.*;
22+
import static org.lwjgl.opengl.GL15.*;
23+
import static org.lwjgl.opengl.GL20.*;
24+
import static org.lwjgl.opengl.GL30.*;
25+
import static org.lwjgl.stb.STBImage.*;
26+
import static org.lwjgl.system.MemoryUtil.*;
27+
28+
/**
29+
* Renders a simple textured quad using OpenGL 3.0.
30+
*
31+
* @author Kai Burjack
32+
*/
33+
public class SimpleTexturedQuad {
34+
35+
long window;
36+
int width = 1024;
37+
int height = 768;
38+
39+
int vao;
40+
int quadProgram;
41+
int quadProgram_inputPosition;
42+
int quadProgram_inputTextureCoords;
43+
44+
GLCapabilities caps;
45+
GLFWKeyCallback keyCallback;
46+
Callback debugProc;
47+
48+
void init() throws IOException {
49+
glfwInit();
50+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
51+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
52+
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
53+
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
54+
window = glfwCreateWindow(width, height, "Simple textured quad", NULL, NULL);
55+
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
56+
public void invoke(long window, int key, int scancode, int action, int mods) {
57+
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
58+
glfwSetWindowShouldClose(window, true);
59+
}
60+
});
61+
glfwMakeContextCurrent(window);
62+
glfwShowWindow(window);
63+
IntBuffer framebufferSize = BufferUtils.createIntBuffer(2);
64+
nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
65+
width = framebufferSize.get(0);
66+
height = framebufferSize.get(1);
67+
caps = GL.createCapabilities();
68+
debugProc = GLUtil.setupDebugMessageCallback();
69+
createTexture();
70+
createQuadProgram();
71+
createFullScreenQuad();
72+
}
73+
74+
void createQuadProgram() throws IOException {
75+
int program = glCreateProgram();
76+
int vshader = createShader("org/lwjgl/demo/opengl/textures/texturedQuad.vs", GL_VERTEX_SHADER);
77+
int fshader = createShader("org/lwjgl/demo/opengl/textures/texturedQuad.fs", GL_FRAGMENT_SHADER);
78+
glAttachShader(program, vshader);
79+
glAttachShader(program, fshader);
80+
glLinkProgram(program);
81+
int linked = glGetProgrami(program, GL_LINK_STATUS);
82+
String programLog = glGetProgramInfoLog(program);
83+
if (programLog != null && programLog.trim().length() > 0)
84+
System.err.println(programLog);
85+
if (linked == 0)
86+
throw new AssertionError("Could not link program");
87+
glUseProgram(program);
88+
int texLocation = glGetUniformLocation(program, "tex");
89+
glUniform1i(texLocation, 0);
90+
quadProgram_inputPosition = glGetAttribLocation(program, "position");
91+
quadProgram_inputTextureCoords = glGetAttribLocation(program, "texCoords");
92+
glUseProgram(0);
93+
this.quadProgram = program;
94+
}
95+
96+
void createFullScreenQuad() {
97+
vao = glGenVertexArrays();
98+
glBindVertexArray(vao);
99+
int positionVbo = glGenBuffers();
100+
FloatBuffer fb = BufferUtils.createFloatBuffer(2 * 6);
101+
fb.put(-1.0f).put(-1.0f);
102+
fb.put(1.0f).put(-1.0f);
103+
fb.put(1.0f).put(1.0f);
104+
fb.put(1.0f).put(1.0f);
105+
fb.put(-1.0f).put(1.0f);
106+
fb.put(-1.0f).put(-1.0f);
107+
fb.flip();
108+
glBindBuffer(GL_ARRAY_BUFFER, positionVbo);
109+
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
110+
glVertexAttribPointer(quadProgram_inputPosition, 2, GL_FLOAT, false, 0, 0L);
111+
glEnableVertexAttribArray(quadProgram_inputPosition);
112+
int texCoordsVbo = glGenBuffers();
113+
fb = BufferUtils.createFloatBuffer(2 * 6);
114+
fb.put(0.0f).put(1.0f);
115+
fb.put(1.0f).put(1.0f);
116+
fb.put(1.0f).put(0.0f);
117+
fb.put(1.0f).put(0.0f);
118+
fb.put(0.0f).put(0.0f);
119+
fb.put(0.0f).put(1.0f);
120+
fb.flip();
121+
glBindBuffer(GL_ARRAY_BUFFER, texCoordsVbo);
122+
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
123+
glVertexAttribPointer(quadProgram_inputTextureCoords, 2, GL_FLOAT, true, 0, 0L);
124+
glEnableVertexAttribArray(quadProgram_inputTextureCoords);
125+
glBindBuffer(GL_ARRAY_BUFFER, 0);
126+
glBindVertexArray(0);
127+
}
128+
129+
static void createTexture() throws IOException {
130+
IntBuffer width = BufferUtils.createIntBuffer(1);
131+
IntBuffer height = BufferUtils.createIntBuffer(1);
132+
IntBuffer components = BufferUtils.createIntBuffer(1);
133+
ByteBuffer data = stbi_load_from_memory(ioResourceToByteBuffer("org/lwjgl/demo/opengl/textures/environment.jpg", 1024), width, height, components, 4);
134+
int id = glGenTextures();
135+
glBindTexture(GL_TEXTURE_2D, id);
136+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(), height.get(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
139+
stbi_image_free(data);
140+
}
141+
142+
void render() {
143+
glClear(GL_COLOR_BUFFER_BIT);
144+
glUseProgram(quadProgram);
145+
glBindVertexArray(vao);
146+
glDrawArrays(GL_TRIANGLES, 0, 6);
147+
glBindVertexArray(0);
148+
glUseProgram(0);
149+
}
150+
151+
void loop() {
152+
while (!glfwWindowShouldClose(window)) {
153+
glfwPollEvents();
154+
glViewport(0, 0, width, height);
155+
render();
156+
glfwSwapBuffers(window);
157+
}
158+
}
159+
160+
void run() throws IOException {
161+
init();
162+
loop();
163+
if (debugProc != null)
164+
debugProc.free();
165+
keyCallback.free();
166+
glfwDestroyWindow(window);
167+
glfwTerminate();
168+
}
169+
170+
public static void main(String[] args) throws IOException {
171+
new SimpleTexturedQuad().run();
172+
}
173+
174+
}

0 commit comments

Comments
 (0)