Right now, I have more than 25 vertices that form a model. I want to interpolate color linearly between the first and last vertex. The Problem is when I write the following code
glColor3f(1.0,0.0,0.0);
vertex3f(1.0,1.0,1.0);
vertex3f(0.9,1.0,1.0);
.
.`<more vertices>;
glColor3f(0.0,0.0,1.0);
vertex3f(0.0,0.0,0.0);
All the vertices except that last one are red. Now I am wondering if there is a way to interpolate color across these vertices without me having to manually interpolate color (instead natively, like how opengl does it automatically) at each vertex since, I will be having a lot more number of colors at various vertices. Any help would be extremely appreciated.
Thank you!
OpenGL will interpolate colors of pixels between one vertex and the next, but I don't know of any way to get it to automatically interpolate the values for intermediate vertexes. Normally, that's not particularly difficult though -- you don't want to write code for each individual vertex anyway, so adding the computation is pretty trivial:
class pointf {
GLfloat x, y, z;
};
std::vector<pointf> spots;
// ...
GLfloat start_blue = 1.0f;
GLfloat end_blue = 0.0f;
GLfloat start_green = 0.0f;
GLfloat end_green = 0.0f;
GLfloat start_red = 0.0f;
GLfloat end_red = 1.0f;
GLfloat size = spots.size();
glBegin(GL_POLYGON);
for (int i=0; i<spots.size(); i++) {
GLfloat red = start_red + (end_red-start_red) * i/size;
GLfloat green = start_green + (end_green-start_green) * i/size;
GLfloat blue = start_blue + (end_blue-start_blue) * i/size;
glColor3f(red, green, blue);
glVertex3f(spots[i].x, spots[i].y, spots[i].z);
}
glEnd();
One thing though: this does purely linear interpolation per vertex. It does not, for example, attempt to take into account the distance between one vertex and the next. I'd guess questions of how to do things like this are (at least part of) why OpenGL doesn't attempt this on its own.
Edit: for a gradient across a hemisphere, I'd try something like this:
// Blue light on the left
GLfloat blue[] = {0.0f, 0.0f, 1.0f, 1.0f};
GLfloat blue_pos[] = {-1.0f, 0.0f, -0.3f, 0.0f};
// red light on the right
GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat red_pos[] = {1.0f, 0.0f, -0.3f, 0.0f};
// turn on lighting:
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
// Set up the two lights:
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, blue);
glLightfv(GL_LIGHT0, GL_POSITION, blue_pos);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
glLightfv(GL_LIGHT1, GL_POSITION, red_pos);
// set up the material for our sphere (light neutral gray):
GLfloat sph_mat[] = {0.8f, 0.8f, 0.8f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sph_mat);
// Draw a sphere:
GLUquadric *q = gluNewQuadric();
gluQuadricOrientation(q, GLU_OUTSIDE);
gluQuadricDrawStyle(q, GLU_FILL);
gluQuadricNormals(q, GLU_SMOOTH);
gluSphere(q, 1.0, 64, 64);
For the moment, I've done the outside of a sphere, but doing a hemisphere isn't drastically different.
OpenGL will interpolate color within polygons, but not across polygons.
Related
I'm new to openGL and I drew a cube, then I place a camera inside the cube.
What I'm trying to achieve now is lighting the cube.
This is how I tried:
void draw(GLFWwindow* window)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { -1.5f, 1.0f, -1.0f, 1.0f };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection = glm::perspective(PI / 4, 1.f / 1.f, 1.0f, 10.0f);
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, -1.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model = glm::rotate(glm::mat4(1.f), PI, glm::vec3(0.0f, 1.0f, 0.0f));
....
....
}
But I don't see any lightings
But I don't see any lightings
Welcome to the world of the old-and-busted fixed function pipeline. You're using OpenGL as it was done 20 years ago. The mode of illumination you're using is doing the lighting calculations only at the vertex locations and then simply blends the resulting color over the triangles (or quads). Obviously that won't work if there's a lot of change in illumination over the span of a single triangle.
In your case the light source is very close to your cube, so that's not going to work. You should address this by ditching the fixed function pipeline (FFP) and use shaders. Seriously, the FFP has been out of fashion for some 13 years (first GLSL capable GPUs arrived on the market in 2003). FFP has been emulated with shaders created in-situ ever since.
Also (judging from your other questions related to drawing a cube) you don't supply face normals. Normals are essential to doing illumination calculations, so you'll have to supply those as well.
I have a problem when I draw a scaled (with glScale) gluSphere.
The Color changes after Scaling and the darker faces are not as dark as they should be...
This is how I set up the light:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
GLfloat LAmbient[4] = {0.2f, 0.2f, 0.2f, 1.0};
GLfloat LDiffuse[4] = {1.0f, 1.0f, 1.0f, 1.f};
GLfloat LSpecular[4] = {1.0f, 1.0f, 1.0f, 0.0f};
GLfloat LPosition[4] = {100.0f, -200.0f, -50.0f, 0.0f};
GLfloat LSpotDirec[3] = {0.0,0.0,0.0};
GLfloat LSpotCutOff = 180.0f;
GLfloat LSpotExponent = 0.0f;
GLfloat LAttenuationConst = 1.0f;
GLfloat LAttenuationLinear = 0.0f;
GLfloat LAttenuationQuadrat = 0.0f;
glLightfv(GL_LIGHT0, GL_AMBIENT, LAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, LPosition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LSpotDirec);
glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, &LSpotCutOff);
glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, &LSpotExponent);
glLightfv(GL_LIGHT0, GL_CONSTANT_ATTENUATION, &LAttenuationConst);
glLightfv(GL_LIGHT0, GL_LINEAR_ATTENUATION, &LAttenuationLinear);
glLightfv(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, &LAttenuationQuadrat);
Here is my code, that sets up a new glList, where the glu Sphere is drawn:
//Sphere
GLUquadric * pSphere = gluNewQuadric();
glNewList(LIST_SPHERE, GL_COMPILE);//'LIST_SPHERE' is defined above
gluQuadricTexture(pSphere, GL_FALSE);
gluQuadricOrientation(pSphere, GLU_OUTSIDE);
gluQuadricDrawStyle(pSphere, GLU_FILL);
gluQuadricNormals(pSphere, GLU_SMOOTH);
gluQuadricCallback(pSphere, GLU_ERROR, NULL);
gluSphere(pSphere, 1.0f, 20, 10);
glEndList();
and here is the code, where the List is called:
glPushMatrix();
//drawing a white Sphere with a radius of 6
glColor3f(1.0f, 1.0f, 1.0f);
glScalef(6.f,6.f,6.f);
glCallList(LIST_SPHERE);
glPopMatrix();
Sphere drawn with 'glScalef(1.0f, 1.0f, 1.0f)' (no scale)
Sphere drawn with 'glScalef(6.0f, 6.0f, 6.0f)'
I hope you have any Idea, why things are not working probably.
The normals are not being scaled correctly (they are non-unit length after scaling).
You have two options to solve this:
GL_NORMALIZE -- This will renormalize your normals, which is costly.
GL_RESCALE_NORMAL -- This will simply rescale them.
Option #2 is what you want here, since you applied a uniform scale (6.0x in every direction). If you had applied a non-uniform scale (e.g. glScalef (1.0f, 6.0f, 3.0f)) then this would not be a valid option and you would have to resort to option #1.
In either case, all you need to do is enable GL_NORMALIZE or GL_RESCALE_NORMAL to solve this problem.
I have a problem with opengl lighting, I have an object let say this one.
It's an original object without applying light effect, So the problem is when I try to add light to this object it hides the object colors and turn the object into this color
diffuse and specular component I used :
GLfloat diffu[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat spec[] = {0.5f, 0.5f, 0.5f, 0.5f};
GLfloat shinnes [] = {50};
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffu);
glLightfv(GL_LIGHT1, GL_SPECULAR, spec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,shinnes);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
So I need to understand what's the problem is ???
From this (http://www.glprogramming.com/red/chapter05.html) link:
Define Material Properties for the Objects in the Scene
An object's material properties determine how it reflects light and
therefore what material it seems to be made of. Because the
interaction between an object's material surface and incident light is
complex, specifying material properties so that an object has a
certain desired appearance is an art. You can specify a material's
ambient, diffuse, and specular colors and how shiny it is. In this
example, only these last two material properties - the specular
material color and shininess - are explicitly specified (with the
glMaterialfv() calls). (See "Defining Material Properties" for a
description and examples of all the material-property parameters.)
As soon as you start using lighting, the objects material properties (its color) are specified by the specular/diffuse... properties passed in by glMaterialfv(). So if you where using glColor() you now need to specify the material properties with glMaterialfv().
In your code, as well as setting the light specular and diffuse colour, you need to set the material specular and diffuse colour:
// Set light properties
GLfloat diffu[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat spec[] = {0.5f, 0.5f, 0.5f, 0.5f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffu);
glLightfv(GL_LIGHT1, GL_SPECULAR, spec);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
// Set material properties
GLfloat shinnes [] = {50};
GLfloat matdiffu[] = {1.0f, 0.f, 0.f, 1.0f};
GLfloat matspec[] = {1.0f, 0.f, 0.f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,matdiffu);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,matspec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,shinnes);
// Draw object
the diffu and spec define the color of the material as well; use {0f, 1f, 0f, 1f} and {0f, 1f, 0f, 0.5f} for a pure blue color (assuming RGBA)
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseColor);
along with GL_SHININESS, GL_SPECULAR to set the material properties of your geometry. Then the lighting will interact with the geometry material properly.
here is my Setup() function:
void Setup() // TOUCH IT !!
{
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//Parameter handling
glShadeModel (GL_SMOOTH);
//glEnable(GL_NORMALIZE);
glDepthFunc(GL_LEQUAL); //renders a fragment if its z value is less or equal of the stored value
glClearDepth(1);
//Set up light source
GLfloat light_position[] = { 20.0, 0.0, 0.0, 0.0 };
GLfloat ambientLight[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, light_position);
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
glEnable(GL_LIGHT0);
// polygon rendering mode
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glColorMaterial( GL_FRONT, GL_EMISSION );
// material identities
float specReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float ambReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float diffReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambReflection);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffReflection);
glMateriali(GL_FRONT,GL_SHININESS,20);
//// about texture
//glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// Black background
glClearColor(0.0f,0.0f,0.0f,1.0f);
}
here is my CalcNormal() function, where normals are being calculated. v1 and v2 are two vectors of a triangle:
Point CalcNormal(Point v1, Point v2)
{
Point normal;
normal.x = v1.y*v2.z - v1.z*v2.y;
normal.y = -v1.x*v2.z + v2.x*v1.z;
normal.z = v1.x*v2.y - v2.x*v1.y;
float dist1 = sqrt( pow(v1.x,2) + pow(v1.y,2) + pow(v1.z,2));
float dist2 = sqrt( pow(v2.x,2) + pow(v2.y,2) + pow(v2.z,2));
float dist = dist1*dist2;
normal.x = normal.x/dist;
normal.y = normal.y/dist;
normal.z = normal.z/dist;
return normal;
}
here is the vectors i send to CalcNormal():
Point n;
Point a1, a2;
a1.x=triangles.at(i).p2.x-triangles.at(i).p1.x; a1.y=triangles.at(i).p2.y-triangles.at(i).p1.y; a1.z=triangles.at(i).p2.z-triangles.at(i).p1.z;
a2.x=triangles.at(i).p3.x-triangles.at(i).p1.x; a2.y=triangles.at(i).p3.y-triangles.at(i).p1.y; a2.z=triangles.at(i).p3.z-triangles.at(i).p1.z;
n = CalcNormal(a1, a2);
and the result is like this:
rendering result
For smooth shading to be effective, you also need smooth normals - per vertex. The code you have posted so far shown only per-triangle normal calculations. In that case, the result will still look flat.
The easiest way to get smooth normals is, after you have calculated a normal per triangle, to average all normals of the adjacent triangles of each vertex. More complex schemes use a weighted average based on some function of the area of the triangles. There are also some heuristics whcih try to conserve hard edges, but as it is always the case with heuristics, there will always be some corner cases. Ideally, the normals are created when the model is created and stored in the file.
I am using GL_TRIANGLE_STRIP to draw my terrain to the screen, however when I compile and run the program I get nothing. When I change GL_TRIANGLE_STRIP to GL_LINES it shows up and works. What can I do to get it working with GL_TRIANGLE_STRIP?
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
glRotatef(-_angle, 0.0f, 1.0f, 0.0f);
GLfloat ambientColor[] = {.5, .5, .5, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
float scale = 5.0f / max(63.0,63.0);
glScalef(scale, scale*10, scale);
glTranslatef(-(float)(63.0) / 2, 0.0f, -(float)(63.0) / 2);
/*GLfloat lightColor0[] = {0.6f, 0.6f, 0.6f, 1.0f};
GLfloat lightPos0[] = {-0.5f, 4, 32, 0.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/
glColor3f(0.3f, 0.9f, 0.0f);
for(int x = 0;x<64;x++){
glBegin(GL_LINES);
for(int z = 0;z<63;z++){
glNormal3f(0,1,0);
Vertex(x,map[x][z],z);
Vertex(x,map[x][z+1],z+1);
}
glEnd();
}
for(int z = 0;z<64;z++){
glBegin(GL_LINES);
for(int x = 0;x<63;x++){
Vertex(x,map[x][z],z);
Vertex(x+1,map[x+1][z],z);
}
glEnd();
}
I'm not sure this is important but for my terrain I have code that makes it a 3d Gaussian distribution.
An explanation of TRIANGLE_STRIP is found by a Google search. There is an illustration on Wikipedia.
My advice is to print out the first five vertices of your vertex data, and draw it by hand on paper. The fact that GL_LINES work suggest that the right vertices are there, you might just drawing them in the wrong order.
Another piece of advice is to disable backface culling.
glDisable(GL_CULL_FACE);
As a random stab in the dark, do you have culling turned on? It may be that it's not drawing any triangles because the back of the triangles are invisible. Try adding:
glDisable(GL_CULL_FACE);
To you init code, or simply removing:
glEnable(GL_CULL_FACE);
From your init code.