Color of objects on the screen

Started by
4 comments, last by karwosts 14 years, 1 month ago
So I've been studying OpenGL through the red book (just finished the chapter on lighting) and have a question. Until here, to define the colors of objects I just used glColor3f or something similar and they would all show up with that color. When I started adding lights, this method obviously stopped working, and all the objects have the color of the light. If I want to draw, for example, a red cube and a blue cube and I have a white light on screen, do I have to do something like this all the time? (using only ambient and diffuse as an example): glEnable (GL_COLOR_MATERIAL); glColorMaterial (GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glColor3f (1, 0, 0); // Draw cube somewhere here glcolor3f (0, 0, 1); // Draw another cube somewhere here glDisable (GL_COLOR_MATERIAL); This is probably a stupid question but I want to understand this before moving to the next chapter. Thanks in advance.
Advertisement
Well yes, you'd need to call glColorMaterial in such situations.

HOWEVER,
Quote:
This is probably a stupid question but I want to understand this before moving to the next chapter.

Whatever book/guide/tutorial you are using, stop using it. It is teaching you concepts that are essentially artifacts from the stoneage of 3D. All the functions you are referring to (fixed function lighting, colour material, glColor, etc) are completely obsolete, and should not be used anymore today. Modern OpenGL doesn't even have these functions anymore. You will have to eventually unlearn everything if you continue on this path.

Modern implementations should use shaders for this.
Thank you for the answer.

Quote:Original post by Yann L
Whatever book/guide/tutorial you are using, stop using it. It is teaching you concepts that are essentially artifacts from the stoneage of 3D. All the functions you are referring to (fixed function lighting, colour material, glColor, etc) are completely obsolete, and should not be used anymore today. Modern OpenGL doesn't even have these functions anymore. You will have to eventually unlearn everything if you continue on this path.

Modern implementations should use shaders for this.


Oh... Really? I said it on my original post but I'm using the official OpenGL programming guide (red book) 6th edition. The only reason why I'm even following this is simply because it's the official book so I expected it to be correct... What should I even read then?
Like he said, it's the red book. Unfortunately, I'm not aware of any good free material that teaches "modern" OpenGL from the ground up. There's always the official spec but that's a bit heavy to digest for a beginner. And to eventually implement your own lighting model in shaders, it's not a bad idea to know how the original fixed-function lighting model in OpenGL worked.

Litz, you should keep in mind that some/many other things in that book are now also obsolete, like the matrix stacks. Nowadays, instead of calling those lighting or matrix or fog or whatever functions, you pass variables (light position for each light, light color, material color, vertex data (position, texture coordinates, normals) from VBO, the current modelview and projection matrices) directly into your shader, where you are responsible for both combining them into your own lighting model as well as transforming vertices yourself. With geometry shaders you can even emit new vertices alltogether, although you can save that for much later.

Quote:Original post by Litz
Oh... Really? I said it on my original post but I'm using the official OpenGL programming guide (red book) 6th edition.

That's already ancient, because it's for OpenGL 2.1 which is mostly deprecated now. Unfortunately, if the Amazon reviews are any indication, the 7th edition isn't any better. I guess you're gonna have to read the official spec for core profile and try to fill in the gaps with the deprecated info from the red book. Someone needs to write a good intro book on the new API, there seems to be a market gap here :)
This is really disappointing. I was having fun reading the book and seriously thought I was in the right direction. The official specification seems a bit hard to understand and without any actual books on the subject, I guess I'm doomed..
Don't be too disappointed. I went through the same hopeless feeling when I gave up learning fixed-pipeline stuff, but the new methods are really not that difficult to learn. You can probably get a simple shader running in under a day, and once you start to grasp the flexibility they provide you may find it more enjoyable than working with the old fixed pipeline.

Personally to me the new methods seem much easier, everything is streamlined and you don't have to mess with nearly as many functions and states. You just get to write a little shader program that does exactly what you want.

If you're interested in more textbooks, I'd recommend the "Orange Book" (OpenGL Shading Language, 3rd edition). I think it covers most everything you need to know about shaders and the new 3.0+ programming flow.

I've got 7th edition Red Book as well, and I'd agree its probably not worth your money to pick up a copy if you already have the 6th edition. It does have enough content to teach you how to setup shaders and VBO, but I'd say at least 60% of the book is still dedicated to depreciated functions.



[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement