Jump to content
  • Advertisement
Sign in to follow this  

openGL n00b stuff

This topic is 4348 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

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                       *
***********************************************/
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")


#include <vector>
#include <iostream>
#include <glut.h>


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) {

glBegin(GL_QUADS);

	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);
	glLoadIdentity();

	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<vertex> p(8);
	vector<vertex>::iterator op;
	op = p.begin();

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

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

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

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

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


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


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


	op->x= 0.5; //point H
	op->y=-0.5;
	op->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);
	glLoadIdentity();

	glViewport(0, 0, w, h);

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

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!