Jump to content
  • Advertisement
Sign in to follow this  

glBegin(GL_LINES) drawing but not coloring

This topic is 2772 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'm re making a program a made using SDL in OpenGL (witch is why i have a whole bunch of already coded functions even though i have no working display) the only thing i need to do is modify the rendering functions to render the "cells" correctly. not sure why my lines are not being colored green.


#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <vector>

#define White 0,0,0 ///define colors that can be easily passed to drawing function
#define Black 255,255,255
#define Green 0,255,0

bool Setup=true;

static void resize(int width, int height) {
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(0, width, height, 0, -1, 2);
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}

struct Cell {
bool TBF;
bool alive;
};

void KillCell(std::vector<std::vector<Cell> >& Buffer, short x, short y) {
Buffer[x][y].alive = false;//it's dead
}
void BirthCell(std::vector<std::vector<Cell> >& Buffer, short x, short y) {
Buffer[x][y].alive = true;//it's alive
}

void boxColor(short x,short y,short w,short h,unsigned char r,unsigned char g,unsigned char b) {
glPushMatrix();
glColor3b(r,g,b);
glBegin(GL_QUADS);
glVertex2i(x,y);
glVertex2i(x,y+h);
glVertex2i(x+w,y+h);
glVertex2i(x+w,y);
glEnd();
glPopMatrix();
}

void lineColor(short x1,short y1,short x2,short y2,unsigned char r,unsigned char g,unsigned char b) {
glPushMatrix();
glColor3b(r,g,b);
glBegin(GL_LINES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
glPopMatrix();
}

void RenderCells(std::vector<std::vector<Cell> >& Buffer) {
short CW=800,CH=600;
short sizex = Buffer.size();
short sizey = Buffer[0].size();

for (short i=0;i<sizex;++i) {
for (short j=0;j<sizey;++j) {
if (Buffer[j].alive==true) {
boxColor(i*CW,j*CH,i*CW+CW,j*CH+CH,Black);
}
else boxColor(i*CW,j*CH,i*CW+CW,j*CH+CH,White);
}
}
///draw all the lins
for (short i=0;i<sizex;++i) /**call lineColro here*/;
for (short i=0;i<sizey;++i) /**call lineColro here*/;
}


void InitCellBuffer(std::vector<std::vector<Cell> >& Buffer, short nx, short ny, bool live) {
/************************
* initlize all cells *
* to some state *
************************/

for (short i=0;i<nx;++i) {
std::vector<Cell> TempV;
for (short j=0;j<ny;++j) {
Cell TempC;
TempC.alive = live;///choose witch state to initlize them to
TempC.TBF = false; ///we dont want them to be fliped just yet
TempV.push_back(TempC);
}
Buffer.push_back(TempV);
}
}

void CheckCells(std::vector<std::vector<Cell> >& Buffer) {
short sizex = Buffer.size();
short sizey = Buffer[0].size();
for (short i=0;i<sizex;++i) {
for (short j=0;j<sizey;++j) {

char alive=0;
char dead=0;

/**************************
* how many live or dead *
* adjacent cells *
**************************/

if(i>0&&j>0&&i<sizex-1&&j<sizey-1) {
if (Buffer[j+1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j-1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
}

if(i==0&&j==0) {
if (Buffer[j+1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
}
if(i==0&&j==sizey-1) {
if (Buffer[j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
}
if(i==sizex-1&&j==sizey-1) {
if (Buffer[i-1][j-1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
}
if(i==sizex-1&&j==0) {
if (Buffer[i-1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[j+1].alive) ++alive;
else ++dead;
}
if(i>0&&i<sizex-1&&j==0) {
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[i-1][j+1].alive) ++alive;
else ++dead;
if (Buffer[j+1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j+1].alive) ++alive;
else ++dead;
}
if(i>0&&i<sizex-1&&j==sizey-1) {
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
if (Buffer[i-1][j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j-1].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
}
if(i==sizex-1&&j>0&&j<sizey-1) {
if (Buffer[i-1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j-1].alive) ++alive;
else ++dead;
if (Buffer[j+1].alive) ++alive;
else ++dead;
if (Buffer[i-1][j].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
}
if(i==0&&j>0&&j<sizey-1) {
if (Buffer[j+1].alive) ++alive;
else ++dead;
if (Buffer[j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j+1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j-1].alive) ++alive;
else ++dead;
if (Buffer[i+1][j].alive) ++alive;
else ++dead;
}
/**************************
* ? to be fliped or not *
**************************/


if (Buffer[j].alive&&alive<2) {
Buffer[j].TBF = true;
}
if (Buffer[j].alive&&alive>3) {
Buffer[j].TBF = true;
}
if (!Buffer[j].alive&&alive==3) {
Buffer[j].TBF = true;
}

}
}
}

void ClearCells(std::vector<std::vector<Cell> >& Buffer) {
short sizex = Buffer.size();
short sizey = Buffer[0].size();
for (short i=0;i<sizex;++i) {
for (short j=0;j<sizey;++j) {
Buffer[j].alive = false;
Buffer[j].TBF = false;
}
}
}

void FlipCells(std::vector<std::vector<Cell> >& Buffer) {
short sizex = Buffer.size();
short sizey = Buffer[0].size();
for (short i=0;i<sizex;++i) {
for (short j=0;j<sizey;++j) {
if (Buffer[j].TBF) {
Buffer[j].alive = !Buffer[j].alive;
Buffer[j].TBF = false;
}
}
}
}

static void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
boxColor(100,500,50,50,Black);
lineColor(0,0,800,600,Green);
glutSwapBuffers();
}


static void key(unsigned char key, int x, int y) {
switch (key) {
case 27:
case 'q':
///qiut
break;
case 32:
Setup = !Setup;
break;
}

glutPostRedisplay();
}

static void idle(void) {
glutPostRedisplay();
}

int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitWindowPosition(800,500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutCreateWindow("Game Of Life");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);

glClearColor(1,1,1,1);

glutMainLoop();

return EXIT_SUCCESS;
}





in fact nothing is coloring, whats up with that?

Share this post


Link to post
Share on other sites
Advertisement
no, srry i should have said there coming out black. i did not think that anything else was effected at first but i can now see that everything is effected, including quads.

Share this post


Link to post
Share on other sites
ok i found a strange fix, normally i use glColor3f this time i used glColor3b. so i was able to fix it using glColor3f instead. what was i doing wrong with glColor3b ?

edit: ok i got it, for some reason glColor3b has to be in between glBegin and glEnd calls while glColor3f dose not. no clue why this is though.

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
A quick look on MSDN and it looks like glColor3b takes signed values. Therefore the color range is -127 to 127.

No, the color range is 0 to 127. For any integer type, integer value 0 maps to floating point value 0.0, and the maximum possible integer value maps to 1.0. Negative values are clamped to 0.0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by empirical2
A quick look on MSDN and it looks like glColor3b takes signed values. Therefore the color range is -127 to 127.

No, the color range is 0 to 127. For any integer type, integer value 0 maps to floating point value 0.0, and the maximum possible integer value maps to 1.0. Negative values are clamped to 0.0.


O rly?

I'm so glad I use glColor4f!

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Quote:
Original post by Brother Bob
Quote:
Original post by empirical2
A quick look on MSDN and it looks like glColor3b takes signed values. Therefore the color range is -127 to 127.

No, the color range is 0 to 127. For any integer type, integer value 0 maps to floating point value 0.0, and the maximum possible integer value maps to 1.0. Negative values are clamped to 0.0.


O rly?

I'm so glad I use glColor4f!

Personally, I'm glad I use modern technology, like VA with VBO.

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!