Sign in to follow this  

Simple OpenGL Questions

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've started teaching myself a little OpenGL over the holidays. I've managed to get some stuff texture mapped by writing my own class to load 24bpp bmps.

My first question is why does everything have to be between -1.0 and 1.0 on the z-axis? When part or all of my object goes past that boundary it disappears. I understand that if it comes to close to the view it would disappear... but when it goes deeper shouldn't it look farther away?

Now I'm trying to make a cube with a texture on each side. It goes great as I'm adding each side... checking to see if it gets attached on the right spot... up until I get 5 or 6 sides on it... then it starts looking weird:

I made a temporary vid to show what I mean since it's a little difficult to explain:

http://www.youtube.com/watch?v=tNZUXwNq_90

Now for my code:



#include "bitmap.h"
#include <GL/glut.h>

using namespace std;

const int DEMON = 0;
const int SPIRIT = 1;
const int LOVE = 2;
const int INTENSE = 3;
const int KEY = 4;
const int PROTECTION = 5;

float rquadx = 0.015f;
float rquady = 0.01f;
float rquadz = 0.02f;

void glInit(void){
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void loadTextures(Bitmap* imgs){

glBindTexture(GL_TEXTURE_2D,PROTECTION);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imgs[PROTECTION].getWidth(),
imgs[PROTECTION].getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
imgs[PROTECTION].getPixelData());

}

void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glTranslatef(0.0f,0.0f,-5.0f);
glBindTexture(GL_TEXTURE_2D,PROTECTION);

glRotatef(rquadx,1.0f,0.0f,0.0f);
glRotatef(rquady,0.0f,1.0f,0.0f);
glRotatef(rquadz,0.0f,0.0f,1.0f);

glBegin (GL_QUADS);

//front
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, 0.25f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, 0.25f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, -0.25f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, -0.25f, 0.0f);

//back
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, -0.25f, -0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, -0.25f, -0.5f);

//right
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25f, 0.25f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f,-0.25f, -0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.25f,-0.25f, 0.0f);

//left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, 0.25f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.25f,-0.25f, -0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f,-0.25f, 0.0f);

//top
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, 0.25f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, 0.25f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, 0.25f, 0.0f);

//bottom
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, -0.25f, -0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, -0.25f, -0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, -0.25f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, -0.25f, 0.0f);

glEnd ();
glutSwapBuffers();

rquadx += 0.003f;
rquady += 0.002f;
rquadz += 0.004f;
}

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

Bitmap* images = new Bitmap[6];
images[DEMON].readBMP("Models\\JCube\\demon.bmp");
images[SPIRIT].readBMP("Models\\JCube\\spirit.bmp");
images[INTENSE].readBMP("Models\\JCube\\intense.bmp");
images[KEY].readBMP("Models\\JCube\\key.bmp");
images[PROTECTION].readBMP("Models\\JCube\\protection.bmp");
images[LOVE].readBMP("Models\\JCube\\love.bmp");

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Texture Mapping 1");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glInit();
loadTextures(images);
glutMainLoop();
}




I haven't really learned in-depth about the opengl functions yet... I really wanted to try to get this to work then go back and review everything.

Share this post


Link to post
Share on other sites
Quote:
Original post by yukina
My first question is why does everything have to be between -1.0 and 1.0 on the z-axis? When part or all of my object goes past that boundary it disappears. I understand that if it comes to close to the view it would disappear... but when it goes deeper shouldn't it look farther away?

That is the default view volume. Anything outside a unit cube, centered at the origin, will be removed. Unless you change the projection matrix, this is what you get. Look into, for example, gluPerspective to setup a perspective-based view volume.

Quote:
Original post by yukina
Now I'm trying to make a cube with a texture on each side. It goes great as I'm adding each side... checking to see if it gets attached on the right spot... up until I get 5 or 6 sides on it... then it starts looking weird:

I made a temporary vid to show what I mean since it's a little difficult to explain:

http://www.youtube.com/watch?v=tNZUXwNq_90

You haven't enabled the depth test; glEnable(GL_DEPTH_TEST).

Share this post


Link to post
Share on other sites
glEnable(GL_DEPTH_TEST); fixed my problem.

But I have more questions. I've just looked up gluPerspective and it seems to have been removed since OpenGL 3.2+? So is there something better I should be using?

I'm curious if there is a good reference as to what has been deprecated and/or removed?

I found the OpenGL Quick Reference Card but it makes it look like 50% of the functions including ones like glBegin have been deprecated. "Content shown in blue is removed from the OpenGL 3.2 core profile and present only in the OpenGL 3.2 compatibility profile." That sounds like everything that is in blue has been deprecated to me. Since deprecated means it's sticking around for compatibility issues only right?

Share this post


Link to post
Share on other sites
It means you won't be able to use that code if you create an OpenGL 3.2 or later rendering context. But you need to explicitly make one, so it's not like your current code will suddenly start to not work anymore.

But deprecated in that context pretty much means the functions aren't available anymore as you move into the more recent era of OpenGL.

Share this post


Link to post
Share on other sites
I'm confused. Is it a waste of time to learn deprecated stuff?? If so are there any resources online or books which start out with OpenGL 4.1? Or do I need to work myself up to OpenGL 4.1?

Share this post


Link to post
Share on other sites
Depends on how you learn it. It is a waste if you just memorize syntax and what you need to type to make OpenGL to do something. If you instead learn the concept behind everything, then the concepts stays the mostly the same but you just have to look up how to implement it with the new API. In that regard, the deprecated API isn't a waste to learn since it's mostly easier and faster to get something going so you can experiment and understand the concepts behind 3D graphics.

Personally, I think there's just so much that can go wrong with the advanced path for a beginner whose struggling to understand the basic ideas of 3D graphics in the first place, and on top of that has to get buffers, shaders and settings correctly linked together, or nothing will happen as soon as just something is off.

It's not so much features that are deprecated, more the functions to interface with those features that are deprecated, and features is what you should learn.

Share this post


Link to post
Share on other sites

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this