PDA

View Full Version : glVertex3fv problem

leopard
04-18-2009, 03:36 PM
hey there.....
it's with glVertex3fv......
as you see there are point class and main... and in main i'm trying to draw a simple polygon......
now when i call glVretex3fv( anyPoint.getcoord() ) the polygon deforms ( stretched from the right too much and takes a triangular shape on the left side ).......
but replacing glVertex3fv with glVertex3f works fine...... like glVertex3f( anypoint.getX(), anypoint.getY(), anypoint.getZ() ).....

this problem happened only with me ( the same code works correctly on other computers ) so may be you won't be able to see what's really going on..........

here's the code .....

point.h

#pragma once

class Point
{
private:
float x, y, z;

public:
void initialize (float x, float y, float z);
Point(float x, float y, float z);
Point(void);
~Point(void);

void setX(float x) { this->x = x; }
void setY(float y) { this->y = y; }
void setZ(float z) { this->z = z; }

void setCoord(float x, float y,float z) { this->x = x; this->y = y; this->z = z; }
void setCoord(float coord[] ) { this->x = coord[0]; this->y = coord[1]; this->z = coord[2]; }

float getX() { return this->x; }
float getY() { return this->y; }
float getZ() { return this->z; }

float* getCoord() { float coord [] = {this->x, this->y, this->z}; return (coord);}
};

point.cpp

#include "Point.h"

void Point::initialize (float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}

Point::Point(float x, float y, float z)
{
this->initialize (x, y, z);
}

Point::Point(void)
{
this->initialize (0, 0, 0);
}

Point::~Point(void)
{
}

main.cpp

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

Point bl(-1,-1,-1);
Point br(1,-1,-1);
Point tr(1,1,-1);
Point tl(-1,1,-1);

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1);
glEnable (GL_DEPTH_TEST);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glTranslatef(0,0,-2);

glVertex3fv( bl.getCoord() );
glVertex3fv( br.getCoord() );
glVertex3fv( tr.getCoord() );
glVertex3fv( tl.getCoord() );
glEnd();

glFlush();
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);
gluPerspective (60, (float)w/(float)h, 1, 100);

glMatrixMode(GL_MODELVIEW);
gluLookAt(0,1,0,0,0,0,0,1,0);
}

void main(int argc, char** argv)
{
glutInit (&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (1200, 700);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);

init ();
glutDisplayFunc (display);
glutReshapeFunc (reshape);

glutMainLoop();
}

i'm using visual studio 2008 .......but i don't know how to discover the OpenGl version i'm working with......

todayman
04-18-2009, 04:52 PM
I think you're having a problem because getCoord() returns a local variable. As soon as the function exits, those values are effectively destroyed.
But, since the data in the Point class is the data you want, I think passing &bl, etc. and typecasting will work.

You can get the OpenGL version by calling glGetString(GL_VERSION).

leopard
04-19-2009, 07:05 AM
i'm sorry but i couldn't understand what you ment by passing &bl and typecasting ......
a little example would be sufficient....
so i used glGetString and the result was "2.1.7276 Release".....

Rosario Leonardi
04-19-2009, 08:29 AM
Returning a pointer to automatic variable is unsafe cause the memory is allocated in the stack and can be overwritten by other functions. :-\

Your get function should be like this:

inline void Point::getCoord(float coord[3]){
coord[0] = x;
coord[1] = y;
coord[2] = z;
}
and you have to call it in this way..

{
float myPoint[3];

bl.getCoord(myPoint); glVertex3fv(myPoint);
br.getCoord(myPoint); glVertex3fv(myPoint);
tr.getCoord(myPoint); glVertex3fv(myPoint);
tl.getCoord(myPoint); glVertex3fv(myPoint);
glEnd();

}

By the way using data hiding for simple class like point make the code less legible, I don't think it's a good design.
Also remove the zero initialization.. it's useless and it take precious time. :P

Edito:
Last tough.. I'm not sure buy you can even use a dirty way e redefine the float* cast operator and return the pointer to x, the other variable should follow in memory. ;)

martinsm
04-19-2009, 12:23 PM
I usually do the following trick:

struct Point
{
union
{
struct
{
float x, y, z;
};
float v[3];
};

...
};

Point p;
glVertex3fv(p.v);

Also I do not see much reason in your Point class to declare x,y,z members private. I mean if you provide both - trivial setters and getters for members, what reason is to put them private? You can not mess up internal structure of Point objects by changing their member variables from outside, because each member variable is independent from others.

leopard
04-19-2009, 02:23 PM
actually this trick is awesome .... i used it.....
and thanx alot Rosario Leonardi and all you guys for your support...