Sign in to follow this  
Xare

2 colors depending on depth...

Recommended Posts

Iam having some trouble with glEnable(GL_DEPTH_TEST); Iam trying to color one color for infront and another color for behind but depth test is refusing to work for me lol. glDisable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); //Enables Coloring glColor3f(0.0f,0.0f,1.0f); //Makes it Blue glDisable(GL_COLOR_MATERIAL); //Disables Coloring (glDrawElements) (mode, count, type, indices); //Redraws glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); //Enables Coloring glColor3f(1.0f,0.0f,0.0f); //Makes it Red glDisable(GL_COLOR_MATERIAL); //Disables Coloring Is there another way to do with without Depth_Test ? I tried using glDepthFunc But iam pretty new at all this...

Share this post


Link to post
Share on other sites
glEnable(GL_COLOR_MATERIAL); //Enables Coloring
glColor3f(0.0f,0.0f,1.0f); //Makes it Blue
glDisable(GL_COLOR_MATERIAL); //Disables Coloring

Will have no effect. If you want to change the material colours using glColor3f you must leave GL_COLOR_MATERIAL enabled while you do your drawing.

Enigma

Share this post


Link to post
Share on other sites
Iam pretty sure ive tried that...

You mean like this right ?

glDisable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enables Coloring
glColor3f(0.0f,0.0f,1.0f); //Makes it Blue
(glDrawElements) (mode, count, type, indices); //Redraws
glEnable(GL_DEPTH_TEST);
glColor3f(1.0f,0.0f,0.0f); //Makes it Red
glDisable(GL_COLOR_MATERIAL); //Disables Coloring


When I tried that it didnt work.


*edit* yea when i try that ^ it colors red but dosent color blue if its behind.

Ive tried this so many ways :(

Share this post


Link to post
Share on other sites
Having re-read your original post I'm having a hard time visualising what you're trying to do. "One colour for infront and another colour for behind" - infront and behind of what? Can you give a more detailed description?

Enigma

Share this post


Link to post
Share on other sites
Does this do what you are wanting?
#include <ctime>
#include <GL/glut.h>

const float vertices[3 * 4] =
{
0, 0, 1,
1, 1, -1,
-1, 1, -1,
0, -1, -1,
};

const unsigned char indices[6] =
{
0, 1, 2, 3, 0, 1
};

float alpha = 0;
float beta = 0;
float gamma = 0;
const float alphaIncrement = 0.05;
const float betaIncrement = 0.03;
const float gammaIncrement = 0.021;
int time;

void reshape(int width, int height){
if (height == 0)
{
height = 1;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(45.0f, GLfloat(width) / GLfloat(height), 0.1f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glBegin(GL_QUADS);
glVertex2f(-2, -2);
glVertex2f(2, -2);
glVertex2f(2, 2);
glVertex2f(-2, 2);
glEnd();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPushMatrix();
glRotatef(alpha, 1, 0, 0);
glRotatef(beta, 0, 1, 0);
glRotatef(gamma, 0, 0, 1);
float currentTime = std::clock();
alpha += (currentTime - time) * alphaIncrement;
beta += (currentTime - time) * betaIncrement;
gamma += (currentTime - time) * gammaIncrement;
time = currentTime;
glDisable(GL_DEPTH_TEST);
glColor3f(0, 0, 1);
glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_BYTE, indices);
glEnable(GL_DEPTH_TEST);
glColor3f(1, 0, 0);
glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_BYTE, indices);
glPopMatrix();
glColor4f(0, 1, 0, 0.3);
glEnable(GL_BLEND);
glBegin(GL_QUADS);
glVertex2f(-2, -2);
glVertex2f(2, -2);
glVertex2f(2, 2);
glVertex2f(-2, 2);
glEnd();
glDisable(GL_BLEND);
glutSwapBuffers();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(512,512);
glutCreateWindow("Depth Colour Test");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
time = std::clock();
glutMainLoop();
return 0;
}

It's just a simple example using a rotating triangular pyramid with a quad for the plane to determine infront/behind. This example doesn't use material properties. Hopefully this example will either help you find where you're going wrong, or at least let you confirm whether or not I understand what you're trying to do.

Enigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this