Show Posts
|
Pages: [1]
|
6
|
Game Development / Newbie & Debugging Questions / Re: OpenGL rendering problem
|
on: 2014-06-20 13:43:08
|
Matrices in OpenGL are a very important concept. They basically define the viewing space of the screen. There are 3 main types of matrices in OpenGL: model, view and projection. Each mesh has its own model matrix which defines how it is transformed into world space. The view matrix is independent of the model matrix. It basically defines where the camera is, and is applied to every model after the model matrix. The projection matrix is what you can think of as your eyes, or the lenses of a camera. It defines what you can actually see and is finally multiplied by the modelview matrix. I just gave you a quick vague explanation, but if you want to learn more, I recommend you see this article: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/So back in the days of OpenGL all of the matrix math was done for you. You could just do a few function calls to set up the projection, and easily render 3D scenes. Modern versions, on the other hand, expect you to do all of that matrix math yourself. This is for a good reason too. OpenGL is not a math library, it is purely a graphics library. Since you are using a modern GL shading version, you would need to set up the matrices yourself. The article I linked has some great explanations on how you would go about setting up implementations for them, too. This is just temporary code to show you how it's done without getting into how to set up matrices. I recommend you completely move into the modern way of doing things, which you pretty much already are, but implement matrices and pass them in as uniform variables to the vertex shader. A few other things. You weren't flipping the buffer after you filled it with data which was a whole other problem which would have hindered it from working. I also moved the vertices back on the z axis a little so they wouldn't be clipped by the near clipping plane defined in the gluPerspective method call. One last thing I need to point out is that I used the lwjgl-utils library for the gluPerspective method. Here is your problem fixed using a bit older version of GL to set up the matrices: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import static org.lwjgl.opengl.GL11.GL_MODELVIEW; import static org.lwjgl.opengl.GL11.GL_PROJECTION; import static org.lwjgl.opengl.GL11.glLoadIdentity; import static org.lwjgl.opengl.GL11.glMatrixMode; import static org.lwjgl.util.glu.GLU.gluPerspective;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30;
public class Test { public static final int WIDTH = 800; public static final int HEIGHT = 600;
public static final String TITLE = "Learning OpenGL";
public static final boolean FULLSCREEN = false; public static final boolean RESIZABLE = false; public static final boolean VSYNC = false;
public static final int TARGET_FPS = 60;
private int program;
private int vertexBuffer; private int vertexArray;
private float[] vertexPositions = new float[] { 0.75f, 0.75f, -10f, 1.0f, 0.75f, -0.75f, -10f, 1.0f, -0.75f, -0.75f, -10f, 1.0f };
private String vertexShader2 = "#version 110\n" + "void main()\n" + "{\n" + "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + "}\n"; private String fragShader2 = "#version 110\n" + "void main()\n" + "{\n" + "gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" + "}\n";
private int createShader(int type, String code) { int shader;
shader = GL20.glCreateShader(type);
GL20.glShaderSource(shader, code); GL20.glCompileShader(shader);
if (GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) { System.out.println("Failed to compile a shader."); System.out.println(GL20.glGetShaderInfoLog(shader, 5000)); System.exit(1); }
return shader; }
private int createShaderProgram(int[] shaders) {
int program;
program = GL20.glCreateProgram();
for (int shader : shaders) GL20.glAttachShader(program, shader);
GL20.glLinkProgram(program);
if (GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) { System.out.println("Failed to link a program."); System.exit(1); }
for (int shader : shaders) GL20.glDetachShader(program, shader);
return program; }
public void initializeBuffer() { vertexBuffer = GL15.glGenBuffers();
FloatBuffer vertexPositionsBuffer = BufferUtils.createFloatBuffer(vertexPositions.length); vertexPositionsBuffer.put(vertexPositions);
vertexPositionsBuffer.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexPositionsBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
public void initializeProgram() { int[] shaderList = new int[] { createShader(GL20.GL_VERTEX_SHADER, vertexShader2), createShader(GL20.GL_FRAGMENT_SHADER, fragShader2) };
program = createShaderProgram(shaderList);
vertexArray = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vertexArray); }
public void displayTriangle() { GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL20.glUseProgram(program);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer); GL20.glEnableVertexAttribArray(0); GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
GL20.glDisableVertexAttribArray(0); GL20.glUseProgram(0); }
public Test() {
try { Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); Display.setTitle(TITLE); Display.setFullscreen(FULLSCREEN); Display.setResizable(RESIZABLE); Display.setVSyncEnabled(VSYNC);
Display.create(); } catch (LWJGLException e) { System.out.println("Display initialization error."); System.exit(0); }
glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70, WIDTH / HEIGHT, 0.1f, 1000f); glMatrixMode(GL_MODELVIEW);
initializeProgram(); initializeBuffer();
while (!Display.isCloseRequested()) { displayTriangle(); Display.update(); Display.sync(TARGET_FPS); } }
public static void main(String[] args) { new Test(); } }
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / Re: Should I even bother remembering depreciated rendering?
|
on: 2014-06-14 03:50:30
|
LWJGL is a wrapper around OpenGL, so really any OpenGL tutorial you follow could be adapted to use with LWJGL. The C++ calls are the same as the Java calls for the most part.
BTW, display lists are deprecated and FBOs(Frame Buffer Objects) were added in GL 3 whiles VBOs were added in GL 1.5. Like said above, it's fine to learn and remember deprecated rendering techniques, but remember that there are much faster approaches that you should use in an actual release.
|
|
|
11
|
Game Development / Newbie & Debugging Questions / Re: libgdx How to make multiple enemy arrays? >>Any Help<<
|
on: 2014-06-13 14:22:25
|
Before getting into game development, I would recommend at least a basic understanding of object oriented programming concepts. There's nothing wrong with venturing around, but you should at least know the fundamentals or else you'll probably end up copying and pasting a lot of code.
When dealing with things as dynamic as entities in games, I would actually use something like an ArrayList.
|
|
|
12
|
Game Development / Newbie & Debugging Questions / Re: Weird OpenGL Blending Issue
|
on: 2014-06-10 09:13:52
|
So to any of you who were wondering, what was screwing me over was an error where I was updating my uniforms - one of the least places that I would have thought the error would be. I needed to actually bind the shader before I set the uniforms. Since I wasn't, OpenGL was secretly(me not checking glGetError) throwing error 502, which basically means you called a method with an incorrect state. That prevented blending from working.
|
|
|
13
|
Game Development / Newbie & Debugging Questions / [SOLVED] Weird OpenGL Blending Issue
|
on: 2014-06-10 06:33:11
|
So I'm working on implementing a basic 2D forward rendering system right now with lighting, but there is a slight problem that I can't wrap my head around. All of the articles that I have read about blending point me right where I am now. In forward rendering, you need to sum up all of the fragment shading calculations to get your final scene with blending, but somehow blending is just not working for me. I apologize if this problem is extremely simple ahead of time as I haven't really worked with blending in OpenGL until just now. I have included what I think to be the relevant code. If you need to see any more of the code, or details, just let me know. Core class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| package gel.engine.core;
import gel.engine.graphics.RenderingEngine;
import static org.lwjgl.opengl.GL11.*; import gel.engine.input.Input; import gel.engine.window.Window;
public abstract class Core {
private static Core instance;
protected boolean running; protected int targetFps; protected int targetUps; protected ProfileTimer profileTimer; protected Window window; protected Input input; protected RenderingEngine renderingEngine;
public Core() { if (instance == null) {
instance = this;
this.running = false; this.targetFps = -1; this.targetUps = 60;
window = new Window();
} else { throw new IllegalStateException("Can not make more than one instance of Core"); } }
public static Core getInstance() { return instance; }
public static boolean created() { return (instance == null); }
public boolean isRunning() { return running; }
public int getTargetFps() { return targetFps; }
public int getTargetUps() { return targetUps; }
public ProfileTimer getProfileTimer() { return profileTimer; }
public Window getWindow() { return window; }
public Input getInput() { return input; }
public RenderingEngine getRenderingEngine() { return renderingEngine; }
public void setTargetFps(int target) { if (!running) { this.targetFps = target; } }
public void setTargetUps(int target) { if (!running) { this.targetUps = target; } }
public void start() { run(); }
public void stop() { running = false; }
private void run() {
initEngine();
running = true;
long lastSecond = System.nanoTime(); long thisFrame = System.nanoTime(); long lastFrame = System.nanoTime(); double updateTime = 1e9f / targetUps; double renderTime = 1e9f / targetFps; double accumulativeUpdateTime = 0; double accumulativeFrameTime = 0; long sleepTime = 0; int updateCount = 0; int frameCount = 0;
while (running) {
thisFrame = System.nanoTime(); profileTimer.updateDelta(thisFrame, lastFrame); lastFrame = thisFrame;
accumulativeUpdateTime += profileTimer.getDelta(); accumulativeFrameTime += profileTimer.getDelta();
if (targetUps != -1) { while (accumulativeUpdateTime >= updateTime) { updateGame(); updateCount++; accumulativeUpdateTime -= updateTime; } } else { updateGame(); updateCount++; accumulativeUpdateTime = 0; }
if (targetFps != -1) { if (accumulativeFrameTime >= renderTime) { renderGame(); frameCount++; accumulativeFrameTime = 0; } else {
sleepTime = Math.round((renderTime - accumulativeFrameTime) / (1024 * 1024));
try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); }
} } else { renderGame(); frameCount++; accumulativeFrameTime = 0; }
if (System.nanoTime() - lastSecond >= 1e9) { profileTimer.updateFps(frameCount); profileTimer.updateUps(updateCount); frameCount = 0; updateCount = 0; lastSecond = System.nanoTime(); }
if (window.isCloseRequested()) { stop(); }
}
unloadGame();
}
private void initEngine() {
window.create();
renderingEngine = new RenderingEngine(); profileTimer = new ProfileTimer(); input = new Input(); input.create();
loadGame(); }
private void loadGame() { load(); }
private void updateGame() { input.update(); update(); }
private void renderGame() { glClear(GL_COLOR_BUFFER_BIT); renderingEngine.update(); render(); window.update(); }
private void unloadGame() { unload(); }
public abstract void load();
public abstract void update();
public abstract void render();
public abstract void unload();
} |
RenderingEngine Class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| package gel.engine.graphics;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.glDisable; import gel.engine.core.Core; import gel.engine.graphics.lighting.AmbientLight; import gel.engine.graphics.lighting.PointLight; import gel.engine.graphics.matrix.RotationMatrix; import gel.engine.graphics.matrix.ScaleMatrix; import gel.engine.graphics.matrix.TransformationMatrix; import gel.engine.graphics.matrix.TranslationMatrix; import gel.engine.math.Vector2f;
public class RenderingEngine {
public static final String UNIFORM_MODEL_MATRIX = "modelMatrix"; public static final String UNIFORM_VIEW_MATRIX = "viewMatrix"; public static final String UNIFORM_PROJECTION_MATRIX = "projectionMatrix";
public static final String UNIFORM_AMBIENTCOLOR = "ambientColor";
private static RenderingEngine instance;
private VertexArray vertexArray; private OrthographicCamera camera; private ShaderProgram ambientShader; private ShaderProgram pointLightShader;
private AmbientLight ambient;
public RenderingEngine() { if (instance == null) { instance = this;
vertexArray = new VertexArray(); camera = new OrthographicCamera(0, Core.getInstance().getWindow().getWidth(), 0, Core.getInstance().getWindow().getHeight(), 1, -1);
ambientShader = new ShaderProgram(); ambientShader.createAndCompileVertexShader("res\\shaders\\ambientShader.vs"); ambientShader.createAndCompileFragmentShader("res\\shaders\\ambientShader.fs"); ambientShader.linkProgram();
pointLightShader = new ShaderProgram(); pointLightShader.createAndCompileVertexShader("res\\shaders\\pointLightShader.vs"); pointLightShader.createAndCompileFragmentShader("res\\shaders\\pointLightShader.fs"); pointLightShader.linkProgram();
ambient = new AmbientLight(new Color(255, 255, 255));
} else { throw new IllegalStateException("Can not make more than one instance of RenderingEngine"); } }
public static RenderingEngine getInstance() { return instance; }
public OrthographicCamera getCamera() { return camera; }
public AmbientLight getAmbientLight() { return ambient; }
public void update() { camera.update();
ambientShader.setMatrix4f(UNIFORM_VIEW_MATRIX, camera.getViewMatrix().getMatrix()); ambientShader.setMatrix4f(UNIFORM_PROJECTION_MATRIX, camera.getProjectionMatrix().getMatrix());
pointLightShader.setMatrix4f(UNIFORM_VIEW_MATRIX, camera.getViewMatrix().getMatrix()); pointLightShader.setMatrix4f(UNIFORM_PROJECTION_MATRIX, camera.getProjectionMatrix().getMatrix());
}
public void renderTexture(Texture t, float x, float y, float rotation) { if (x + t.getWidth() >= 0 && x < Core.getInstance().getWindow().getWidth() + t.getWidth() && y + t.getHeight() >= 0 && y < Core.getInstance().getWindow().getHeight() + t.getHeight()) { ambientShader.bind(); vertexArray.clear(); vertexArray.addVertex(new Vertex(new Vector2f(-t.getWidth() / 2, -t.getHeight() / 2), new Color(0), new Vector2f(1, 1))); vertexArray.addVertex(new Vertex(new Vector2f(-t.getWidth() / 2, t.getHeight() / 2), new Color(0), new Vector2f(1, 0))); vertexArray.addVertex(new Vertex(new Vector2f(t.getWidth() / 2, t.getHeight() / 2), new Color(0), new Vector2f(0, 0))); vertexArray.addVertex(new Vertex(new Vector2f(t.getWidth() / 2, -t.getHeight() / 2), new Color(0), new Vector2f(0, 1))); ambientShader.setMatrix4f(UNIFORM_MODEL_MATRIX, new TransformationMatrix(new TranslationMatrix(x + t.getWidth() / 2, y + t.getHeight() / 2), new RotationMatrix(rotation), new ScaleMatrix(1, 1)).getMatrix()); ambientShader.setVector3f(UNIFORM_AMBIENTCOLOR, ambient.getColor().toVector3f()); vertexArray.render(); } }
public void renderPointLight(PointLight light) { glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); if (light.getPosition().getX() + light.getRadius() >= 0 && light.getPosition().getX() < Core.getInstance().getWindow().getWidth() && light.getPosition().getY() + light.getRadius() >= 0 && light.getPosition().getY() < Core.getInstance().getWindow().getHeight()) { pointLightShader.bind(); vertexArray.clear(); vertexArray.addVertex(new Vertex(new Vector2f(-light.getRadius(), -light.getRadius()))); vertexArray.addVertex(new Vertex(new Vector2f(-light.getRadius(), light.getRadius()))); vertexArray.addVertex(new Vertex(new Vector2f(light.getRadius(), light.getRadius()))); vertexArray.addVertex(new Vertex(new Vector2f(light.getRadius(), -light.getRadius()))); pointLightShader.setMatrix4f(UNIFORM_MODEL_MATRIX, new TransformationMatrix(new TranslationMatrix(light.getPosition().getX(), light.getPosition().getY()), new RotationMatrix(0), new ScaleMatrix(1, 1)).getMatrix()); pointLightShader.setVector2f("pos", light.getPosition()); pointLightShader.setVector3f("color", light.getColor().toVector3f()); pointLightShader.setFloat("radius", light.getRadius()); pointLightShader.setFloat("constant", light.getConstAtten()); pointLightShader.setFloat("linear", light.getLinearAtten()); pointLightShader.setFloat("quadratic", light.getQuadAtten()); vertexArray.render(); } glDisable(GL_BLEND); }
} |
Game class(Subclass of Core): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package game;
import static org.lwjgl.opengl.GL11.*; import gel.engine.core.Core; import gel.engine.graphics.Bitmap; import gel.engine.graphics.Color; import gel.engine.graphics.Texture; import gel.engine.graphics.lighting.PointLight; import gel.engine.math.Vector2f;
public class Game extends Core {
private Texture grass; private PointLight light;
public static void main(String[] args) { Game g = new Game(); g.getWindow().setTitle("Gel Engine"); g.getWindow().setSize(16 * 60, 9 * 60); g.getWindow().setResizable(false); g.start(); }
@Override public void load() { grass = new Texture(new Bitmap("res\\grass.png")); light = new PointLight(new Vector2f(400, 400), new Color(255, 127, 0), 200, 0, 0, 0.01f); }
@Override public void update() {
}
@Override public void render() { renderingEngine.renderTexture(grass, 400, 400, 0); renderingEngine.renderPointLight(light); }
@Override public void unload() {
}
} |
Texture and Ambient Vertex Shader: 1 2 3 4 5 6 7 8 9 10 11
| #version 110
uniform mat4 modelMatrix; uniform mat4 viewMatrix; uniform mat4 projectionMatrix;
void main() { gl_Position = projectionMatrix * viewMatrix * modelMatrix * gl_Vertex; gl_TexCoord[0] = gl_MultiTexCoord0; } |
Ambient Fragment Shader: 1 2 3 4 5 6 7 8 9
| #version 110
uniform sampler2D textureSampler; uniform vec3 ambientColor;
void main() { gl_FragColor = vec4(ambientColor, 1.0) * texture2D(textureSampler, gl_TexCoord[0].st); } |
Point light fragment shader: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #version 110
uniform vec2 pos; uniform vec3 color; uniform float radius; uniform float constant; uniform float linear; uniform float quadratic;
void main() {
float dist = distance(gl_FragCoord.xy, pos); float atten = 1.0 / (quadratic * dist * dist + linear * dist + constant); gl_FragColor = vec4(atten, atten, atten, 1.0) * vec4(color, 1.0); } |
With this code, this is the current scene that I get:  If I comment out rendering the texture in the render() method in the Game class, I get the same results. Could blending be getting ignored or something alike? If I comment out where I render the point light in the render() method of the Game class I get the actual texture rendered, like so:  Out of those results, I would expect to get the grass texture additively blended in with the light, but that is obviously not so. If anyone who has a solution to this issue, it would be greatly appreciated.
|
|
|
|
|
ivj94
(584 views)
2018-03-24 14:47:39
ivj94
(48 views)
2018-03-24 14:46:31
ivj94
(382 views)
2018-03-24 14:43:53
Solater
(62 views)
2018-03-17 05:04:08
nelsongames
(109 views)
2018-03-05 17:56:34
Gornova
(159 views)
2018-03-02 22:15:33
buddyBro
(703 views)
2018-02-28 16:59:18
buddyBro
(92 views)
2018-02-28 16:45:17
xxMrPHDxx
(493 views)
2017-12-31 17:17:51
xxMrPHDxx
(733 views)
2017-12-31 17:15:51
|
java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org
|
|