Show Posts
|
|
Pages: [1]
|
|
1
|
Java Game APIs & Engines / OpenGL Development / Mouseinput and MatrixView (OpenGL 3.2)
|
on: 2013-02-10 14:31:50
|
Hello. I'm trying to implement/add mouse to my test app. My test app is one of the LWJGL OpenGL 3.2 app from wiki page http://lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matricesWhat I did: - before while loop in the constructor I add this part of code: 1 2
| Mouse.setCursorPosition(800/2, 600/2); Mouse.setGrabbed(true); |
- in method "logicCycle()" before "// Translate camera" i added this part of code: 1 2 3 4 5 6
| if(Mouse.isGrabbed()) { horizontalAngle+= mouseSpeed * Mouse.getDX(); verticalAngle+= mouseSpeed * Mouse.getDY();
mouse(); } |
where 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
| float horizontalAngle = 3.14f; float verticalAngle = 3.14f; private float mouseSpeed = 0.005f;
private Vector3f direction = new Vector3f(); private Vector3f right = new Vector3f(); private Vector3f up = new Vector3f();
public void mouse(){ direction.set( (float)Math.cos(verticalAngle) * (float)Math.sin(horizontalAngle), (float)Math.sin(verticalAngle), (float)Math.cos(verticalAngle) * (float)Math.cos(horizontalAngle)); right.set( (float)Math.sin(horizontalAngle - PI/2.0f), 0, (float)Math.cos(horizontalAngle - PI/2.0f)); up = cross(right, direction); viewMatrix.m00 = up.x; viewMatrix.m01 = up.y; viewMatrix.m02 = up.z; viewMatrix.m10 = right.x; viewMatrix.m11 = right.y; viewMatrix.m12 = right.z; viewMatrix.m20 = direction.x; viewMatrix.m21 = direction.y; viewMatrix.m22 = direction.z; }
private Vector3f cross(Vector3f b, Vector3f c){ Vector3f cross = new Vector3f(); cross.x = (b.y*c.z) - (b.z*c.y); cross.y = (b.z*c.x) - (b.x*c.z); cross.z = (b.x*c.y) - (b.y*c.x); return cross; } |
Of course mouse working but not correctly, camera is set in strange positions. Based on this example app (which display only square), when I turn to the right by 90 degrees and I move the mouse to up another 90 degrees square will be on bottom camera but still should be on the left. Please HELP I start freaking out how to count this matrix. I assume i have bad count in mouse method.
|
|
|
|
|
2
|
Java Game APIs & Engines / OpenGL Development / Re: Two squares/cubes (basic question OpenGL 3.x LWJGL)
|
on: 2013-01-27 15:10:39
|
You only have the vertices and colors for a single cube.
Hmmm... not quite function "CubeUtil.vertices" returns vertices for whole cube, function "CubeUtil.merge" marge two array so in variable "vertices" i have two arrays. First param in "CubeUtil.vertices" sets cube on differents X axis positions (-1.5f and 1.5f). Same situation in colors array. Or maybe there is some magic mistake which I don't see. Ideally you could just draw the single cube twice and adjust its location with the model matrix. Edit: Oh didn't see you weren't using matrices, maybe you should consider using some.
Sounds interesting but I'm to new in OpenGL and I don't know how to do this. I'm really appreciate your help. EDIT. I started checking my code in indices and vertices functions and it looks like there is a bug in this functions. Magn919 thanks for your help.
|
|
|
|
|
3
|
Java Game APIs & Engines / OpenGL Development / Re: Two squares/cubes (basic question OpenGL 3.x LWJGL)
|
on: 2013-01-27 14:11:52
|
Ok, true its working but when I want to draw cube I still have problem. My indices array If I'm not wrong should be 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
| byte[] indices = { 0, 1, 2, 2, 3, 0, 0, 4, 7, 7, 0, 3, 4, 7, 6, 6, 5, 4, 5, 6, 2, 2, 1, 5, 4, 0, 5, 5, 0, 1, 7, 3, 6, 6, 3, 2, 8, 9, 10, 10, 11, 8, 8, 12, 15, 15, 8, 11, 12, 15, 14, 14, 13, 12, 13, 14, 10, 10, 9, 13, 12, 8, 13, 13, 8, 9, 15, 11, 14, 14, 11, 10 }; |
where my vertices and colors array is 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
| float[] vertices = CubeUtil.merge(CubeUtil.vertices(-1.5f,0.0f,0.0f,0.5f,0.5f,0.5f), CubeUtil.vertices(1.5f,0.0f,0.0f,0.5f,0.5f,0.5f)); float[] colors = CubeUtil.merge(CubeUtil.colors(), CubeUtil.colors());
....
public static float[] vertices(float x, float y, float z, float sizeX, float sizeY, float sizeZ){ float[] vertices = { sizeX+x, sizeY+y, sizeZ+z, -sizeX+x, sizeY+y, sizeZ+z, -sizeX+x, -sizeY+y, sizeZ+z, sizeX+x, -sizeY+y, sizeZ+z, sizeX+x, sizeY+y, -sizeZ+z, -sizeX+x, sizeY+y, -sizeZ+z, -sizeX+x, -sizeY+y, -sizeZ+z, sizeX+x, -sizeY+y, -sizeZ+z, -sizeX+x, sizeY+y, sizeZ+z, -sizeX+x, -sizeY+y, sizeZ+z, -sizeX+x, -sizeY+y, -sizeZ+z, -sizeX+x, sizeY+y, -sizeZ+z, sizeX+x, sizeY+y, sizeZ+z, sizeX+x, -sizeY+y, sizeZ+z, sizeX+x, -sizeY+y, -sizeZ+z, sizeX+x, sizeY+y, -sizeZ+z, sizeX+x, sizeY+y, sizeZ+z, -sizeX+x, sizeY+y, sizeZ+z, -sizeX+x, sizeY+y, -sizeZ+z, sizeX+x, sizeY+y, -sizeZ+z, sizeX+x, -sizeY+y, sizeZ+z, -sizeX+x, -sizeY+y, sizeZ+z, -sizeX+x, -sizeY+y, -sizeZ+z, sizeX+x, -sizeY+y, -sizeZ+z }; return vertices; }
public static float[] merge(float[] a, float[] b) { int aLen = a.length; int bLen = b.length; float[] c = new float[aLen + bLen]; System.arraycopy(a, 0, c, 0, aLen); System.arraycopy(b, 0, c, aLen, bLen); return c; }
public static float[] colors(){ float[] colors = { 0.583f, 0.771f, 0.014f, 0.609f, 0.115f, 0.436f, 0.327f, 0.483f, 0.844f, 0.822f, 0.569f, 0.201f, 0.435f, 0.602f, 0.223f, 0.310f, 0.747f, 0.185f, 0.597f, 0.770f, 0.761f, 0.559f, 0.436f, 0.730f, 0.359f, 0.583f, 0.152f, 0.483f, 0.596f, 0.789f, 0.559f, 0.861f, 0.639f, 0.195f, 0.548f, 0.859f, 0.014f, 0.184f, 0.576f, 0.771f, 0.328f, 0.970f, 0.406f, 0.615f, 0.116f, 0.676f, 0.977f, 0.133f, 0.971f, 0.572f, 0.833f, 0.140f, 0.616f, 0.489f, 0.997f, 0.513f, 0.064f, 0.945f, 0.719f, 0.592f, 0.543f, 0.021f, 0.978f, 0.279f, 0.317f, 0.505f, 0.167f, 0.620f, 0.077f, 0.347f, 0.857f, 0.137f }; return colors; } |
|
|
|
|
|
4
|
Java Game APIs & Engines / OpenGL Development / Re: Two squares/cubes (basic question OpenGL 3.x LWJGL)
|
on: 2013-01-27 13:32:43
|
You need to add the other squares indices to your indices array. The indices tells your vboi which of the vertices it should draw and what order to draw them in.
what do you mean other squares indices? If you mean something like this 1 2 3 4 5 6 7
| byte[] indices = { 0, 1, 2, 2, 3, 0, 0, 1, 2, 2, 3, 0, }; |
it doesn't work. I tested this before that's why I asked.
|
|
|
|
|
5
|
Java Game APIs & Engines / OpenGL Development / Two squares/cubes (basic question OpenGL 3.x LWJGL)
|
on: 2013-01-27 11:03:29
|
Hello. I hava basic qiestion how to display two squares? Based on this tutorial http://lwjgl.org/wiki/index.php?title=The_Quad_interleavedI know how to display square even cube but I don't know how to display two square (or cube). I was thinking that I need only to set vertices e.g. 1 2 3 4 5 6 7 8 9 10
| float[] vertices = { -0.2f, 0.2f, 0f, 1f, -0.2f, -0.2f, 0f, 1f, 0.2f, -0.2f, 0f, 1f, 0.2f, 0.2f, 0f, 1f, 0.3f, 0.2f, 0f, 1f, 0.3f, -0.2f, 0f, 1f, 0.6f, -0.2f, 0f, 1f, 0.6f, 0.2f, 0f, 1f}; |
and colors: 1 2 3 4 5 6 7 8 9 10 11
| float[] colors = { 1f, 0f, 0f, 1f, 0f, 1f, 0f, 1f, 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f, 1f, 0f, 1f, 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f }; |
but it doesn't work the second square does not appear. Do I need to do something else with indices array? How to tell OpenGL that after drawing first square move on this position and draw another square. Here is my whole code 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| package com.opengl3.tutorial.frame;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.ContextAttribs; 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; import org.lwjgl.opengl.PixelFormat; import org.lwjgl.util.glu.GLU;
public class TheQuadExampleColored { public static void main(String[] args) { new TheQuadExampleColored(); }
private final String WINDOW_TITLE = "The Quad: colored"; private final int WIDTH = 800; private final int HEIGHT = 600; private int vaoId = 0; private int vboId = 0; private int vbocId = 0; private int vboiId = 0; private int indicesCount = 0; private int vsId = 0; private int fsId = 0; private int pId = 0;
public TheQuadExampleColored() { this.setupOpenGL();
this.setupQuad(); this.setupShaders();
while (!Display.isCloseRequested()) { this.loopCycle();
Display.sync(60); Display.update(); }
this.destroyOpenGL(); }
public void setupOpenGL() { try { PixelFormat pixelFormat = new PixelFormat(); ContextAttribs contextAtrributes = new ContextAttribs(3, 2); contextAtrributes.withForwardCompatible(true); contextAtrributes.withProfileCore(true);
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); Display.setTitle(WINDOW_TITLE); Display.create(pixelFormat, contextAtrributes);
GL11.glViewport(0, 0, WIDTH, HEIGHT); } catch (LWJGLException e) { e.printStackTrace(); System.exit(-1); }
GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
GL11.glViewport(0, 0, WIDTH, HEIGHT); }
public void setupQuad() { float[] vertices = { -0.2f, 0.2f, 0f, 1f, -0.2f, -0.2f, 0f, 1f, 0.2f, -0.2f, 0f, 1f, 0.2f, 0.2f, 0f, 1f, 0.3f, 0.2f, 0f, 1f, 0.3f, -0.2f, 0f, 1f, 0.6f, -0.2f, 0f, 1f, 0.6f, 0.2f, 0f, 1f}; FloatBuffer verticesBuffer = BufferUtils .createFloatBuffer(vertices.length); verticesBuffer.put(vertices); verticesBuffer.flip();
float[] colors = { 1f, 0f, 0f, 1f, 0f, 1f, 0f, 1f, 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f, 1f, 0f, 1f, 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f }; FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length); colorsBuffer.put(colors); colorsBuffer.flip();
byte[] indices = { 0, 1, 2, 2, 3, 0, }; indicesCount = indices.length; ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount); indicesBuffer.put(indices); indicesBuffer.flip();
vaoId = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vaoId);
vboId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer,GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
vbocId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbocId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer,GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
vboiId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer,GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
private void setupShaders() { int errorCheckValue = GL11.glGetError();
vsId = this.loadShader("src/com/opengl3/tutorial/frame/vertex.glsl", GL20.GL_VERTEX_SHADER); fsId = this.loadShader("src/com/opengl3/tutorial/frame/fragment.glsl",GL20.GL_FRAGMENT_SHADER);
pId = GL20.glCreateProgram(); GL20.glAttachShader(pId, vsId); GL20.glAttachShader(pId, fsId); GL20.glLinkProgram(pId);
GL20.glBindAttribLocation(pId, 0, "in_Position"); GL20.glBindAttribLocation(pId, 1, "in_Color");
GL20.glValidateProgram(pId);
errorCheckValue = GL11.glGetError(); if (errorCheckValue != GL11.GL_NO_ERROR) { System.out.println("ERROR - Could not create the shaders:" + GLU.gluErrorString(errorCheckValue)); System.exit(-1); } }
public void loopCycle() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL20.glUseProgram(pId);
GL30.glBindVertexArray(vaoId); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount,GL11.GL_UNSIGNED_BYTE, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); GL30.glBindVertexArray(0); GL20.glUseProgram(0); }
public void destroyOpenGL() { GL20.glUseProgram(0); GL20.glDetachShader(pId, vsId); GL20.glDetachShader(pId, fsId);
GL20.glDeleteShader(vsId); GL20.glDeleteShader(fsId); GL20.glDeleteProgram(pId);
GL30.glBindVertexArray(vaoId);
GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(vboId);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(vbocId);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(vboiId);
GL30.glBindVertexArray(0); GL30.glDeleteVertexArrays(vaoId);
Display.destroy(); }
public int loadShader(String filename, int type) { StringBuilder shaderSource = new StringBuilder(); int shaderID = 0;
try { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { shaderSource.append(line).append("\n"); } reader.close(); } catch (IOException e) { System.err.println("Could not read file."); e.printStackTrace(); System.exit(-1); }
shaderID = GL20.glCreateShader(type); GL20.glShaderSource(shaderID, shaderSource); GL20.glCompileShader(shaderID);
return shaderID; } }
#vertex.glsl
in vec4 in_Position; in vec4 in_Color;
out vec4 pass_Color;
void main(void) { gl_Position = in_Position; pass_Color = in_Color; }
#fragment.glsl
in vec4 pass_Color;
out vec4 out_Color;
void main(void) { out_Color = pass_Color; } |
|
|
|
|
|