Trying to render a triangle and failing

Started by kululu123, September 18, 2013, 16:36:33

Previous topic - Next topic

kululu123

here are the 2 classes I made
http://pastebin.com/xtMv244A
http://pastebin.com/AsGbFWYf

I know making a triangle object as one of the display class attributes is not a good example of coding in OOP neither making display and triangle class in the same package but this is just for study reasons ..
here's the exception I get :
Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread.
	at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
	at org.lwjgl.opengl.GL15.glGenBuffers(GL15.java:114)
	at pkg3dgame.Triangle.initializeVertexBuffer(Triangle.java:62)
	at pkg3dgame.Triangle.<init>(Triangle.java:50)
	at pkg3dgame.GameDisplay.<init>(GameDisplay.java:17)
	at pkg3dgame.GameDisplay.main(GameDisplay.java:23)
Java Result: 1

Detrox

You might want to look at the error, because it says there is no context to draw.
I think you forgot to set up a glortho or glperspective, like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

(Use this before drawing ofcourse)

You only used glViewport(), which sets up the viewport, not the context you need to draw on.
I could be wrong,  but this is what I think you did wrong.

kululu123

Quote from: Detrox on September 18, 2013, 17:26:40
You might want to look at the error, because it says there is no context to draw.
I think you forgot to set up a glortho or glperspective, like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

(Use this before drawing ofcourse)

You only used glViewport(), which sets up the viewport, not the context you need to draw on.
I could be wrong,  but this is what I think you did wrong.

im learning openGL from a book now and the code I wrote so far is based on the book and he didnt explain anything about the matrices so no its not that because im now at a point in the book that he says try to change different values and see how it affects the outcome so thats mean the code need to work so far and he expect us to learn more from the code by changing values to change the output which is the triangle

quew8

An OpenGL context has nothing to do with transformation matrices. The context is like the "instance" of OpenGL that you are working with, in LWJGL you create a context by calling one of the Display.create() methods. In your Triangle class, you create shaders programs and VBOs, all of which require a valid OpenGL context which is fine, except that you initialize an instance of Triangle in the field declarations of GameDisplay. So when you create an instance of GameDisplay in main(), it tried to create these shaders and VBOs before you have created a context, throwing the error.

Move the initialization of t1 out of the field declaration and put it after you call Display.create().

@Detrox: If you notice, he's actually using shaders, so the matrices don't have any effect whatsoever.

kululu123

Quote from: quew8 on September 18, 2013, 18:07:34
An OpenGL context has nothing to do with transformation matrices. The context is like the "instance" of OpenGL that you are working with, in LWJGL you create a context by calling one of the Display.create() methods. In your Triangle class, you create shaders programs and VBOs, all of which require a valid OpenGL context which is fine, except that you initialize an instance of Triangle in the field declarations of GameDisplay. So when you create an instance of GameDisplay in main(), it tried to create these shaders and VBOs before you have created a context, throwing the error.

Move the initialization of t1 out of the field declaration and put it after you call Display.create().

@Detrox: If you notice, he's actually using shaders, so the matrices don't have any effect whatsoever.

but why does the VBOs and shaders required a display to be created before initializing them and creating the shaders program ? doesnt it matter only when you want to start and render (by glDrawArrays())using the gl so you actually need a screen to be the output of the fragments ?

also I first init t1 in the main and moved it a scope to the field declaration area because in the display method I want to call t1.display so it will keep drawing the vertices in the main while loop
unless you could gimme a better way to do it

Cornix

The shaders and buffers do not need a display; they need an openGL context. But the context is only created once you create a display, thats how lwjgl works.
Without the opengl context you can not call any opengl methods. Without the display you have no context.

kululu123

Quote from: Cornix on September 18, 2013, 18:35:44
The shaders and buffers do not need a display; they need an openGL context. But the context is only created once you create a display, thats how lwjgl works.
Without the opengl context you can not call any opengl methods. Without the display you have no context.
ok got it but can you help me solve my problem ?
I change my start method to this
public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(500, 500));
            Display.setTitle("Jurassic Island");
            Display.setResizable(true);
            Display.create();
        } catch(LWJGLException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        reshape(Display.getWidth(), Display.getHeight());
        
        Triangle t1 = new Triangle();
        
        while(!Display.isCloseRequested())
        {
            Display.update();
            Display.sync(60);
            t1.display();
            
            if(Display.wasResized())
            {
                reshape(Display.getWidth(), Display.getHeight());
            }
        }
        Display.destroy();
    }


but still no triangle is being rendered why is that ?

kululu123

so anyone can help me understand why its not rendering the triangle :( ?

Fool Running

Quote from: kululu123 on September 19, 2013, 11:30:36
so anyone can help me understand why its not rendering the triangle :( ?
... Patience, Dude, this isn't a really active forum ... :-\


The first problem that jumps out to me is that the color of your triangle is the same color as the background (and is completely transparent).
The second problem that I see is that you aren't doing any matrix multiplications (setting up the projection matrix). I think this means that the coordinates will be mapped one unit for one pixel. Your triangle is only 1.5 units big, so it might only appear as one pixel on the screen.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

kululu123

Quote from: Fool Running on September 19, 2013, 12:40:07
Quote from: kululu123 on September 19, 2013, 11:30:36
so anyone can help me understand why its not rendering the triangle :( ?
... Patience, Dude, this isn't a really active forum ... :-\


The first problem that jumps out to me is that the color of your triangle is the same color as the background (and is completely transparent).
The second problem that I see is that you aren't doing any matrix multiplications (setting up the projection matrix). I think this means that the coordinates will be mapped one unit for one pixel. Your triangle is only 1.5 units big, so it might only appear as one pixel on the screen.


its not about the matrices but lol didnt notice i use the same color thanks xD