Stopping glUseProgram() - java

I'm using an opengl shader on a texture. Once the texture is done shading I want to stop the glUseProgram() function.
Presently the shader is going overtop of everything, including rectf() functions that I need to not be shaded.
I tried glUseProgram(0) but that didn't work.
related question: Java Opengl: Discarding Texture Background with Shaders
Here is the relevant code.
glPushMatrix();
dirPosd = i.torso.getPosition().mul(30);
glTranslatef(dirPosd.x, dirPosd.y, 0);
glRotated(Math.toDegrees(i.torso.getAngle()), 0, 0, 1);
glColor3f(1,1,1);
skel_torso.bind();
sizer = 40;
glUseProgram(shaderProgram);
glBegin(GL_QUADS);
glTexCoord2f(0f, 0f);
glVertex2f( i.torso.getPosition().x - sizer-5, i.torso.getPosition().y - sizer-5); //NW
glTexCoord2f(1, 0);
glVertex2f( i.torso.getPosition().x + sizer-5, i.torso.getPosition().y - sizer-5); //NE
glTexCoord2f(1, 1);
glVertex2f( i.torso.getPosition().x + sizer-5, i.torso.getPosition().y + sizer-5); //SE
glTexCoord2f(0, 1);
glVertex2f( i.torso.getPosition().x - sizer-5, i.torso.getPosition().y + sizer-5); //SW
glEnd();
glPopMatrix();
glUseProgram(0); //Note here
glPushMatrix();
Vec2 shoulderPosL = i.shouldL.getPosition().mul(30);
glTranslatef(shoulderPosL.x, shoulderPosL.y, 0);
glRotated(Math.toDegrees(i.shouldL.getAngle()), 0, 0, 1);
glColor3f(1,1,0);
glRectf(-i.shoulderSize[0] * 30, -i.shoulderSize[1] * 30, i.shoulderSize[0] * 30, i.shoulderSize[1] * 30);
glPopMatrix();`
I've added a glUseProgram(0) statement, but when I added that the shader doesn't work at all.

You have to switch to a different shader, doing what you want for the other stuff rendering. Shaders are not something "wrapped" over something you render. Shaders are what make rendering work. If you disable the shaders, and your OpenGL context is not a compatibility profile no default fallback (to fixed function pipeline behavior) is provided and nothing renders. OTOH you're using the fixed function pipeline, but for that to work you have to parametize it properly (enable texture targets, modulation modes, colors and so on). It's generally easier to simply switch shaders instead of juggle with the FF pipeline state.

Related

Display ContextAttribs and GL11 Projection Matrix Mode - Function is not supported

I am getting an error when I try to set the Matrix mode of my LWJGL program to GL_Projection.
glMatrixMode(GL_PROJECTION);
The error is :
Exception in thread "main" java.lang.IllegalStateException: Function is not supported
at org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)
at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:2075)
....
I have tracked down the error to when I make my Display. When I remove my ContexAttribs my code doesn't display the error and renders! ( when I comment out the code that needs the contexattribs )
This is my code:
display code:
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
ContextAttribs attribs = new ContextAttribs(3, 2).withProfileCore(true).withForwardCompatible(true);
Display.create(new PixelFormat().withDepthBits(24).withSamples(4), attribs);
Display.setTitle(TITLE);
Display.setInitialBackground(1, 1, 1);
GL11.glEnable(GL13.GL_MULTISAMPLE);
GL11.glViewport(0, 0, WIDTH, HEIGHT);
initialization method:
glMatrixMode(GL_PROJECTION);
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 1, 0, 0);
textureID = loadTexture("res/hud.png");
glEnable(GL_TEXTURE_2D);
rendering method:
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glTranslatef(0, 0, 0);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
}
glEnd();
glPopMatrix();
Does anyone know how I could get this code working with the contextAttribs?
Thanks in advance!
Edit 1: I have all the functions and variables in GL11 statically imported.
First of all, drawing with glBegin/glEnd sequences is deprecated since more than 10 years. See Vertex Specification for a state of the art way of rendering.
With the line
ContextAttribs attribs = new ContextAttribs(3, 2).withProfileCore(true).withForwardCompatible(true);
a OpenGL core profile Context with Forward compatibility bit set is generated.
In this context all the deprecated functions like glBegin/glEnd sequences, matrix stack (glMatrixMode), the standard light model etc. are removed. This causes the error.
See also Fixed Function Pipeline and OpenGL Context
Skip the setting of the forward compatibility bit (.withForwardCompatible(true)) to solve the issue.

Text field using OpenGL

So, I'm new to OpenGL and trying to create a 2D Multiplayer game, I know how to do all of the networking, although the graphics portion is honestly kicking my buttox.
I have tried looking at NiftyGUI aswell as TWL as they have been recommended a-lot, although I can't seem to get a grasp of them as there aren't very many tutorials and no videos to help explain what's going on, not to mention the OpenGL Documentary page is just horribly laid out.
I've attemped drawing a black rectangle, which I was going to go ahead and outline in white somehow and making that a temporary textbox, by drawing white font onto it, although I don't even know how to draw font. These are just a few of the things I'm strugling with that I can't find and I'm aware that I'm going to have to use some libraries, so I'll name the ones I have implemented currently.
LWJGL
Slick2D
I don't have any others currently, besides for TWL but I can't figure out how to use it for the life of me.
Here's the code that I made myself while trying to get a small black rectangle going
void drawTextBox(int fromLeft, int fromTop, int width, int height) {
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(fromLeft, fromTop); // Upper Left
glTexCoord2f(1, 0);
glVertex2i(fromLeft - width, fromTop); // Uppright
glTexCoord2f(1, 1);
glVertex2i(fromLeft - width, fromTop + height); // Bottom right
glTexCoord2f(0, 1);
glVertex2i(fromLeft, fromTop + height); // bottom left
glEnd();
}
This is working correctly, all but one part... It's drawing the last texture that I have loaded, even though I'm not binding it anywhere in the program, because I've made sure of it. Then it's scaling it to fit into the dimensions of the text-box.
Could someone help me resolve this error and direct me to where I can learn how to set the opacity of quads as-well as draw some text?
Try binding the texture you want right before calling the function. Without knowing how you've "made sure" that the other texture is not being bound, that's all I can say. Also, for changing opacity simply use one of GL11.glColor4_ functions; the last parameter is the alpha (opacity) of the color.
void drawTextBox(int fromLeft, int fromTop, int width, int height) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0f, 1.0f, 1.0f, <B>0.5f</B>);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(fromLeft, fromTop); // Upper Left
glTexCoord2f(1, 0);
glVertex2i(fromLeft - width, fromTop); // Uppright
glTexCoord2f(1, 1);
glVertex2i(fromLeft - width, fromTop + height); // Bottom right
glTexCoord2f(0, 1);
glVertex2i(fromLeft, fromTop + height); // bottom left
glEnd();
glDisable(GL_BLEND);
}
This block of code allows you to draw a white rectangle with %50 opacity.
Take a look at the BOLDED part (0.5f). This float number defines the opacity of your rectangle. The bigger this number is (0.0f~1.0f), the more the opacity of the rectangle is.

OpenGL Image not loading correctly?

My Image is 1280x740 aswell as my game Canvas.
here is my code
// Initialize code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
// end initializiation
glClear(GL_COLOR_BUFFER_BIT);
getResources().backgroundImage.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0); // Upper left
glVertex2f(1280,0);
glTexCoord2f(1,0); // Upper Right
glVertex2f(0,0);
glTexCoord2f(1,1); // Bottom Right
glVertex2f(0,740);
glTexCoord2f(0,1); // Bottom Left
glVertex2f(1280,740);
glEnd();
Here is the problem I'm getting, while trying to make this a full-screen texture... the image is actually being scaled down?
Image showing
http://i48.tinypic.com/inrbeu.png
Vertex position is transformed to viewport coordinates by either fixed function pipeline or a vertex shader. Without specifying those, the outcome is probably not what you want.
You need to either setup a projection·modelview or a shader matching your needs.

Trouble when drawing a 2nd object without texture

Im having trouble geting a 2nd box to render.
So fare I have 1 object drawn with a texture and it seems to work fine but I have some code attached to it, so if I hold down the left mouse button ontop of it a 2nd box apears to its right.
Instead the 2nd box fails to apear and the textured box disaperes altogether.
I think im missing something with opengl, like something i have to enable or disable before or after drawing 1 thing.
heres my initilization code for opengl:
private static void glInit(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
}
heres the code that draws the objects:
public void render(){
if(myImage != null){
myImage.bind();
glBegin(GL_QUADS);
{
// upper left
glTexCoord2f(0, 0);
glVertex2f(x, y);
// upper right
glTexCoord2f(1, 0);
glVertex2f(x + size, y);
// bottom right
glTexCoord2f(1, 1);
glVertex2f(x + size, y + size);
// bottom left
glTexCoord2f(0, 1);
glVertex2f(x, y + size);
}
glEnd();
}
if(drawChoiceBox){
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
{
glVertex2f(x + size, y - size);
glVertex2f(x + size*2, y - size);
glVertex2f(x + size*2, y + size*2);
glVertex2f(x + size, y + size*2);
}
glEnd();
}
}
I have tested the logic for geting the 2nd box up and its works so i guess it must be me missing somthing with the way opengl draws things.
Any idea what im missing? that is ALL the opengl code in my project right now.
It doesn't look like you're disabling texturing for the 2nd object, so it's being drawn with texture.
Meanwhile your first box doesn't have glColor() specified, so after drawing the 2nd box, the first will be red. This could make it disappear, if modulating by red produces black, and modulation is enabled.
So you should probably explicitly enable/disable texturing when drawing your quads, as well as specifying all the vertex attributes which are changed, such as colour.

OpenGL ES buffers not clean

I am currently working on a 2D game rendered in OpenGL ES in Android. My sprites seem to render correctly but, sadly, they keep on screen when I do not want them renderer. When I draw a frame with nothing in buffers I can still see previously drawn sprites.
This is the code.
public void renderFrame()
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glViewport(0, 0, viewPort.getWidth(), viewPort.getHeight());
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(position.x - frustumWidth * zoom / 2,
position.x + frustumWidth * zoom/ 2,
position.y - frustumHeight * zoom / 2,
position.y + frustumHeight * zoom/ 2,
1, -1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
/*
* Buffer filling code goes here
*/
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
verticesIntBuffer.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, vertexSize, vertices);
gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT,
shortBufferIndices);
gl.glDisable(GL10.GL_BLEND);
}
I presumed glClear would clean buffers but this doesn't look like it in here.
This solution did not work for me either Clearing/releasing OpenGL ES buffers
Any ideas on how to explicity clean buffers? Another ideas of what could be happening due to rendering previous sprites still occurring?
Thanks a lot for your patience.
You should clear both color and depth buffer:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

Categories