PDA

View Full Version : Science Fair Project - OpenGL For the Job?



31337
01-15-2003, 03:54 PM
My science fair project is producing a file format (see http://plato.hightechhigh.org/students/2005/jjensen/sciencefair/index.html for a very rough file format specification) for storing 2d images. I'm writing a demo program and I'm wondering. Is OpenGL the right tool for the job? I need it to have a graphical interface but now I'm starting to think that I should use windows.h but how do I draw images that are loaded into memory as RGBARGBA with that?

I was thinking just draw a textured quad on the screen and when the mouse is clicked switch the texture to the next one to create a slide show. When the mouse is moved translate the textured quad, etc.

But I'm kindof stumped on this problem. My draw code looks like this:



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); // Reset The View
// Position View Up Vector

gluLookAt(0, 1, 0, 0, 0, 0, 0, 1, 0); // This determines where the camera's position and view is
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable( GL_DEPTH_TEST );
glColor3f(1, 0, 0);
glBegin( GL_QUADS );
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(1, 0, 1);
glVertex3f(0, 0, 1);
glEnd();
SwapBuffers(win.returnHDC());
return TRUE;

and when I change the clear color it changes the background color but I don't see any quad on the screen. Does anybody see the problem? Thanks

nexusone
01-15-2003, 04:30 PM
Is creating a computer file format considard science?

Maybe if you mapped atoms or DNA with openGL would be cool... of some effect....


One of my science far projects was on 3D holigrams
http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by nexusone (edited 01-15-2003).]

Halcyon
01-15-2003, 05:00 PM
Well, try using glDrawPixels for the bitmap thing you were talking about. You can specify the format of the pixels so you could have it GL_RGB or GL_RGBA and so on.

Now as to the code you provided, the splitting it up into two procedures. You don't have to keep setting keep loading the identity matrix into the projection matrix stack every frame. Look at how NeHe does it in his tutorials. You put all the code to set up the projection in a seperate procedure that is only called when the window is resized and then you set the glEnable(...) calls and all that in the init statement (if you don't have to keep turning it on at different points in the code). You clear the depth buffer before you enable it in your code.

To get the quad try to translate it back a little. That might do it. Oh and you aren't setting up your projection anywhere. Put the appropriate gluPerspective of glFrustum call right after the glLoadidentity() following the glMatrixMode(GL_PROJECTION) statement.

- Halcyon

Edit: Put some code up. I may have some mistakes in this. I just typed it out in wordpad and copied it onto here.





void InitGL(){
glClearColor(0.0f,0.0f,0.0f,0.0f); // Set the background color
glClearDepth(1.0f); // Set the clear depth
glEnable(GL_DEPTH_TEST); //Enable the depth buffer
glDepthFunc(GL_LEQUAL); //Set the depth function

// Set the viewport
glViewport(0.0f,0.0f,windowWidth,windowHeight);

glMatrixMode(GL_PROJECTION); // Switch to projection stack
glLoadIdentity(); // Reset the matrix
gluPerspective(45.0f, // field of view angle
windowWindth/windowHeight, //aspect ratio
0.1f, // near plane
1000.0f ); // far plane

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void Render(){
// Clear the buffers
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

// Move the polygon away from the screen by 5 units
glTranslatef(0.0f,0.0f,-5.0f);

// Set the polygon color
glColor3f(1.0f,0.0f,0.0f);

glBegin(GL_QUADS);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();

SwapBuffers(win.returnHDC());
}



[This message has been edited by Halcyon (edited 01-15-2003).]

31337
01-15-2003, 07:00 PM
Whoa smashing good job! Plugged that code in and it worked like a charm. Now I kindof get the projection vs model view matrix stack a little more.

Projection is for establishing the viewing options like gluPerspective and modelview is for drawing stuff. Is that accurate? I consulted the red book but it kindof loss me.

How is this science? Good question I'm gonna have to come up with an answer

Halcyon
01-15-2003, 07:46 PM
How is this science? Simple....it's computer science http://www.opengl.org/discussion_boards/ubb/biggrin.gif



Projection Matrix Stack: This stack has a capacity of two matrices. Basically this is where you tell OpenGL how you want the scene to look. There are two types of projections:
Perspective: This is where where the whole scene is shaped like a frustum (pyramid with pointy part cut off - it's like a 3d trapazoid). This is the best model of how humans view the world around them. You can either use the glFrustum or the gluPerspective calls to set up this projection. I prefer the gluPerspective procedure. Orthographic: This projection shapes the scene like a cube (or a rectangular cube). This is used for things like grids or 2D GUI (buttons, text boxes, etc.)



Modelview matrix stack : This matrix stack has a depth of 32 and is basically meant for holding transforms. If you use glTranslate*(...), then the top matrix is changed through that call. If you want to transform (translate, rotate, shear, scale) in it's model space then you use glPushMatrix() and glPopMatrix(). They push the depth in by one and give you a fresh matrix to start from. Each object rendered has it's own coordinate system and that is called it's model space.


If you have any questions about what I explained, feel free to ask away http://www.opengl.org/discussion_boards/ubb/smile.gif. This stuff is kinda confusing at first. But once you understand it, it all falls into place. The biggest mistake I made was to learn the bare minimum i needed to understand what I was the code I was copying from the books meant. I got up to texturing and realized that I needed to understand what was happening, so I started over learning what was happening behind the scenes. I'm a 17 year old college student who just got done with calc II . You can imagine how little I understand sometimes when they start going into the math behind things http://www.opengl.org/discussion_boards/ubb/smile.gif. I'm starting to get the hang of transform matrices....but quaternions....no clue what so ever!

Hope I helped!

- Halcyon

Edit: Sorry, I had a formatting error with the lists.

[This message has been edited by Halcyon (edited 01-15-2003).]