Related
Trying to make some effect on these 2D shapes
How do I make the quad shape change color automatically without user interactive. Like a constant cycle of color from red, yellow and green?
void changecolor() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2f(0.0, 0.8);
glVertex2f(-0.2, 0.3);
glVertex2f(0.0, 0.0);
glVertex2f(0.2, 0.3);
glEnd();
}
Trying to make the quad to slow expansion animation as i run it. Currently it just shows the final expanded result as i run.
void q4() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(15, 15, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 0, 0);
glVertex2f(0.1, 0.1);
glVertex2f(0.1, -0.1);
glVertex2f(-0.1, -0.1);
glVertex2f(-0.1, 0.1);
glEnd();
}
The Problem arises when I go from Welcome screen which is in glOrtho to a other screen which is in gluPerspective.
The only way I can render them both is when I resize the window after clicking to play from welcome screen, otherwise I receive black screen.
I think I need to fix the glVertex3f values for it to render properly as when I move directly to function without going to welcome screen, the polygon renders properly
Do check the gif below:
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (key1 == 3){
glOrtho(0.0, 100.0, 0.0, 100.0, -5.0 , 10.0);
glMatrixMode(GL_MODELVIEW);
}
if (key1 == 1 || key1 == 2){
gluPerspective(45.0, (float)w/(float)h, 0.1f, 200.0);
gluLookAt(0.0, 0.0, 5.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
}
void par(float x1, float x2, float y1, float y2, float z1, float z2){
glColor3f(0.3,0.56,0.84);
glBegin(GL_POLYGON);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y1, z1);
glVertex3f(x2, y2, z1);
glVertex3f(x1, y2, z1);
glEnd();
}
void Drawkey1/2(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glTranslatef(0.0, 0.0, -22.0);
int i;
par(-8.7, 9.2, 9.0, 9.2, 0.0, 0.0);
par(-8.7, 9.2, -8.5, -8.7, 0.0, 0.0);
par(-8.5, -8.7, -8.7, 9.2, 0.0, 0.0);
par( 9.2, 9.0, -8.7, 9.2, 0.0, 0.0);
while(p != NULL){
par((p -> x)/2.0,(p -> x)/2.0 + 0.4,(p -> y)/2.0,(p -> y)/2.0 + 0.4, 0.0, 0.0);
p = p -> nexploration_ratet;
}
par(food_x/2.0, food_x/2.0 + 0.4 , food_y/2.0 , food_y/2.0 + 0.4, 0.0 , 0.0);
}
void Drawkey3(){
glColor3f(0.3,0.56,0.84);
glBegin(GL_POLYGON);
glVertex3f(0.0,0.0,0.0);
glColor3f(0.137,0.137,0.556);
glVertex3f(100.0,0.0,0.0);
glColor3f(0.196,0.196,0.8);
glVertex3f(100.0,100.0,0.0);
glVertex3f(0.0,100.0,0.0);
glEnd();
glColor3f(0.196,0.196,0.8);
glRectf(39.5,39.5,60.5,45.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,40,60,45);
glColor3f(0.137,0.137,0.556);
drawString(47,42,0,GLUT_BITMAP_HELVETICA_18,"USER");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,29.5,60.5,35.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,30,60,35);
glColor3f(0.137,0.137,0.556);
drawString(41,31,0,GLUT_BITMAP_HELVETICA_18,"NETWORK_PLAY");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,19.5,60.5,25.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,20,60,25);
glColor3f(0.137,0.137,0.556);
drawString(46,21,0,GLUT_BITMAP_HELVETICA_18,"HOW_TO");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,9.5,60.5,15.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,10,60,15);
glColor3f(0.137,0.137,0.556);
drawString(47,11,0,GLUT_BITMAP_HELVETICA_18,"EXIT");
glPushMatrix();
glColor3f(0.8,0.8,0.8);
drawString(25.5,92,0,GLUT_BITMAP_TIMES_ROMAN_24,"COMPUTER GRAPHICS PROJECT ");
drawString(35.5,80,0,GLUT_BITMAP_TIMES_ROMAN_24,"SRIJANA");
glPopMatrix();
glColor3f(0.137,0.137,0.556);
}
Update:
Fixed the error by making sure the glMatrixMode doesn't fetch GL_MODELVIEW when transition from GL_PROJECTION
I'm doing an SDL/OpenGL project on MinGW Windows Code::Blocks 12.11, using GLEW. I'm trying to implement 2D off-screen rendering, so that I can eventually try some fragment shader effects.
Here is the scene rendered to the default framebuffer:
However, if I render the scene to a Framebuffer Object's texture (called fbo_texture) and then try and render that to a full-screen quad, I get this:
The image appears flipped, mirrored and green. I'm pretty sure that the rendering to the Framebuffer is working correctly, but for the life of me I can't figure out why the texture is appearing so skewed.
Here is how I tried to render fbo_texture to a textured quad, after calling glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0). I am using glMatrixMode(GL_MODELVIEW) and glOrtho(0, screen_width, screen_height, 0, 0, 1) when the program initializes. Screen_width is 400 and screen_height is 400.
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fbo_texture);
glBegin(GL_QUADS);
x1= 0.5;
x2= 400.5;
y1= 0.5;
y2 = 400.5;
glTexCoord2f(0.0f, 0.0f); glVertex2f(x1, y1);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x2, y1);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x2, y2);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x1, y2);
glEnd();
glDisable(GL_TEXTURE_2D);
I really appreciate any help, please let me know if I should provide any more information.
This might not be your only problem, but from the part you show, two vertices are swapped in your texture coordinates. The 3rd of the 4 vertices is the top-right corner, with coordinates (x2, y2), so it should have texture coordinates (1.0f, 1.0f):
glTexCoord2f(0.0f, 0.0f); glVertex2f(x1, y1);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x2, y1);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x2, y2);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x1, y2);
You're also saying that you call glOrtho() after glMatrixMode(GL_MODELVIEW). glOrtho() sets a projection matrix, so it should normally be called after glMatrixMode(GL_PROJECTION).
I am trying to create the background of my solar system using skybox; however it's not outputting what I thought it would.
First of all, the displaying issue. The pictures below show the problem I'm having.
different angle:
another angle:
As you can see, the texture is mapped to only one side of the cube. That would be OK if the side it was being mapped to was the inside one. I don't get why it's not being mapped properly.
The second issue is that (as you can see from the pictures) as I rotate the camera, the box rotates with it and as I zoom out I can see the full box. I want the box to stay in the background always, and be only able to zoom in/out of my solar system. That way the stars are always in the background. I am not sure how to accomplish it.
Here is the code I'm using to render the skybox and solar system. (keep in my that the solar system works the way I intended it to)
this is the code for the skybox:
void Skybox::displaySkybox()
{
Images::RGBImage test[6]; //6 pictures for 6 sides
test[0]=Images::readImageFile(fileName); //I'll only use one for testing purposes
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
test[0].glTexImage2D(GL_TEXTURE_2D,0,GL_RGB);
// Save Current Matrix
glPushMatrix();
// Second Move the render space to the correct position (Translate)
glTranslatef(0,0,0);
// First apply scale matrix
glScalef(10000,10000,10000);
float cz = -0.0f,cx = 1.0f;
float r = 1.0f; // If you have border issues change this to 1.005f
// Common Axis Z - FRONT Side
glBegin(GL_QUADS);
glTexCoord2f(cx, cz); glVertex3f(-r,1.0f,-r);
glTexCoord2f(cx, cx); glVertex3f(-r,1.0f, r);
glTexCoord2f(cz, cx); glVertex3f( r,1.0f, r);
glTexCoord2f(cz, cz); glVertex3f( r,1.0f,-r);
glEnd();
// Common Axis Z - BACK side
glBegin(GL_QUADS);
glTexCoord2f(cx,cz); glVertex3f(-r,-1.0f,-r);
glTexCoord2f(cx,cx); glVertex3f(-r,-1.0f, r);
glTexCoord2f(cz,cx); glVertex3f( r,-1.0f, r);
glTexCoord2f(cz,cz); glVertex3f( r,-1.0f,-r);
glEnd();
// Common Axis X - Left side
glBegin(GL_QUADS);
glTexCoord2f(cx,cx); glVertex3f(-1.0f, -r, r);
glTexCoord2f(cz,cx); glVertex3f(-1.0f, r, r);
glTexCoord2f(cz,cz); glVertex3f(-1.0f, r,-r);
glTexCoord2f(cx,cz); glVertex3f(-1.0f, -r,-r);
glEnd();
// Common Axis X - Right side
glBegin(GL_QUADS);
glTexCoord2f(cx, cx); glVertex3f(1.0f, -r, r);
glTexCoord2f(cz, cx); glVertex3f(1.0f, r, r);
glTexCoord2f(cz, cz); glVertex3f(1.0f, r,-r);
glTexCoord2f(cx, cz); glVertex3f(1.0f, -r,-r);
glEnd();
// Common Axis Y - Draw Up side
glBegin(GL_QUADS);
glTexCoord2f(cz, cz); glVertex3f( r, -r, 1.0f);
glTexCoord2f(cx, cz); glVertex3f( r, r, 1.0f);
glTexCoord2f(cx, cx); glVertex3f(-r, r, 1.0f);
glTexCoord2f(cz, cx); glVertex3f(-r, -r, 1.0f);
glEnd();
// Common Axis Y - Down side
glBegin(GL_QUADS);
glTexCoord2f(cz, cz); glVertex3f( r, -r, -1.0f);
glTexCoord2f(cx, cz); glVertex3f( r, r, -1.0f);
glTexCoord2f(cx, cx); glVertex3f(-r, r, -1.0f);
glTexCoord2f(cz, cx); glVertex3f(-r, -r, -1.0f);
glEnd();
// Load Saved Matrix
glPopMatrix();
}
Here is the code for the Solar System:
void SolarSystem::display(GLContextData& contextData) const
{
glDisable(GL_LIGHTING);
Skybox test("images/test.jpg");
test.displaySkybox();
drawCircle(800, 720, 2, 100);
//SUN
//Picture location, major radius, minor radius, major orbit, minor orbit, angle
Planet Sun ("images/Sun.jpg",
100, 99, 200.0, 0.0, 0.0);
double sunOrbS = 0;
double sunRotS = rotatSpeed/10;
//orbit speed, rotation speed, moon reference coordinates (Parent planet's major and minor Axis)
Sun.displayPlanet(sunOrbS, sunRotS, 0.0, 0.0);
//EARTH
GLfloat light_diffuse[] = { 1.5, 1.5, 1.5, 1.5 };
GLfloat pos[] = { 200.0, 0.0, 0.0, 1.0};
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
Planet Earth ("images/earth.jpg",
50, 49, 500.0, 450.0, 23.5);
double eaOrbS = orbitSpeed;
double eaRotS = rotatSpeed*3;
Earth.displayPlanet(eaOrbS, eaRotS, 0.0, 0.0);
//EARTH'S MOON
Planet Moon ("images/moon.jpg",
25, 23, 100.0, 100.0, 15);
double moOrbS = rotatSpeed*4;
double moRotS = eaOrbS;
Moon.displayPlanet(moOrbS, moRotS, Earth.getMajorAxis(), Earth.getMinorAxis());
orbitSpeed+=.3;
if (orbitSpeed > 359.0)
orbitSpeed = 0.0;
rotatSpeed+=1.0;
if (rotatSpeed > 7190.0)
rotatSpeed = 0.0;
}
void Skybox::displaySkybox()
{
...
glPushMatrix();
...
glPushMatrix();
...
glPopMatrix();
...
// huh? where's the second glPopMatrix()?
}
Don't do that. Make sure you Pop as much as you Push.
If i comment out DrawGLScene(), i see a large shaded triangle, if I comment out drawtri() i see a square texture drawn. But I am not able to combine both - when both func's are called, I see only triangle outline and the texture is rendered with a strong red filter applied.
What could be the problem?
void DrawGLScene()
{
int x, y;
float float_x, float_y, float_xb, float_yb;
float x0=0,y0=0,x1=10,y1=10,z=-3;
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glEnable(GL_TEXTURE_2D);
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-12.0f); // move 12 units into the screen.
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
glPolygonMode(GL_BACK, GL_FILL);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_QUADS);
glTexCoord2f( 0,0);
glVertex3f( x0, y0, z );
glTexCoord2f( 0, 1 );
glVertex3f( x0, y1, z );
glTexCoord2f( 1, 1);
glVertex3f( x1, y1, z );
glTexCoord2f( 1, 0 );
glVertex3f( x1, y0,z );
glEnd();
glDisable(GL_TEXTURE_2D);
// since this is double buffered, swap the buffers to display what just got drawn.
// glutSwapBuffers();
}
void drawtri() {
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
}
void zdisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
glTranslatef(0.0f,0.0f,-3.0f);
//drawtri();
DrawGLScene();
glutSwapBuffers();
}
The red filter is caused by the applied color:
glColor3f(0.0f,0.0f,1.0f);
Reset the color to white before drawing the quad:
glColor3f(1.0f,1.0f,1.0f);
The outline is caused by the following specification:
glPolygonMode(GL_FRONT, GL_LINE);
Specify fill mode:
glPolygonMode(GL_FRONT, GL_FILL);
When calling both functions, did you actually call the methods in the order in which the code shows them? Because I suspect that the order was reversed. If I'm right, I think you must reset the glPolygonMode when drawing the triangle. Try adding glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) before the call to glBegin(GL_TRIANGLES).