I want to draw a model of a chair that I exported from 3ds max as a txt file. I read the file and display it in OpenGL (using Visual Studio -> C++). My problem is that I have to multiplicate the chair many times (10). I did try to multiplicate it twice but it does not work. The program crashes with these "Program is not responding" Windows errors. I have a main class that calls all of the nessessery methods from CDrawModel like that:
CDrawModel mModel;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(100, 100);
glutCreateWindow("Georgi Koemdzhiev - 1306794");
glutKeyboardFunc(KeyResponse);
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
mModel.initGL();
glutMainLoop();
}
void DrawGLScene(void) {
mModel.myDrawGLScene();
}
GLvoid ReSizeGLScene(GLsizei fwidth, GLsizei fheight) {
mModel.MyReSizeGLScene(fwidth, fheight);
}
CDrawModal initialises an object of type CPolygonMesh which handles the reading from the file and drawing on the screen functionality. I know my code works since I am getting my modal drawn on the screen:
this is my CDrawModal class:
oid CDrawModel::initGL(void) {
glClearColor(1.0, 1.0, 1.0, 1.0);
near = 1.0;
far = 1000.0;
height = 1.5;
glCullFace(GL_BACK); // don’t draw back facing faces
glEnable(GL_CULL_FACE); // enable face culling
glPolygonMode(GL_FRONT, GL_LINE); // select front polygons and draw edges only
}
void CDrawModel::MyReSizeGLScene(int fwidth, int fheight) // Resize And Initialize The GL Window{
// Store window size so it can be accessed in myDrawGLScene()
wWidth = fwidth;
wHeight = fheight;
// Set fovy so that the viewing frustum has the specified height at the
// near clipping plane
fovy = (360 / PI) * atan(height / (2.0 * near));
// Calculate the aspect ratio of the VIEWPORT
// so that we can set the camera’s aspect ratio to the same value
aspect_ratio = (double)fwidth / (double)fheight;
glMatrixMode(GL_PROJECTION); // Select The Projection Stack
glLoadIdentity();
/* void glOrtho( GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);*/
gluPerspective(90, aspect_ratio, near, far); // perspective view
//glOrtho(-130.0,130.0,-130.0,130.0,near,far);
glViewport(0, 0, wWidth, wHeight); // Viewport fills the window
// Print values of parameters
cout << fixed; // Use fixed-point notation
cout.precision(3); // Number of digits after the decimal point
cout << "fovy = " << fovy << endl;
cout << "aspect_ratio = " << aspect_ratio << endl;
cout << "near = " << near << endl;
cout << "far = " << far << endl;
}
void CDrawModel::myDrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -200);
mesh.draw_model();
glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}
What I need to do in order to draw it twice.This is my drawModal method in CPolugonMesh:
void CPolygonMesh::draw_model(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0); // draw red things
glBegin(GL_TRIANGLES);
for (int i = 0; i < Mesh_NumFaces; i++) {
// Look up the coordinates of each vertex
// in vertex_list[]
glVertex3fv(vertex_list[ face_list[i][0] ]);
glVertex3fv(vertex_list[ face_list[i][1] ]);
glVertex3fv(vertex_list[ face_list[i][2] ]);
//cout << "Drawing face: " << i << endl;
}
glEnd();
}
Your CPolygonMesh::draw_model(void) method actually clears the screen each time you draw a model, so only the last call will leave anything on the screen.
The line
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
is totally wrong there. You already do clear the screen at the beginning of your frame.
Related
I am trying to plot 3D points by reading in a series of coordinates contained in a text file but I don't seem to get any output.
I personally think that either I am doing something wrong while reading my text file or in void reshape
Here is my code so far:
#include <glut.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
struct POINTS
{
int n;
float x;
float y;
float z;
};
POINTS point[1371];
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-200, 0.0, 0.0, 200, 0.0, 300);
}
void display()
{
ifstream f;
f.open("points.txt");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2);
glBegin(GL_POINTS);
for (int i = 0; i < 1371; ++i)
{
f >> point[i].n >> point[i].x >> point[i].y >> point[i].z;
cout << point[i].n << " " << point[i].x << " " << point[i].y << " " << point[i].z << endl;
glVertex3f(point[i].x, point[i].y, point[i].z);
}
glEnd();
glFlush();
f.close();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -5.0, -117.564, 36.7301, 151.769, 0, 1, 0);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA
| GLUT_SINGLE | GLUT_MULTISAMPLE); // initialize GLUT
glutInitWindowSize(640, 480); // set the window size
glutInitWindowPosition(100, 100); // set display-window width and height to 100
glutCreateWindow("Plotting a series of 3D Points");
glutReshapeFunc(reshape);
glutDisplayFunc(display); // send graphics to display window
initGL();
glutMainLoop();
return 0;
}
And here is the text file:
0 -117.6027 161.5286 70.5128
1 -82.9727 87.7585 107.0592
2 -117.6027 72.2113 106.0432
3 -92.2141 80.0949 116.0134
4 -86.2138 96.987 122.6796
5 -102.6702 75.0957 108.7022
6 -58.8401 129.0492 72.169
7 -75.3688 91.5178 93.905
8 -97.0844 22.4057 115.8543
9 -101.1874 18.6077 127.3053
10 -111.0116 13.8925 122.3735
Your points are clipped by the near an far plane of the Viewing frustum of the perspective projection. Alle the geometry which is not in between the near and far plane is clipped:
Increase the distance between the near and far plane (gluPerspective) and change the look at (gluLookAt) the scene:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -1.0, -117.564, 36.7301, 0.0, 0, 1, 0);
I made a simple OpenGL program that draws a 2D texture to the screen. When you resize the window, it doesn't adjust properly, so to fix that, I would just run the projection matrix code again:
if (windowSizeChange)
{
std::cout << "Window resized." << std::endl;
std::cout << windowWidth << " " << windowHeight << std::endl;
windowSizeChange = false;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
However, running this code warps the image. To my understanding, to make it so I can draw 2D-like on the screen, my texture is drawn using an orthographic projection matrix which means there is a plane that is "parallel" with the window port or something like that which I draw on. When I try to re-make it to accommodate for the new window size, it doesn't adjust properly. What's going wrong with this code?
In your code you're changing the ortho matrix, but you also need to change the glViewport:
if (windowSizeChange)
{
glViewport(0, 0, windowWidth, windowHeight); // <-- Add this
std::cout << "Window resized." << std::endl;
std::cout << windowWidth << " " << windowHeight << std::endl;
windowSizeChange = false;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
Orthographic matrices are, like you said, just a matrix that in this case is parallel to the screen. When we call glOrtho, it changes the size of the matrix we're working with, and glViewport tells openGL the size of the viewport (in this case, our window) we're working with. You'll generally want glOrtho and glViewport to be the same dimensions
When the size of the window and the framebuffer has been changed, then you have to adjust the viewport rectangle.
The viewport rectangle can be set by glViewport and specifies how the normalized device coordinates are mapped to window coordinates. It defines the area of the framebuffer, where the normalized device coordinates from (-1, -1) to (1, 1) are mapped to.
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 1.0);
I'm trying to get world coordinates from any of my rendered vertices in OpenGL window (I prefer to use GLUT library). The problem is when I'm calling glReadPixels function to get depth value of a vertex, it always returns a 1 value, when I'm clicking my mouse anywhere.
I am stuck on this point, have already read a ton of articles, but didn't get any answer.
Here is my code:
display function
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH | GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthRange(200, 2000);
//here i put some glBegins and glEnds
glutSwapBuffers();}
main function
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1280, 720);
glutInitWindowPosition(50, 86);
glutCreateWindow("2D correlation function");
glClearColor(1,1,1,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFrustum(-150, 150, -150, 150, 200, 2000);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
gluLookAt(200,400,200,0,0.0,0.0,0.0,0.0,1.0);
glScalef(0.4, 0.4, 0.4);
glutDisplayFunc(display);
timer();
glutMainLoop();
return 0;}
mouse clicking function
void mouse(int button, int state, int x, int y){
GLdouble objX, objY, objZ;
GLdouble matModelView[16], matProjection[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, matModelView);
glGetDoublev(GL_PROJECTION_MATRIX, matProjection);
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat winX = x;
GLfloat winY = viewport[3] - y;
GLfloat winZ = 0;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
cout << winX << " " << winY << " " << winZ << " " <<endl;
gluUnProject(winX, winY, winZ, matModelView, matProjection, viewport, &objX, &objY, &objZ);
cout << objX << " " << objY << " " << objZ << " " <<endl;}
Because of this, my world coordinates are displayed incorrectly:
example of mouse clicking
another example
I think i'm doing something wrong in display procedure
The parameter to glEnable has to be a single enumerator constant. The parameter is not a bit field.
The following is not valid and will generate a GL_INVALID_ENUM error, which can be detected by glGetError or Debug Output:
glEnable(GL_DEPTH | GL_DEPTH_TEST);
It has to be
glEnable(GL_DEPTH_TEST);
Anyway GL_DEPTH is not a valid parameter for glEnable, but it is possibly an parameter to glCopyPixels.
Note, the value of the enumerator constant GL_DEPTH_TEST is 0x0B71 and the value of GL_DEPTH is 0x1801. A binary or (|) operation of the both constants won't make any sense.
Because of this the depth test has never been enabled and nothing was written to the depth buffer.
Further note, that the values which are accepted for the depth range glDepthRange have to be in the range [0, 1]. The values which are passed to the glDepthRange are both clamped to this range before they are accepted.
This means, that
glDepthRange(200, 2000);
is equal to
glDepthRange(1, 1);
so the depth range is [1, 1] and all values returned by glReadPixels are 1, too.
Skip glDepthRange(200, 2000); to solve the issue.
glGetDoublev(GL_MODELVIEW_MATRIX, matModelView) gets the current model view matrix from the GL_MODELVIEW matrix stack.
glGetDoublev(GL_PROJECTION_MATRIX, matProjection) gets the current projection matrix from the GL_PROJECTION matrix stack.
So you should put the projection matrix on the GL_PROJECTION matrix stack and the view matrix on the GL_MODELVIEW matrix stack (see glMatrixMode):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-150, 150, -150, 150, 200, 2000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(200,400,200,0,0.0,0.0,0.0,0.0,1.0);
Have you tried passing GLUT_DEPTH to glutInitDisplayMode?
According to GLUT documentation that flag is needed to create a depth buffer.
hello I am using glut and opengl with c++ , I have home I want to draw blue QUADS in it my problem when I draw the QUADS all the sense color in blue , so how I can to color only QUADS in blue color and Prevents to color all sense in blue color what I do wrong how to remove the blue color from all the sense and color only my QUAD?
my try:
void drawSquare1()
{
glBegin(GL_QUADS);
glColor3d(1,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3d(1,1,0);
glVertex3f(0.5,-0.5,-0.5);
glColor3d(1,1,1);
glVertex3f(0.5,0.5,-0.5);
glColor3d(0,1,1);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
}
void render(void) // Our Rendering Is Done Here
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
GLfloat xtrans = -g_xpos;
GLfloat ztrans = -g_zpos;
GLfloat ytrans = -g_ypos;
if(g_yrot > 360)
g_yrot -= 360;
else if(g_yrot < 0)
g_yrot += 360;
GLfloat sceneroty = (360.0f - g_yrot);
int numpolygons;
glRotatef(g_lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);
numpolygons = g_sector1.numpolygons;
for (int loop_m = 0; loop_m < numpolygons; loop_m++)
texture_object(loop_m);
gluQuadricDrawStyle(my_shape[0],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[1].texID);
glScalef(0.1,0.1,0.1);
glTranslatef(0.78,14.3,-4.2);
gluSphere(my_shape[0], 1.0,50,50);
gluQuadricDrawStyle(my_shape[1],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[8].texID);
glTranslatef(-20,0,0);
gluSphere(my_shape[1], 1.0,50,50);
gluQuadricDrawStyle(my_shape[2],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[22].texID);
glTranslatef(40,0,0);
gluSphere(my_shape[2], 1.0,50,50);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(-10,window_width,0,window_height,-10,10); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
drawSquare1();
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
//glPushMatrix();
drawSquare1();
//glPopMatrix();
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glutSwapBuffers ( );
}
int main(int argc, char** argv) // Main Function For Bringing It All Together.
{
//cout << "Hello World!" << endl;
//cin.get();
glutInit(&argc, argv); // GLUT Initializtion
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE); // (CHANGED)
if (g_gamemode)
{
glutGameModeString("640x480:16"); // Select The 640x480 In 16bpp Mode
if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
glutEnterGameMode(); // Enter Full Screen
else g_gamemode = false; // Cannot Enter Game Mode, Switch To Windowed
}
screen_width = glutGet(GLUT_SCREEN_WIDTH);
screen_height = glutGet(GLUT_SCREEN_HEIGHT);
window_width = screen_width/1.4;
window_height = screen_height/1.4;
if (!g_gamemode)
{
glutInitWindowSize(window_width,window_height); // Window Size If We Start In Windowed Mode
glutInitWindowPosition((screen_width-window_width)/2,(screen_height-window_height)/2);
glutCreateWindow("Frank's 3-D House"); // Window Title
}
init();
glutIgnoreKeyRepeat(true); // Disable Auto Repeat (NEW)
// glutKeyboardFunc(myKey); // register the key handler.
glutDisplayFunc(render); // Register The Display Function
glutReshapeFunc(reshape); // Register The Reshape Handler
glutKeyboardFunc(keyboard); // Register The Keyboard Handler
//glRasterPos2f(lineMargin, currentHight); // set the cursor to the initial position.
glutSpecialFunc(special_keys); // Register Special Keys Handler
glutSpecialUpFunc(special_keys_up); // Called When A Special Key Released (NEW)
glutIdleFunc(game_function); // Process User Input And Does Rendering (CHANGED)
glutMouseFunc(mouse) ;
glutMainLoop(); // Go To GLUT Main Loop
return 0;
}
and this picture of run my code:
By default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.
This means if texturing is enabled (glEnable(GL_TEXTURE_2D)), then the color from the texture is multiplied by the color which is currently set by glColor.
To fix your issue, I recommend to set glColor4f(1.0f,1.0f,1.0f,1.0f); before the geometry is drawn:
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
.....
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
for (int loop_m = 0; loop_m < numpolygons; loop_m++)
texture_object(loop_m);
.....
}
Note, the current color is changed in the function drawSquare1 and keeps its state.
I am a beginner in OpenGl and I am struggling a bit with setting up the glOrtho camera to match the window size so that I can draw a line using the window's coordinates. For example, if I want to draw a line from coordinates 0,10 (x,y) to 600,10. I managed to draw the line (which will be a "Separator" from the viewport and a toolbar with buttons) in my current set up but it was by "try end error" approach and the coordinates that I needed to put don't make any sense to me. When I tried to draw a line using the above-mentioned coordinates, the line simply did not show up. What I need to change in the glOrtho set up in order to work with these (1000x600) screen size and draw my vertices and not these:
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
Note, my current window size is 1000x600 (width/height)
This is the line (on the top that crosses the whole screen):
This is my OGWindow class that handles all of the drawing:
void OGWindow::MyReSizeGLScene(int fwidth, int fheight)
{
// Store window size in class variables so it can be accessed in myDrawGLScene() if necessary
wWidth = fwidth;
wHeight = fheight;
// Calculate aspect ration of the OpenGL window
aspect_ratio = (float) fwidth / fheight;
// Set camera so it can see a square area of space running from 0 to 10
// in both X and Y directions, plus a bit of space around it.
Ymin = -1;
Ymax = 12;
Xmin = -1;
// Choose Xmax so that the aspect ration of the projection
// = the aspect ratio of the viewport
Xmax = (aspect_ratio * (Ymax -Ymin)) + Xmin;
glMatrixMode(GL_PROJECTION); // Select The Projection Stack
glLoadIdentity();
glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0);
glViewport(0, 0, wWidth, wHeight); // Viewport fills the window
}
void OGWindow::myDrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area
OGWindow::myDrawModel();
drawToolbar();
glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}
void OGWindow::myDrawModel(GLvoid)
{
switch ( squareColour ) {
case RED:
glColor3f(1.0, 0.0, 0.0);
break;
case BLUE:
glColor3f(0.0, 0.0, 1.0);
break;
}
glBegin( GL_QUADS );
glVertex3f( squareX, squareY, 0.0 ); // Coordinates of bottom-left corner of square
glVertex3f( squareX + squareWidth, squareY, 0.0 );
glVertex3f( squareX + squareWidth, squareY + squareHeight, 0.0 );
glVertex3f( squareX, squareY + squareHeight, 0.0 );
glEnd();
}
// Convert from screen coords returned by mouse
// to world coordinates.
// Return result in worldX, worldY
void OGWindow::screen2World(int screenX, int screenY, double & worldX, double & worldY)
{
// Dimensions of rectangle viewed by camera projection
double projWidth = Xmax -Xmin;
double projHeight = Ymax - Ymin;
// Screen coords with origin at bottom left
int screenLeft = screenX;
int screenUp = wHeight - screenY;
worldX = Xmin + screenLeft * projWidth / wWidth ;
worldY = Ymin + screenUp * projHeight / wHeight ;
}
//Method to draw the toolbar separator line
void OGWindow::drawToolbar(GLvoid) {
//draw toolbar line separator
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
glEnd();
//draw create button
glPushMatrix();
glTranslatef(2.0, 10.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
}
This is my main class where I am ivoking the methods from OGWindow:
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Demo");
glEnable(GL_DEPTH_TEST); // enable the depth buffer test
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutPassiveMotionFunc(mousePassiveMotion);
glutIdleFunc(Idle);
theWindow.initGL();
glutMainLoop();
}
Check out the documentation of glOrtho function. As you see, there are 6 parameters: left, right, bottom, top, near, far. You made mistake by setting window width to top instead of bottom parameter. Here's proper use of function:
glOrtho (0, 1000, 600, 0, -1.0, 1.0)
So, first your ortho settings. If you want your camera to match the screen dimensions, glOrtho has to use the same dimensions.
// This will anchor the camera to the center of the screen
// Camera will be centered on (0,0)
glOrtho( -screenWidth/2.f, screenWidth/2.f, -screenHeight/2.f, screenHeight/2.f, -1, 1 );
// This will anchor the camera to the lower left corner of the screen
// Camera will be centered on (screenWidth/2, screenHeight/2)
glOrtho( 0, screenWidth, 0, screenHeight, -1, 1 );
Try both and see the difference. Although if you are making some sort of editor, where your camera doesn't move, you may be looking for the second ortho setup.
Second, you only ever use (apparently) the GL_PROJECTION matrix mode. You must use this mode to set the camera projection and GL_MODELVIEW to apply transforms to the camera or the objects.
So when you call resize and don't change the matrix mode back to GL_MODELVIEW, you'll be applying translations to the projection matrix.
If you did forget to initialize the modelview matrix it may contain garbage values and yield unexpected results.