Fix my opengl rendering function
I'm using this as a portion of a rendering function for my lwjgl game. It doesn't render anything to the screen, but I can't figure out what's messed up. The program already draws an advanced world of textured cubes, this will be to select the one the player is looking at. It's probably some dumb setting I'm missing. I've tried running it at all different times of the rendering loop, as well as fiddling with a few settings. The spatial coordinates are definitely right.
At the moment, I'm just trying to get it to render a white square over the selected cubes.
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
System.out.println("coords: "+left+" "+bottom+" "+back);
GL11.glColor3f(255f, 255f, 255f);
//left
GL11.glBegin(GL11.G开发者_开发知识库L_QUADS);
GL11.glVertex3f(left, bottom, back);
GL11.glVertex3f(left, bottom, front);
GL11.glVertex3f(left, top, front);
GL11.glVertex3f(left, top, back);
//right
GL11.glVertex3f(right, bottom, front);
GL11.glVertex3f(right, bottom, back);
GL11.glVertex3f(right, top, back);
GL11.glVertex3f(right, top, front);
//top
GL11.glVertex3f(left, top, front);
GL11.glVertex3f(right, top, front);
GL11.glVertex3f(right, top, back);
GL11.glVertex3f(left, top, back);
//front
GL11.glVertex3f(left, bottom, front);
GL11.glVertex3f(right, bottom, front);
GL11.glVertex3f(right, top, front);
GL11.glVertex3f(left, top, front);
//back
GL11.glVertex3f(right, bottom, back);
GL11.glVertex3f(left, bottom, back);
GL11.glVertex3f(left, top, back);
GL11.glVertex3f(right, top, back);
//bottom
GL11.glVertex3f(right, bottom, front);
GL11.glVertex3f(left, bottom, front);
GL11.glVertex3f(left, bottom, back);
GL11.glVertex3f(right, bottom, back);
GL11.glEnd();
GL11.glPopMatrix();
AND all my initializers, some of which i disabled before running the above, and some of which I omitted because they were easy to prepare/reverse. I can't post the full code because it is too long, the graphics engine for this game is HEAVY in development already.
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE,GL11.GL_MODULATE);
GL11.glClearColor(0.47f,0.55f,1.0f, 0.0f);
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glEnable( GL13.GL_MULTISAMPLE );
GL11.glEnable( GL13.GL_SAMPLE_ALPHA_TO_COVERAGE );
GL11.glAlphaFunc(GL11.GL_GREATER, (float) 0.01);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glTexParameterf( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
You're resetting the modelview matrix to identity, thus any view transformations (aligning/positioning "the camera") are overridden. Technically you're drawing in a "default" view and should you use a perspective projection probably outside the viewing volume's bounds.
I solved this myself, I was calculating coordinates wrong, but it took a while to figure that out since it appeared mostly fine ingame.
I had to leave out a lot of the important code here for sake of copyright, but I doubt anyone would have spotted the bug I found.
You enable texturing, but don't give your vertices any texture coordinates. You also don't give them normals, so I hope you don't enable lighting.
Based on your statements about the initializers ("some of which i disabled before running the above, and some of which I omitted") you should really post the state in which the GL is when rendering this cube, or before posting check it yourself. It may be that you even find the error yourself when fiddling out all the state changes your engine makes.
In general you should rather degrade from explicit initialization code and always set all the states you need immediately before rendering, as this prevents such strange things where you don't know in which state the GL really is. Always remember, OpenGL is a state machine.
精彩评论