Public Group

# openGL n00b stuff

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

## Recommended Posts

Hy, Im having n00bling a problem with a tutorial from www.zeuscmd.com (tut 13): The tutorial is showing how to create a cube from vertices. I made a function that unifys the points and uses 8 vertices for a cube. When i use the function works, but now I made a class, and doesnt work anymore I dunno why
/***********************************************
* Zeus CMD - OpenGL Tutorial 13 : Solid Shapes *
* By Grant James (ZEUS)                        *
* http://www.zeuscmd.com                       *
***********************************************/

#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;glut.h&gt;

using namespace std;

bool fullscreen = false;
bool mouseDown = false;

float xrot = 0.0f;
float yrot = 0.0f;

float xdiff = 0.0f;
float ydiff = 0.0f;

class cube {

typedef struct {
float x;
float y;
float z;
}vertex;

public:

void drawBox(vertex A, vertex B, vertex C, vertex D, vertex E, vertex F, vertex G, vertex H);        // should pass by value vertices
};

void cube::drawBox(vertex A, vertex B, vertex C, vertex D, vertex E, vertex F, vertex G, vertex H) {

glColor3f(1.0f, 0.0f, 0.0f);
// FRONT
glVertex3f(A.x, A.y,  A.z);
glVertex3f(B.x, B.y,  B.z);
glVertex3f(C.x,	C.y , C.z);
glVertex3f(D.x, D.y,  D.z);

// BACK
glVertex3f(E.x, E.y, E.z);
glVertex3f(F.x, F.y, F.z);
glVertex3f(G.x, G.y, G.z);
glVertex3f(H.x, H.y, H.z);

glColor3f(0.0f, 1.0f, 0.0f);

// LEFT
glVertex3f(A.x, A.y,  A.z);
glVertex3f(D.x, D.y,  D.z);
glVertex3f(F.x, F.y,  F.z);
glVertex3f(E.x, E.y,  E.z);

// RIGHT
glVertex3f( H.x, H.y,  H.z);
glVertex3f( G.x, G.y,  G.z);
glVertex3f( C.x, C.y,  C.z);
glVertex3f( B.x, B.y,  B.z);

glColor3f(0.0f, 0.0f, 1.0f);
// TOP

glVertex3f(D.x,  D.y,  D.z);
glVertex3f(C.x,  C.y,  C.z);
glVertex3f(G.x,  G.y,  G.z);
glVertex3f(F.x,  F.y,  F.z);
// BOTTOM

glVertex3f(A.x, A.y,  A.z);
glVertex3f(E.x, E.y,  E.z);
glVertex3f(H.x, H.y,  H.z);
glVertex3f(B.x, B.y,  B.z);
glEnd();

}

bool init()
{
glClearColor(0.93f, 0.93f, 0.93f, 0.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);

return true;
}

void display()
{

typedef struct {
float x;
float y;
float z;
}vertex;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

gluLookAt(
0.0f, 0.0f, 3.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);

glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);

vector&lt;vertex&gt; p(8);
vector&lt;vertex&gt;::iterator op;
op = p.begin();

op-&gt;x=-0.5; //point A
op-&gt;y=-0.5;
op-&gt;z= 0.5;
op++;

op-&gt;x= 0.5; //point B
op-&gt;y=-0.5;
op-&gt;z= 0.5;
op++;

op-&gt;x=0.5; //point C
op-&gt;y=0.5;
op-&gt;z=0.5;
op++;

op-&gt;x=-0.5; //point D
op-&gt;y= 0.5;
op-&gt;z= 0.5;
op++;

op-&gt;x=-0.5; //point E
op-&gt;y=-0.5;
op-&gt;z=-0.5;
op++;

op-&gt;x=-0.5; //point F
op-&gt;y= 0.5;
op-&gt;z=-0.5;
op++;

op-&gt;x= 0.5; //point G
op-&gt;y= 0.5;
op-&gt;z=-0.5;
op++;

op-&gt;x= 0.5; //point H
op-&gt;y=-0.5;
op-&gt;z=-0.5;

cube cube1;
cube1.drawBox(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);

glFlush();
glutSwapBuffers();
}

void resize(int w, int h)
{
glMatrixMode(GL_PROJECTION);

glViewport(0, 0, w, h);

gluPerspective(45.0f, 1.0f * w / h, 1.0f, 100.0f);

glMatrixMode(GL_MODELVIEW);
}

void idle()
{
if (!mouseDown)
{
xrot += 0.3f;
yrot += 0.4f;
}

glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27 :
exit(1); break;
}
}

void specialKeyboard(int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
fullscreen = !fullscreen;

if (fullscreen)
glutFullScreen();
else
{
glutReshapeWindow(500, 500);
glutPositionWindow(50, 50);
}
}
}

void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseDown = true;

xdiff = x - yrot;
ydiff = -y + xrot;
}
else
mouseDown = false;
}

void mouseMotion(int x, int y)
{
if (mouseDown)
{
yrot = x - xdiff;
xrot = y + ydiff;

glutPostRedisplay();
}
}

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

glutInit(&argc, argv);

glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutCreateWindow("13 - Solid Shapes");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutMouseFunc(mouse);
glutMotionFunc(mouseMotion);
glutReshapeFunc(resize);
glutIdleFunc(idle);

if (!init())
return 1;

glutMainLoop();

return 0;
}


error: ------------------------------------------------------------------------------- .\main.cpp(183) : error C2664: 'cube::drawBox' : cannot convert parameter 1 from 'display::vertex' to 'cube::vertex' -------------------------------------------------------------------------------

##### Share on other sites
seems like the forum tag replaced ">" with "&" in my code now, I wonder why

##### Share on other sites
PLEASE could you post only the relevant pieces of code? For example, it tells you line 183 - so post 180 - 185 or so.

And second, it's telling you that it cannot convert from a display::vertex to a cube::vertex. You are passing one type and expecting another.

And for the "replacement", in html a "<" is a 'l'ess 't'han. And the same for the greater than.

##### Share on other sites
Well you're defining the vertex data type in two different places: once in your cube class, and once in the display function, and then you are mixing and matching between scopes. From a design perspective, I would remove your vertex definition from both places, and put it (once) in the global namespace.

Also, you are using C++. You do not need the typedef keyword. This will work fine:

struct Vertex
{ // data
};

##### Share on other sites
yay it worked ..thankyou Doggan

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634056
• Total Posts
3015282
×