The following demo works well on its own program
https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/stb/EasyFont.java
but I just can't get it draw in my game.
I copied from line 53 to line 63 (make it a draw() method) to my game loop , and put other things( like those in front of the while loop in original code) in a init() method,
public void init(){
ByteBuffer charBuffer = BufferUtils.createByteBuffer(text.length() * 270);
int quads = stb_easy_font_print(0, 0, getText(), null, charBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 16, charBuffer);
glClearColor(43f / 255f, 43f / 255f, 43f / 255f, 0f); // BG color
glColor3f(169f / 255f, 183f / 255f, 198f / 255f); // Text color
}
public void draw(){
float scaleFactor = 1.0f + getScale() * 0.25f;
glPushMatrix();
// Zoom
glScalef(scaleFactor, scaleFactor, 1f);
// Scroll
glTranslatef(4.0f, 4.0f - getLineOffset() * getFontHeight(), 0f);
glDrawArrays(GL_QUADS, 0, quads * 4);
glPopMatrix();
}
but I just can't see any text in my game, is it because I am using a shader (I've tried unbind it but didn't work) or a camera (which relates to a "projection" Matrix4f uniform in shader) or a VBO?
or I need to to do some glEnable or glDisable stuff?
my game loop looks like this
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader.bind();
mainCamera.getPosition().lerp(new Vector3f(0.64f*main.getPos().x,0.86f*main.getPos().y,0).mul(-1,new Vector3f()), 0.07f);
onEntitiesDraw();
input.update();
EasyFont.draw(); //much the same as in the while loop of original code
glfwSwapBuffers(window);
glfwPollEvents();
Solved. The texts are rendered but in dark color, so I can barely see them.To make it be the color we assigned in that glColor3f(),
we just need to do
the glDisable(GL_TEXTURE_2D).
Related
I am programming a GUI framework in lwjgl (opengl for java). I've recently implemented rounded rectangles by rendering a couple of normal rectangles surrounded by circles. To render the circles I used GL11.GL_POINTS. I now reached the point, where I am trying to implement animations and for a window open animation, I decided to GL11.glScaled() it from small to normal. That works fine, but unfortunately my circles don't get resized.
I tried changing my GL_POINTS circle render method against a method that uses TRIANGLE_FANs and that worked fine. My problem there was, that the circles didn't look smooth and round at all and if I increase the rendered triangles it starts to lag very quick. Even though my computer isn't bad at all.
This is the code I've used to render circles with GL_POINTS.
GL11.glEnable(GL11.GL_POINT_SMOOTH);
GL11.glHint(GL11.GL_POINT_SMOOTH_HINT, GL11.GL_NICEST);
GL11.glPointSize(radius);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex2d(x, y);
GL11.glEnd();
GL11.glDisable(GL11.GL_POINT_SMOOTH);
This is the code I've used to scale the circles
GL11.glPushMatrix();
GL11.glTranslated(x, y, 0);
GL11.glScaled(2.0f, 2.0f, 1);
GL11.glTranslated(-x, -y, 0);
render circles
GL11.glPopMatrix();
I expect the circles to scale accordingly to the number I've put into glScaled()
Currently they aren't rescaling at all, just rendered at their normal size.
Here's a demonstration of how to properly render a circle using triangle fans:
public void render() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Coordinate system starts out as screen space coordinates
glOrtho(0, 400, 300, 0, 1, -1);
glColor3d(1, 0.5, 0.5);
renderCircle(120, 120, 100);
glColor3d(0.5, 1, 0.5);
renderCircle(300, 200, 50);
glColor3d(0.5, 0.5, 1);
renderCircle(200, 250, 30);
}
private void renderCircle(double centerX, double centerY, double radius) {
glPushMatrix();
glTranslated(centerX, centerY, 0);
glScaled(radius, radius, 1);
// Another translation here would be wrong
renderUnitCircle();
glPopMatrix();
}
private void renderUnitCircle() {
glBegin(GL_TRIANGLE_FAN);
int numVertices = 100;
double angle = 2 * Math.PI / numVertices;
for (int i = 0; i < numVertices; ++i) {
glVertex2d(Math.cos(i*angle), Math.sin(i*angle));
}
glEnd();
}
Output image:
The GL_POINT_SIZE value is actually the size of the point in pixels onscreen, not current coordinate units. For that reason your circles were unaffected by GL_SCALE. That's one reason not to use GL_POINTS to render circles. The other (arguably more important) reason being that GL_POINT_SIZE is severely deprecated and unsupported in newer OpenGL profiles.
I am making a platformer which I started developing with default java functions, but now am switching to OpenGL for everything. I have done everything like I always do with OpenGL, and what I did works fine in my other OpenGL projects. Now my problem is that LWJGL/OpenGL is scaling my textures in a very strange way.
It seems to be related to my screen's aspect ratio. (8:5)
I already had to flip the screen to make it the right way round, but as you can see the text is working fine, it's just the textured rect, and it isn't even straight on the bottom.
Here are the most important snippets from the two classes which actually use OpenGL:
Metamorph.java (main class)
public static void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
}
public void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0f, -1.0f, 1.0f);
glTranslatef(0f, -720f, 0f);
//glScalef(1280f/800, 720f/500, 1f);
renderer.render();
Display.update();
Display.sync(60);
}
Renderer.java (rendering stuff)
private void renderMainMenuWithGL()
{
//System.out.println("Main Menu!");
glColor4f(1, 1, 1, 1);
try
{
Texture bg = loadTexture("mockery");
bg.bind();
} catch (Exception e)
{
e.printStackTrace();
}
//drawQuad(0, 0, 1280, 720, 0, 0, 1280, 720);
glPushMatrix();
{
glBegin(GL_QUADS);
glTexCoord2f(0, 0);glVertex2f(0, 0);
glTexCoord2f(0, 1);glVertex2f(0, 720);
glTexCoord2f(1, 1);glVertex2f(1280, 720);
glTexCoord2f(1, 0);glVertex2f(1280, 0);
glEnd();
}
glPopMatrix();
TrueTypeFont f = loadFont(MAINFONT, Font.PLAIN, 50);
TrueTypeFont fb = loadFont(MAINFONT, Font.PLAIN, 48);
int sel = -1;
if(Mouse.getX() > 1000 && Mouse.getX() < 1240 && Mouse.getY() > 282.5F && Mouse.getY()< 737.5F)
sel = Math.round((Mouse.getY() - 337.5F)/75F);
if(sel == 0)
drawStringRight(fb, 1240, 350, "Story", new Color(0xff516b6b));
else
drawStringRight(f, 1240, 350, "Story", new Color(0xff516b6b));
}
private void drawStringRight(TrueTypeFont f, int x, int y, String s, Color c)
{
glPushMatrix();
f.drawString(x-f.getWidth(s), y, s, c);
glPopMatrix();
}
I am also open to advice on file structure/what I did wrong elsewhere, but keep in mind this is heavily WIP
The only possible problem I can see from what you have posted is the scaling and translating you are doing prior to rendering. You should not need to do this with your projection matrix setup the way it is. Other possibilities are that either the dimensions are not really 1280x720 as you think or you have modified one of the matrices further in the code you have not posted. I would try setting both the modelview and projection matrices to the identity matrix and then use glOrtho as you have above immediatly before drawing your quad, and use Display.getWidth and Display.getHeight instead of 1280/720 for the vertex coords.
This works fine for me:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2i(0, 0);
glTexCoord2f(1,0);
glVertex2i(Display.getWidth(), 0);
glTexCoord2f(1,1);
glVertex2i(Display.getWidth(), Display.getHeight());
glTexCoord2f(0,1);
glVertex2i(0, Display.getHeight());
glEnd();
If this still doesnt work, make sure that the viewport is also set to the entire display: glViewport(0,0,Display.getWidth(),Display.getHeight()).
Aswell, I notice that you are using glPushMatrix() and glPopMatrix before and after drawing, which does nothing and is not needed. Push and pop are used to save the current projection or modelview matrix and then reload it later, so that you can apply transformations inbetween and undo them when needed.
You're setting the projection matrix such that the coordinates of the corners of the window are (0,0) through (1680, 1050), and those are mapped into an area that covers 1280x800 pixels. Then you're drawing a 1280x720 image into it, so the screen coordinates of the image come out to span only 800x500 pixels. If you use 1280x720 in your glOrtho() call, I believe it will fix the issue. That is, you want the size of the window, not the size of the display in that call.
I am working on a camera system in a 3d scene on Android.
So far, I used this initialisation code to:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.i(LOG_TAG, "onSurfaceCreated()");
TextureLibrary.loadTextures(m_context, gl);
//Set initial perspective and viewport
gl.glMatrixMode(GL10.GL_PROJECTION);
float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
float aspectRatio = 1.9541f;
gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_BACK);
initialise();
}
But now, I would like to update the frustum in the render method, to perform zooms and/or image distortions. However, it seems like the the frustum update.
Here is the render code (trimmed of the some fat, which does not contain any GL State altering code, apart from the usual Push and PopMatrix):
private void render(GL10 gl)
{
//Frustrum update code
gl.glMatrixMode(GL10.GL_PROJECTION);
float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
float aspectRatio = 1.9541f;
gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
gl.glLoadIdentity();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//Render all the things here!
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
As you can see, the snippet where the frustum is set is almost identical, and the values used are the same. However, when glFrustum is called in the render method, the program will not show anything being drawn. I find this strange, since the values used are exactly the same (when forcing the aspect ratio).
The viewport is still set/updated in the OnSurfaceChanged method, which is hit at least once.
public void onSurfaceChanged(GL10 gl, int width, int height) {
_viewportWidth = width;
_viewportHeight = height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
I think this may have something to do with OpenGL States, but I haven't yet found a solution, out of everything I tried.
glFrustum() concatenates with the current matrix. As you haven't done a glLoadIdentity(), this means you're going to be multiplying with the previous frustum.
I want to create a camera moving above a tiled plane. The camera is supposed to move in the XY-plane only and to look straight down all the time. With an orthogonal projection I expect a pseudo-2D renderer.
My problem is, that I don't know how to translate the camera. After some research it seems to me, that there is nothing like a "camera" in OpenGL and I have to translate the whole world. Changing the eye-position and view center coordinates in the Matrix.setLookAtM-function just leads to distorted results.
Translating the whole MVP-Matrix does not work either.
I'm running out of ideas now; do I have to translate every single vertex every frame directly in the vertex buffer? That does not seem plausible to me.
I derived GLSurfaceView and implemented the following functions to setup and update the scene:
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// Setup the projection Matrix for an orthogonal view
Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//Setup the camera
float[] camPos = { 0.0f, 0.0f, -3.0f }; //no matter what else I put in here the camera seems to point
float[] lookAt = { 0.0f, 0.0f, 0.0f }; // to the coordinate center and distorts the square
// Set the camera position (View matrix)
Matrix.setLookAtM( vMatrix, 0, camPos[0], camPos[1], camPos[2], lookAt[0], lookAt[1], lookAt[2], 0f, 1f, 0f);
// Calculate the projection and view transformation
Matrix.multiplyMM( mMVPMatrix, 0, projMatrix, 0, vMatrix, 0);
//rotate the viewport
Matrix.setRotateM(mRotationMatrix, 0, getRotationAngle(), 0, 0, -1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
//I also tried to translate the viewport here
// (and several other places), but I could not find any solution
//draw the plane (actually a simple square right now)
mPlane.draw(mMVPMatrix);
}
Changing the eye-position and view center coordinates in the "LookAt"-function just leads to distorted results.
If you got this from the android tutorial, I think they have a bug in their code. (made a comment about it here)
Try the following fixes:
Use setLookatM to point to where you want the camera to be.
In the shader, change the gl_Position line
from: " gl_Position = vPosition * uMVPMatrix;"
to: " gl_Position = uMVPMatrix * vPosition;"
I'd think the //rotate the viewport section should be removed as well, as this is not rotating the camera properly. You can change the camera's orientation in the setlookat function.
I'm trying to implement a basic physics engine in Java and I'm using the JOGL bindings so I can visualize the results. I can create and rotate shapes easily enough, but have run into problems whilst manipulating the viewport and whilst moving the shapes.
I don't think a clipping issue - I've tried using the gluPerspective method with a massive range (0.0001f - 10000f) with no success. When I move the camera further away from my objects or move the objects themselves, they disappear.
Tutorials about JOGL are few and far between and many also use different versions of OpenGL, so I turn to the only friend I have left: the wonderful users of stack overflow. :)
Flattery aside, the code follows:
public class JoglEventListener implements GLEventListener, KeyListener, MouseListener, MouseMotionListener {
// keep pointer to associated canvas so we can refresh the screen (equivalent to glutPostRedisplay())
public GLCanvas canvas;
public Particle triforce;
public float x;
// constructor
public JoglEventListener(GLCanvas canvas) {
this.canvas = canvas;
}
#Override
public void display(GLAutoDrawable drawable) {
update();
render(drawable);
}
#Override
public void init(GLAutoDrawable drawable) {
triforce = new Particle();
x = 0;
}
private void update() {
triforce.integrate(0.0001);
x = x + 0.25f;
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
//gl.glFrustum (.5f, -.5f, -.5f * 1080, .5f * 960, 1.f, 500.f);
glu.gluPerspective(0, 1, 0.1f, 100f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glHint(GL2.GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL2.GL_FASTEST);
glu.gluLookAt(0, 0, 1.5, 0, 0, -10, 0, 1, 0);
//gl.glRotatef(90, 0f , 1f , 0f );
//Draw some scale lines
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.75f, 0.75f, 0.75f);
for (int i = 0; i < 20; i += 1)
{
gl.glVertex3f(-5.0f, 0.0f, i + 0.5f);
gl.glVertex3f(5.0f, 0.0f, i + 0.5f);
}
gl.glEnd();
//gl.glRotatef(x, 1f , 1f , 1f );
gl.glPushMatrix();
gl.glTranslated(triforce.position.x, triforce.position.y, triforce.position.z);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glColor3f(1f, 0f, 0f);
gl.glVertex3d(0, 0, -2);
gl.glColor3f(0f, 1f, 0f);
gl.glVertex3d(0, 0.25d, -2);
gl.glColor3f(0f, 0f, 1f);
gl.glVertex3d(0.25d, 0, -2);
gl.glColor3f(1f, 1f, 0f);
gl.glVertex3d(0.25d, 0.25d, -2.25d);
gl.glEnd();
gl.glPopMatrix();
gl.glFlush();
}
// (empty overridden methods omitted)
public Particle () {
setMass(200d);
velocity = new Vector3(0d, 30d, 40d);
acceleration = new Vector3(0d, -20d, 0d);
position = new Vector3(0d, 0d, 0d);
damping = 0.99d;
}
public void integrate (double duration) {
if (inverseMass <= 0.0d) {
return;
}
assert (duration > 0.0);
position.addScaledVector(velocity, duration);
Vector3 resultingAcc = new Vector3(acceleration.x, acceleration.y, acceleration.z);
velocity.addScaledVector(resultingAcc, duration);
velocity.multEquals(Math.pow(damping, duration));
//clearAccumulator();
}
public void setMass(double mass)
{
assert(mass != 0);
inverseMass = (1.0d)/mass;
}
Before movement / starting position:
The shape drifts upward and is obscured from the right and top, becoming invisible:
Any help would be greatly appreciated! Thanks!
The massive view range can be a problem. The coordinates of the objects are only so precise, and with a huge view range, things that should be near each other are determined to be at the same point. This can cause an object that should be in front of another to disappear behind it. Try using a smaller view range.
I had the same problem. Objects disappearing, while some stay in the scene. After removing:
gl.glEnable(GL2.GL_CULL_FACE);
everything was working just fine ! Of course this is JOGL code, in C, the command would be without all those objects. Just to make this answer clear for everyone.
In the render function, change the value of last parameter of gluPerspective from 100f to 1000f. It will solve your problem.
gl.gluPerspective(0, 1, 0.1f, 100f);
to
gl.gluPerspective(0, 1, 0.1f, 500f);
And I think in your code you have done a mistake in the above line writing glu.gluperspective
I think it is gl.gluPerspective.
In the end, I was never able to track down the issue, and started again from scratch. I didn't run into any further clipping issues on my new build.
My best guess as to my initial failure is an improperly used glHint or glClear call, or perhaps some problem with the version of JOGL I was referencing.