Jump to content
  • Advertisement
Sign in to follow this  
Mushu

OpenGL Whar is mah color?!

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

Well, I've been learning OpenGL. And its been... interesting to say the least. I really miss the rigid OOPness of D3D (and the FVF... [sad]) Anyway, my current 'project' involves fooling around with vertex arrays. So I code up a prototype, blah blah, and wtf, it renders something on the first go. So I'm like "okay, wtf, something must be wrong because its working" and sure enough, the color data doesn't seem to be getting passed to where it needs to go. I've doublechecked the data itself, all the states, everything, and it still renders in white. I can alter the color by using a glColorXX() call, but that's not what I'm trying to do. The thing that's eating me is the vertices are rendering properly. Which means its not a problem with the stride, or the datastructure. The data IS there, and all the flags are set properly (AFAIK). Which means... I have no idea what the heck it means. Hense this post :( Anyway, on with some code (a lot, because I have no idea where the problem is)
	struct cOGLVertex {
		GLfloat tu, tv;
		GLfloat r, g, b, a;
		GLfloat nx, ny, nz;
		GLfloat px, py, pz;

		cOGLVertex(GLfloat px = 0, GLfloat py = 0, GLfloat pz = 0, 				   GLfloat nx = 0, GLfloat ny = 1, GLfloat nz = 0, 				   GLfloat r = 0,  GLfloat g  = 0, GLfloat b  = 0, 				   GLfloat a = 0,  GLfloat tu = 0, GLfloat tv = 0) {
			this->px = px; this->py = py; this->pz = pz;
			this->nx = nx; this->ny = ny; this->nz = nz;
			this->r  = r;  this->b  = b;  this->g  = g;
			this->a  = a;  this->tu = tu; this->tv = tv;
		}
	};

	class cOGLGrid {
	protected:
		std::vector<cOGLVertex>   grid;
		std::vector<unsigned int> indices;
		
		int m_szRow, m_szCol;

		void attachVertex(  cOGLVertex* i) {
			glVertexPointer(3,GL_FLOAT,sizeof(cOGLVertex),&i->px); }

		void attachNormal(  cOGLVertex* i) {
			glNormalPointer(GL_FLOAT, sizeof(cOGLVertex), &i->nx); }

		void attachColor(   cOGLVertex* i) {
			glColorPointer(4, GL_FLOAT, sizeof(cOGLVertex), &i->r); }

		void attachTexCoord(cOGLVertex* i) {
			glTexCoordPointer(2, GL_FLOAT, sizeof(cOGLVertex), &i->tu); }

		void attachAll(cOGLVertex* i) {
			attachVertex(i);
			attachNormal(i);
			attachColor(i);
			attachTexCoord(i); }

	public:
		cOGLVertex* getVertex(int x, int z) {
			return &grid[x + z*m_szRow]; }

		virtual void buildGrid(int x, int z) {
			m_szRow = x; m_szCol = z;

			for (int iz = 0; iz < m_szCol; iz++) {
				for (int ix = 0; ix < m_szRow; ix++) {
					grid.push_back(cOGLVertex(ix,0,iz,0,1,0,1,0,0));
				}
			}
		}

		virtual void buildIndices() {
			for (int iz = 0; iz < m_szCol-1; iz++) {
				for (int ix = 0; ix < m_szRow-1; ++ix) { 
					indices.push_back(ix+   iz   *m_szRow);
					indices.push_back(ix+1+ iz   *m_szRow);
					indices.push_back(ix+1+(iz+1)*m_szRow);
					indices.push_back(ix+  (iz+1)*m_szRow);
					indices.push_back(ix+   iz   *m_szRow);
					indices.push_back(ix+1+(iz+1)*m_szRow);
				}
			}
		}

		virtual void initArrays() {
			glEnableClientState(GL_VERTEX_ARRAY | 								GL_NORMAL_ARRAY | 								GL_TEXTURE_COORD_ARRAY | 								GL_COLOR_ARRAY); 
		}

		virtual void render() {
			attachAll(&grid[0]);
			
			glDrawElements(GL_TRIANGLES,(m_szRow-1)*(m_szCol-1)*6,GL_UNSIGNED_INT,&indices[0]);
		}
	};

	cOGLGrid tehgrid;

	virtual void init() {
		tehgrid.buildGrid(4,4);
		tehgrid.buildIndices();
		tehgrid.initArrays();
	}

	virtual void render() {
                // using this will make the grid render black.
		//glColor3f(0.0f,0.0f,0.0f);
		tehgrid.render(); 
	}

	virtual void initViewport() {
		glViewport(0,0,640,480);
		
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(45.0f,640.0f/480.0f,0.1f,100.0f);
		gluLookAt(0,0.1f,0,1.0f,0,1.0f,0,1.0f,0);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glShadeModel(GL_SMOOTH);
		glClearColor(0.5f,0.0f,1.0f,0.0f);
		glClearDepth(1.0f);

		glEnable(GL_DEPTH_TEST);

		glDepthFunc(GL_LEQUAL);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	}
That's essentially everything. I was going to take some of it out, but there's nothing much besides what could possibly be relevant to the problem (setting states, generating data, render calls, what have you). Anyway, I still have to test the other things (texture coords, normals, etc) to see if everything is bonkers or just the colors. If its just the colors, I can hack my way around it (iirc materials != colors). Thanks for the help guys :D

Share this post


Link to post
Share on other sites
Advertisement
Hi,

your trouble is where you do


glEnableClientState(GL_VERTEX_ARRAY | GL_NORMAL_ARRAY | GL_TEXTURE_COORD_ARRAY | GL_COLOR_ARRAY);


for glEnableCLientState, and for that matter glEnable, you cannot do bitwise OR to enable many things at the same time, so yo need to do:

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

and the corosponding glDisables need to be done seperate like as well.

Best Regards



Share this post


Link to post
Share on other sites
You need to enable your vertex, color and texture arrays using separate calls to glEnableClientState(), instead of trying to OR all the GL_VERTEX_ARRAY, GL_COLOR_ARRAY constants together into one call like that. They are not OR-able in that fashion, and the result is wierdness.

EDIT: Meh. Beaten. [grin]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!