PDA

View Full Version : cubes not same size and text not showing :(



chuck norrrissss
03-24-2011, 09:52 AM
hello,
i'm new to using openGL and GLUT so please be gentle :)
i'm trying to make 3 wirecubes (all the same size) and place them at different places on the screen.

i can get the cubes up but they are all different sizes which is weird AND if i click on the window, all cubes get smaller and smaller with each click and i did not code that.

my other issue is that the text i'm trying to put on the screen (2D text) is not at all showing up.

i'm using vs2005.
here is my code, any ideas???



// ************************************************** *
// ** Creates 3 Boxes **
// ************************************************** *

// used later when the FALCON is connected
//#include <windows.h>
//#include "glut.h"
//#include <math.h>
//#include "haptics.h"

#include <stdlib.h>
#include <GL/openglut.h>
//#include <GL/glut.h>
#include <stdio.h>
#include <iostream>

using namespace std;
int w, h;

// if the window size is changed, the objects inside will be resized as well
void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
double ratio = w * 1.0 / h; // changed 'float' to 'double' due to warning

// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset Matrix
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45, ratio, 0.1, 100);

// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}


// exit out by pressing 'ESC' key
void glutKeyboard(unsigned char key, int x, int y) {

static bool inited = true;
if (key == 27) // esc key
{
exit(0);
}
}


void renderBitmapString (float x, float y, float z, void *font, char *string) {

char *c;
glRasterPos3f(x, y, z);
for (c = string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}


void restorePerspectiveProjection() {

glMatrixMode(GL_PROJECTION);
// restore previous projection matrix
glPopMatrix();

// get back to modelview mode
glMatrixMode(GL_MODELVIEW);
}


void setOrthographicProjection() {

// switch to projection mode
glMatrixMode(GL_PROJECTION);

// save previous matrix which contains the
// settings for the perspective projection
glPushMatrix();

// reset matrix
glLoadIdentity();

// set a 2D orthographic projection
gluOrtho2D(0, w, h, 0);

// switch back to modelview mode
glMatrixMode(GL_MODELVIEW);
}


// creates three boxes and text
void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// draw three boxes
glTranslated(2, 0, -5);
glutWireCube(0.5);

glTranslated(-2, 0, -5);
glutWireCube(0.5);

glTranslated(1, 2, -5);
glutWireCube(0.5);

// Code to display a string (fps) with bitmap fonts
setOrthographicProjection();
glPushMatrix();
glLoadIdentity();
renderBitmapString(1.0f, 2.0f , -5.0f, GLUT_BITMAP_HELVETICA_18, "HELLO");
glPopMatrix();
restorePerspectiveProjection();

glutSwapBuffers();
}


int main(int argc, char **argv) {

// init GLUT, create Window and init Keyboard
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(900, 400);
glutCreateWindow("THREE BOXES DEMO");
glutKeyboardFunc(glutKeyboard);

// register callbacks
glutDisplayFunc(renderScene);
renderBitmapString(0.1f, 0.1f, 0.1f, GLUT_BITMAP_TIMES_ROMAN_10, "hello");
glutReshapeFunc(changeSize);

// enter GLUT event processing cycle
glutMainLoop();

}

robmx
03-24-2011, 11:17 AM
I guess the change of size is because the transformations in renderScene() are being build up in each frame with the transformation done in the previous one. So try resetting the transformation at every frame



void renderScene() {
...
...
glLoadIndentity(); // Reset transformation matrix
glTranslate(...); // apply transformation
glutWireCube(...); // draw object

glLoadIndentity(); // Reset transformation matrix
glTranslate(...); // apply new transformation
glutWireCube(...); // draw new object
...
...
}

chuck norrrissss
03-28-2011, 03:28 AM
thanks!!
not to be an a$$ or anything but its Identity, not Indentity :) just in case someone else looks. took me 10 minutes to see that error :(


and any ideas why my text is not showing up?

chuck norrrissss
03-28-2011, 04:38 AM
nevermind, was calling the text in the wrong spot.
all is fixed and working as should, thanks for reading this :)