OpenGL Making a Room in a given space and camera - c++

This is the setting I have to work with (I cannot change any of these values)
#include <stdlib.h>
#include <GL/glut.h>
const GLdouble FRUSTDIM = 100.0f;
void reshape(int w, int h) // Resize the GL Window. w=width, h=height
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 320., 640.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
I want to build a wall, but something is wrong and I dont quite understand. If I'm not mistaken, the current space is (-100 - 100)x(-100 - 100)x(320 - 640) and the camera is currently at 0,0,320
I want to make a room, but I can't even set up a wall :(....
I tried using QUADS and QUAD_STRIP, but it still wont show up when I run it D:
My code:
void display(void)
{
glBegin(GL_QUADS);
glColor3f(1,1,1);
glVertex3f(50,50,420);
glVertex3f(50,-50,420);
glVertex3f(-50,-50,420);
glVertex3f(-50,50,420);
glEnd();
glutSwapBuffers();
glFlush();
}
I just need to draw a wall to get myself going. If there is any code u think is required to solve my problem, comment and I will edit my question. (FYI other codes are working fine because the skeleton was give to me to start myself going).

It needs to include this:
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); // clearing window
glDisable(GL_LIGHTING);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
to make the window clear for you to see the object. Additionally:
glTranslatef(-100,-100,-630.0);
glBegin(GL_QUADS);
/* Back Wall */
glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(200,0,0);
glVertex3f(200,200,0);
glVertex3f(0,200,0);
glEnd();
this would make the wall (pretty much the same, but just added translate before building it).

Related

GLUT bitmap fonts have unexpected position

I am making a game in OpenGL using GLUT on UNIX. It is a 3D game where the player can move side to side (x-axis) jump (y-axis) and is constantly moving forward and has to avoid oncoming obstacles (for my implementation the player actual stands still while the world constantly moves at the player).
I am having trouble when trying to draw a HUD with bitmap text on it. I have tried creating an orthogonal view and then drawing the text but it always ends up at a random spot on the x-axis and constantly moves towards the player with the world on the z-axis. Once it gets past the player it disappears (which is what happens to all the world objects to cut processing). I want the text in one place and to stay there.
gameSpeed = Accumulator*6;
DrawRobot(); //player
ModelTrans.loadIdentity(); //ModelTrans has helper functions to manipulate
ModelTrans.pushMatrix(); //the current matrix stack
ModelTrans.translate(vec3(0, 0, -gameSpeed)); //move the whole world
...Then I do a bunch of drawing of the game objects...
And here I attempt to do some bitmap fonts. Disabling the depth test helps put the text in front of all the other objects but the other code to create the orthogonal view actually could be commented out and I would still have the same problem.
ModelTrans.popMatrix();
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
ModelTrans.pushMatrix();
glLoadIdentity();
gluOrtho2D(0, WindowWidth, 0, WindowHeight);
glScalef(1, -1, 1);
glTranslatef(0, -WindowHeight, 0);
glMatrixMode(GL_MODELVIEW);
std::string str = "sup";
renderBitmapString(0.5 + xText, 5.0, GLUT_BITMAP_HELVETICA_18, str);
//xText adjusts for the moving left and right of the player
glMatrixMode(GL_PROJECTION);
ModelTrans.popMatrix();
glMatrixMode(GL_MODELVIEW);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
printOpenGLError();
Here is some other code that may be of use:
void renderBitmapString(float x, float y, void *font, std::string s)
{
glRasterPos2f(x, y);
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
glutBitmapCharacter(font, c);
}
}
void Timer(int param)
{
Accumulator += StepSize * 0.001f;
glutTimerFunc(StepSize, Timer, 1);
}
void Reshape(int width, int height)
{
WindowWidth = width;
WindowHeight = height;
glViewport(0, 0, width, height);
}
I am making a game in OpenGL using GLUT on UNIX
First of all, you're not doing it on Unix, but most likely using X11. Also I'm pretty sure your OS is a variant of Linux, which is not Unix (a …BSD would be a true Unix).
Anyway, in this code snippet you're adjusting the projection, but not the modelview matrix
glMatrixMode(GL_PROJECTION);
ModelTrans.pushMatrix();
glLoadIdentity();
gluOrtho2D(0, WindowWidth, 0, WindowHeight);
glScalef(1, -1, 1);
glTranslatef(0, -WindowHeight, 0);
glMatrixMode(GL_MODELVIEW);
std::string str = "sup";
renderBitmapString(0.5 + xText, 5.0, GLUT_BITMAP_HELVETICA_18, str);
//xText adjusts for the moving left and right of the player
glMatrixMode(GL_PROJECTION);
ModelTrans.popMatrix();
glMatrixMode(GL_MODELVIEW);
I'm not entirely sure what ModelTrans is, but it has pushMatrix and popMatrix and if I assume, that those are just glPushMatrix and glPopMatrix then your code mises a
glPushMatrix();
glLoadIdentity()
…
glPopMatrix();
block acting on the Modelview matrix. Modelview and Projection matrices have their own stacks in OpenGL and must be pushed/poped individually.

Why is my HUD not rendering in OpenGL?

I'm new to OpenGL, and I've been going through NeHe's tutorials and various other web sources, and I'm testing some things to render text as a HUD of sorts over everything else.
After a very long night, I can't get this to work and I can't find any solutions here that work, so I thought I'd ask.
My code:
GLvoid glLoadHUD(GLvoid)
{
glPushAttrib(GL_LIGHTING_BIT |
GL_DEPTH_BUFFER_BIT |
GL_TEXTURE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
glRasterPos2f(0.1f, 0.6f);
glColor3f(1.0f,1.0f,1.0f);
glPrint("Test.");
glRasterPos2f(0.0f, 0.0f);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
Which is the code to render the text, and this is the code for drawing the scene:
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears buffers
glLoadIdentity();
// If I put glLoadHUD(); here, it renders but the models render over it,
// which is useless.
for (xloop = 0; xloop < 3;)
{
glLoadIdentity();
glTranslatef(-4.0f+(float(xloop)*4.0f),0.0f,-12.0f);
glCallList(dlstBox); // This is the call to create a box.
xloop++;
}
glLoadHUD(); // If I put it here though, it doesn't render at all.
return TRUE;
}
Thank you in advance for any help you could give, I know I'm pretty green and I'm sure it's staring me right in the face, but this is driving me mad and I'm not sure how to make it work.
With glLoadHud after the rest of the scene, your MODELVIEW matrix is still on the stack, and you do not clear it as part of glLoadHud. Thus all of the glTranslatef translations that you accumulate during the scene are still active when you're drawing the hud, which translates it right out of your viewable window.
Clear the MODELVIEW matrix as part of the start of glLoadHud and see if that makes a difference.
It might be printing inside your z-clipping so it will not show up on your screen. So, move out of the screen a little bit and see if it shows up.

OpenGL Camera position and skybox

Been integrating this camera tutorial http://www.swiftless.com/tutorials/opengl/camera2.html and having a bit of trouble centering the camera in the skybox.
Using this code below makes my camera inside the box:
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.0, 1.0, -1.0*(GLfloat)h/(GLfloat)w,
1.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-1.0*(GLfloat)w/(GLfloat)h,
1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
To draw the skybox, I followed this tutorial: http://sidvind.com/wiki/Skybox_tutorial
I've been trying to translate objects closer to the camera, but didn't work as I expected. Now I'm not sure what I need to do.
Appreciate any help.
First: Don'y apply the projection in the reshape handler. Otherwise simple things appear impossible (like doing a skybox). Second: For a skybox to work you must use the very same projection like for the rendering of the rest of the scene. What you should change is the translation of the modelview to 0, yet keeping the camera orientation.
You can do this by setting the last column of the modelview matrix to (0,0,0,1).
So this makes your rendering code like this:
void render_skybox()
{
push_modelview();
set_modelview_column(3, 0, 0, 1);
draw_skybox();
pop_modelview();
}
void render()
{
set_viewport();
set_projection();
apply_camera_transform();
render_skybox();
render_scene();
}

c++ openGL picking problems

I'm working on a little example, where I have loaded an object from a wavefront file - and am trying to get my picking right, I've gone over this and a few tutorials about 10 times... but must be missing something. Was wondering if anyone could provide an extra set of eyes.
I've used a saved list to draw the object, which appears fine on the screen... At the moment, when gl_select(x, y) runs, I get a hit no matter what, and if I enable the translate/rotate code (which is currently commented out) - I get no hits what-so-ever.
Relevant code blocks:
// gl_select, is called when the mouse is clicked, with its x and y coords
void gl_select(int x, int y)
{
GLuint buff[256];
GLint hits;
GLint view[4];
//Buffer to store selection data
glSelectBuffer(256, buff);
//Viewport information
glGetIntegerv(GL_VIEWPORT, view);
//Switch to select mode
glRenderMode(GL_SELECT);
//Clear the name stack!
glInitNames();
//Fill the stack with one element
glPushName(0);
//Restric viewing volume
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
//Restrict draw area
gluPickMatrix(x, y, 1.0, 1.0, view);
gluPerspective(60, 1, 0.0001, 1000.0);
//Draw the objects onto the screen
glMatrixMode(GL_MODELVIEW);
//Draw only the names in the stack
glutSwapBuffers();
DrawSavedObject();
//Back into projection mode to push the matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
cout << hits;
//Back to modelview mode
glMatrixMode(GL_MODELVIEW);
}
And the draw functions:
void DrawSavedObject()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
//translate and rotate
//glRotated(rotation,0.0,0.0,1.0);
//glTranslated(7.0, 7.0, 0.0);
//Draw the saved object
glLoadName(7);
glCallList(list_object);
glutSwapBuffers();
}
And where the list is saved:
void SaveDisplayList(){
glNewList(list_object, GL_COMPILE);
glVertexPointer(3, GL_DOUBLE, 3*sizeof(GLdouble), vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawElements(GL_TRIANGLES, verticesSize ,GL_UNSIGNED_INT, triangles);
glDisableClientState(GL_VERTEX_ARRAY);
glEndList();
}
Sorry again for the chunkiness of the code blocks.
A few things to consider here:
OpenGL selection mode is deprecated and never was HW accelerated, except on a few SGI boxes and 3DLabs GPUs.
DisplayLists don't mix with Vertex Arrays.
Why do you call glutSwapBuffers right before drawing your list of saved objects? Makes absolutely no sense at all.
I'm not sure if it's relevant but you're not supposed to store things like glVertexPointer in display lists. From the spec http://www.opengl.org/sdk/docs/man/xhtml/glNewList.xml:
Certain commands are not compiled into the display list but are
executed immediately, regardless of the display-list mode. These
commands are glAreTexturesResident, glColorPointer, glDeleteLists,
glDeleteTextures, glDisableClientState, glEdgeFlagPointer,
glEnableClientState, glFeedbackBuffer, glFinish, glFlush, glGenLists,
glGenTextures, glIndexPointer, glInterleavedArrays, glIsEnabled,
glIsList, glIsTexture, glNormalPointer, glPopClientAttrib,
glPixelStore, glPushClientAttrib, glReadPixels, glRenderMode,
glSelectBuffer, glTexCoordPointer, glVertexPointer, and all of the
glGet commands.
This could be what's causing your problem.

How do we rotate a figure in 2d in openGL with respect to a point (other than the origin)?

Using the glrotatef() function rotates the figure by a specified angle with respect to the origin. How do I rotate the same figure with respect to another point without making use of transformation matrices manually? Thanks!
void display(){
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,499,0,499);
glMatrixMode(GL_PROJECTION);
glColor3f(1,0,0);
drawhouse();
glFlush();
glColor3f(0,0,1);
glTranslatef(0.5 ,0.5,0.5);
glRotatef(45,1,1,1);
glTranslatef(-0.5 ,-0.5,-0.5);
drawhouse();
glFlush();
}
Here's a screenshot for what happens. http://postimage.org/image/q81bhupw/
Basically:
If you want to rotate around P, translate by -P (so that P moves to the origin), then perform your rotation, then translate by P (so that the origin moves back to P).
glTranslatef(P.x, P.y, P.z);
glRotatef(angle, A.x, A.y, A.z);
glTranslatef(-P.x, -P.y, -P.z);
(Note: This is in "reverse order" because the last transformation added is the first one applied, under OpenGL rules.)
So in your setup code, you need these calls:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 499, 0, 499);
glMatrixMode(GL_MODELVIEW);
And then your display() method should look something like this:
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.5f, 0.5f, 0.5f);
glRotatef(45.f, 1.f, 1.f, 1.f);
glTranslatef(-0.5f, -0.5f, -0.5f);
glColor3f(0.f, 0.f, 1.f);
drawhouse();
glFlush();
}
Your code has quite a few problems. First, you have to apply the transformation on the moelview matrix and not the projection matrix. Second, you should wrap you transformation into a glPushMatrix/glPopMatrix, otherwise, they aren't reversed when rendering the next frame. Third, you should apply gluOrtho2d on the projection matrix and not the modelview matrx.
In accordance to John's answer your code should look like:
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,499,0,499);
glMatrixMode(GL_MODELVIEW);
glColor3f(1,0,0);
drawhouse();
glFlush();
glColor3f(0,0,1);
glPushMatrix();
glTranslatef(0.5 ,0.5,0.5);
glRotatef(45,1,1,1);
glTranslatef(-0.5 ,-0.5,-0.5);
drawhouse();
glPopMatrix();
glFlush();
}
EDIT: Even (or because) you are afraid of matrices, I suggest you to read some introductory material on vector and matrix operations and transformations. And you should also read some introductory material on OpenGL, to really understand how it works, especially the state machine principle.