I am trying to create a room in openGL and i have the following quads however wall2 and wall4 do not show. I am guessing this is something to do with perspectives as if i go outside of the room i can see they have been rendered.
glColor3f(0.1f, 0.9f, 0.9f);
//Wall1
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//Wall2
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//Wall3
glColor3f(0.4f, 0.9f, 0.1f);
glBegin(GL_QUADS);
glNormal3f(-1,0, 0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//Wall4
glColor3f(0.1f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();
Try disabling face culling! :)
Related
Is it possible in opengl to draw this type to triangles?
I have already tried
glBegin(GL_TRIANGLES);
glBegin(GL_LINE_LOOP);
Code and Result i got using glBegin(GL_LINE_LOOP)
Code:
glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex2f(-30.0, 30.0);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(30.0, 30.0);
glColor3f(0.5f, 0.0f, 1.0f);
glVertex2f(0.0, -30);
glEnd();
Result:
I would change your code to this:
glLineWidth(5.0); // this makes 5 pixel thick lines
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 1.0f); glVertex2f(-30.0, 30.0); glVertex2f( 30.0, 30.0);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f( 30.0, 30.0); glVertex2f( 0.0,-30.0);
glColor3f(0.5f, 0.0f, 1.0f); glVertex2f( 0.0,-30.0); glVertex2f(-30.0, 30.0);
glEnd();
glLineWidth(1.0); // return state to original conditions
So you should have thick lines and the coloring should not interpolate between lines ...
To remove color gradients, you have at least two options:
You could call glShadeModel(GL_FLAT); before rendering the triangle.
You could use GL_LINES, like so:
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex2f(-30.0, 30.0);
glVertex2f(30.0, 30.0);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(30.0, 30.0);
glVertex2f(0.0, -30);
glColor3f(0.5f, 0.0f, 1.0f);
glVertex2f(0.0, -30);
glVertex2f(-30.0, 30.0);
glEnd();
To increase line width, you could use glLineWidth.
But you'll see that ends of wide lines normally don't look as pretty and round as on your image. If that's a problem for you, you'll have to draw the shape using lots of carefully placed GL_TRIANGLES instead.
Im working on a 3D platformer where theres a large platform and a character made up of multiple cubes.
However when I try rotating the Y axis of the characters cubes it doesn't rotate them at their centerpoint. They just orbit as a whole around the platform in a huge circle. I'm guessing my glRotatef's are in the wrong order but I cant seem to figure out what order they should be to make the group of cubes rotate only around their center point.
My code for drawing them is:
for (int i = 0; i < Models.size(); i++){ // theres only one model this has to go through that stores the group of cubes for the character
glPushMatrix(); // set rotation for the whole group (I would expect...)
glRotatef(Models.at(i)->ModelRotation.X,1,0,0);
glRotatef(Models.at(i)->ModelRotation.Y,0,1,0);
glRotatef(Models.at(i)->ModelRotation.Z,0,0,1);
for (int j = 0; j < Models.at(i)->Parts.size(); j++) // For each cube in the character,
Models.at(i)->Parts.at(j)->Render(); // draw the cube
glPopMatrix();
}
Model is a struct that just has a vector3 called "ModelRotation" and a normal vector called "Parts" which stores all the cubes for the character.
My function for rendering the cube in the cube class is:
void Render(){
glPushMatrix();
glTranslatef( Position.X,
Position.Y,
Position.Z
);
glColor3f( Color.R,
Color.G,
Color.B
);
glScalef( Size.X,
Size.Y,
Size.Z
);
// Render the front quad
//glBindTexture(GL_TEXTURE_2D, Faces[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glEnd();
// Render the left quad
//glBindTexture(GL_TEXTURE_2D, Faces[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( 1.0f, 1.0f, 1.0f );
glEnd();
// Render the back quad
//glBindTexture(GL_TEXTURE_2D, Faces[2]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, 1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, 1.0f );
glEnd();
// Render the right quad
//glBindTexture(GL_TEXTURE_2D, Faces[3]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 0); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( -1.0f, 1.0f, 1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glEnd();
// Render the top quad
//glBindTexture(GL_TEXTURE_2D, Faces[4]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 0); glVertex3f( -1.0f, 1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, 1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glEnd();
// Render the bottom quad
//glBindTexture(GL_TEXTURE_2D, Faces[5]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glEnd();
glPopMatrix();
};
Any help would be very much appreciated.
I'm guessing it's just a simple mistake I can't put together.
I'll show more code if it's needed.
The way OpenGL applies the transformations is from bottom to top between PopMatrix() and PushMatrix(), that is the inverse from the way you are actually coding them.
In your code, the order in which you are calling is flipped like this:
Rotatef();
Translatef();
Scalef();
It should be:
Translatef();
Rotatef();
Scalef();
In other words, you want to translate always as last (meaning putting it into your code as the very first line after the PopMatrix()) unless you have a good reason (a particular graphic effect or scene rendering), so that you first apply scaling and rotation transformations and then you translate the object in world coordinates.
For rotating multipart objects (as your case) you should probably translate twice, as explained here: Rotating a multipart object
So your code outside the cube class could be something like:
for (int i = 0; i < Models.size(); i++){
glPushMatrix();
glTranslatef(
Models.at(i)->WorldPosition.X,
Models.at(i)->WorldPosition.Y,
Models.at(i)->WorldPosition.Z,
);
glRotatef(Models.at(i)->ModelRotation.X,1,0,0);
glRotatef(Models.at(i)->ModelRotation.Y,0,1,0);
glRotatef(Models.at(i)->ModelRotation.Z,0,0,1);
for (int j = 0; j < Models.at(i)->Parts.size(); j++)
Models.at(i)->Parts.at(j)->Render(); // draw the cube
glPopMatrix();
}
In many demoscene productions you can see the effect of flashing white screen when percussion beat happens (https://www.youtube.com/watch?v=2SbGffUzHSs). I have coded such effect and in works fine. Code of fading function (it ie executed in the render() function):
void fade()
{
glLoadIdentity();
glTranslatef (0.0f, 0.0f, -5.0f);
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
if (alpha <= 1)
{
glColor4f(1.0, 1.0, 1.0, alpha);
glBegin(GL_QUADS);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
alpha += 0.0025;
}
else
{
glColor4f(1.0, 1.0, 1.0, alpha_inv);
glBegin(GL_QUADS);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
alpha_inv -= 0.0025;
}
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
return;
}
I have two issues:
I need some kind of parameter for white flash duration control (ie. 1st flash lasts 1 second, 2 flash lasts only 0,25 second)
The effect has to have the same speed on each computer (slow and fast).
What can I do in the code above to achieve it?
I want to have 2 squares under each other centered in an openGL scene.
I want it like this: https://image.prntscr.com/image/776a14cd345047a1985072e0cf279ceb.png
How can I do this, with glVertex and glColor?
Thanks
Something like this:
void draw() {
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f( 1.0f, -1.0f);
glVertex2f( 1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-0.5f, 0.8f);
glVertex2f( 0.8f, 0.8f);
glVertex2f( 0.8f, 0.0f);
glVertex2f(-0.5f, 0.0f);
glVertex2f(-0.3f, 0.0f);
glVertex2f( 0.6f, 0.0f);
glVertex2f( 0.6f,-0.6f);
glVertex2f(-0.3f,-0.6f);
glEnd();
}
It was one of my better eye-balling jobs. Output (left is mine, bottom-right is yours):
Note that I am drawing a full-screen black quad instead of what one would usually do (glClearColor(0.0f, 0.0f, 0.0f); then glClear(GL_COLOR_BUFFER_BIT);) since you asked specifically in terms of glColor and glVertex.
I want to render outline font near playing area, but these two parts are in conflict. If I render only outline font, it's ok. When I render also playing area, textures of cubes have defect and light is changed also. When I render only playing area, it's ok. Here are some pictures:
http://img.obrazok.com/Untitled.usdh.png
Outline font part:
//Display-lists
GLuint fontBase;
GLYPHMETRICSFLOAT gmf[256];
//********************************
//3D Font
//********************************
GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
fontBase = glGenLists(256); // Storage For 256 Characters
font = CreateFont( -12, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Comic Sans MS"); // Font Name
HDC hDC = GetDC(GetActiveWindow());
SelectObject(hDC, font); // Selects The Font We Created
wglUseFontOutlines( hDC, // Select The Current DC
0, // Starting Character
255, // Number Of Display Lists To Build
fontBase, // Starting Display Lists
0.0f, // Deviation From The True Outlines
0.2f, // Font Thickness In The Z Direction
WGL_FONT_POLYGONS, // Use Polygons, Not Lines
gmf); // Address Of Buffer To Recieve Data
}
GLvoid KillFont(GLvoid) // Delete The Font
{
glDeleteLists(fontBase, 256); // Delete All 256 Characters
}
void printString(const char *string, float x, float y, float z) // Custom GL "Print" Routine
{
float length=0; // Used To Find The Length Of The Text
for (unsigned int loop=0;loop<(strlen(string));loop++) // Loop To Find Text Length
{
length+=gmf[string[loop]].gmfCellIncX; // Increase Length By Each Characters Width
}
glPushMatrix();
glRotatef(90,0.0,0.0,1.0);
glRotatef(90,1.0,0.0,0.0);
glTranslatef(x,y,z);
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(fontBase); // Sets The Base Character to 0
glCallLists(strlen(string), GL_UNSIGNED_BYTE, string); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
glPopMatrix();
}
Render playing area:
//Display-lists
GLuint ls_mapWalls;
void renderWalls() {
for (int x = 0; x < mapSize; x++) {
for (int y = 0; y < mapSize; y++) {
glPushMatrix();
glTranslatef(x*2.0f,y*2.0f,0.0f);
if (map[x][y] == 'X') {
renderWall();
}
glPopMatrix();
}
}
}
void renderWall() {
glPushMatrix();
renderTexturedCube(1); //1 or 2
glPopMatrix();
}
void renderTexturedCube(int color) {
glEnable(GL_TEXTURE_2D);
switch (color) {
case 1 :
//normal wall
setMaterialColor(1.0f, 1.0f, 1.0f, 0.2f);
break;
case 2 :
//red wall
setMaterialColor(1.0f, 0.0f, 0.0f, 1.0f);
break;
}
glBindTexture(GL_TEXTURE_2D, texid[0]);
glBegin(GL_QUADS);
// Front Face
glNormal3f(0.0,0.0,1.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f(0.0,0.0,-1.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f(0.0,1.0,0.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f(0.0,-1.0,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f(1.0,0.0,0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0,0.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
setMaterialColor(1.0f, 1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
}
Render part:
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLdouble ex = vzd*cos(fi)*cos(xi);
GLdouble ey = vzd*sin(fi)*cos(xi);
GLdouble ez = vzd*sin(xi);
gluLookAt( ex, ey, ez, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f );
printString("Dyna Blaster Beta", 10, 10, 10, 1.0, 0.0, 0.0);
glCallList(ls_mapWalls);
glutSwapBuffers();
}
Init part:
bool init(void)
{
//setup OpenGL
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);
//files load
loadMap();
loadTextures();
//next init
ls_mapWalls = glGenLists(1);
glNewList(ls_mapWalls, GL_COMPILE);
renderWalls();
glEndList();
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 10.0, 10.0, 10.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
BuildFont();
fi = 0.385f; xi = 0.515f; vzd = 69.2f;
return true;
}
If you have some question or want see another part of code, just ask. Thank you.
The documentation for wglUseFontOutlines at MSDN says this:
With WGL_FONT_POLYGONS, the created display lists call glFrontFace( GL_CW )
or glFrontFace( GL_CCW ); thus the current front-face value might be altered.
OpenGL defults to GL_CCW; so chances are, if you set it back with
glFrontFace( GL_CCW );
after drawing the text, your playing area will render correctly.