OpenGL 2D moving relation - c

I have the two methods hich are doing the same thing. Now I am wondering what is their differences and which one can perform faster.
Here are the methods:
1th Method
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2f( x, y);
glTexCoord2f( 1, 0); glVertex2f( x+texWidth, y);
glTexCoord2f( 1, 1); glVertex2f( x+texWidth, y+texHeight);
glTexCoord2f( 0, 1); glVertex2f( x, y+texHeight);
glEnd();
x++; y++;
2th Method
glPushMatrix();
glTranslatef(x, y, 0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2f( 0.0f, 0.0f);
glTexCoord2f( 1, 0); glVertex2f( 1.0f, 0.0f);
glTexCoord2f( 1, 1); glVertex2f( 1.0f, 1.0f);
glTexCoord2f( 0, 1); glVertex2f( 0.0f, 1.0f);
glEnd();
glPopMatrix();
x++; y++;

Regarding performance, you are using immediate mode, which is slow anyway (its the "old" way of doing things; unfortunately most OpenGL tutorials on the web are hopelessly outdated). Use buffers (gpu-side) like VBOs and VAOs if you really care about performance.
A nice modern tutorial is http://open.gl/ which introduces VBOs here: http://open.gl/drawing

The second method is alot faster, because it is translating the whole matrix, and doesn't do many calculations as it does in method one.

The second method is a lot faster, because it translates the whole QUAD matrix with less calculations.

Related

Lighting not working in opengl [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am having trouble getting the lighting to work on just the decline portion of my ground. Below is my code for the ground and the decline (making it a ditch):
static void ground(double x, double y, double z, double dx, double dy,
double dz){
float white[] = {1,1,1,1};
float Emission[] = {0.0,0.0,0.01*emission,1.0};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,Emission);
// Save transformation
glPushMatrix();
// Offset, scale and rotate
glTranslated(x,y,z);
glScaled(dx, dy, dz);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(0.5, 1.0, 0.5);
glNormal3f(0,0,0);
glTexCoord2f(0.0, 0.0); glVertex3f(-100, 0, -300);
glTexCoord2f(300.0,0.0); glVertex3f(-100,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-3,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(-3,0,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.5);
glNormal3f(0,0,-1);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.99,0,-300);
glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.5);
glNormal3f(0,0,1);
glTexCoord2f(0.0, 0.0); glVertex3f(0.99,-1,-300);
glTexCoord2f(300.0,0.0); glVertex3f(0.99,-1,300);
glTexCoord2f(300.0,300.0); glVertex3f(2.99,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(2.99,0,-300);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glColor3f(0.5, 1.0, 0.5);
glNormal3f(0,0,0);
glTexCoord2f(0.0, 0.0); glVertex3f(2.99, 0, -300);
glTexCoord2f(300.0,0.0); glVertex3f(2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(100,0,300);
glTexCoord2f(0.0,300.0); glVertex3f(100,0,-300);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
So the code the doesn't have lighting working correctly is the middle two snippets of "glBegin(GL_QUADS)"
This is just an instance of GIGO (garbage in, garbage out):
glNormal3f(0,0,0);
Nope. That is not a valid normal vector, and will totally break any lighting calculation.
The next one
glNormal3f(0,0,-1);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.99,0,-300);
glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
is at least some non-null vector, but it isn't normal to the face you are describing, so the lighting will be just wrong.

OpenGL 3.3 won't draw my triangle

I'm following these tutorials using C instead of C++:
Tutorial 2, short extension to tutorial 2.
The only change I made to port it was changing Vector3f[3] into GLfloat[9]. The version with GLfloat[1] instead of Vector3f[1] works correctly. I think this change might be the reason of glDrawArrays not working but I don't know how to fix it.
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
GLuint VBO_id;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void CreateVertexBuffer()
{
GLfloat Vertices[9] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
1.0f, 0.0f };
glGenBuffers(1, &VBO_id);
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices,
GL_STATIC_DRAW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 03");
glutDisplayFunc(RenderSceneCB);
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
From here.
OpenGL 3.0 was the last revision of the specification which fully supported both fixed and programmable functionality. Even so, most hardware since the OpenGL 2.0 generation lacked the actual fixed-function hardware. Instead, fixed-function processes are emulated with shaders built by the system.
In OpenGL 3.2, the Core Profile lacks these fixed-function concepts. The compatibility profile keeps them around. However, most newer features of OpenGL cannot work with fixed function, even when it might seem theoretically possible for them to interact.
Sounds like your version of OpenGL doesn't support the fixed function pipeline. Either use an older version of OpenGL that does or write and load a shader as shown in Tutorial 4.

Half cylinder/Closed cylinder in opengl

I currently have a cylinder
glPushMatrix();
glTranslatef(cylinder->pos[0], cylinder->pos[1], cylinder->pos[2]);
glRotatef(cylinder->angle, -1.0, 0, 0);
gluDisk(quad, 0.0, cylinder->radius, 300, 90);
gluCylinder(quad, cylinder->radius, cylinder->radius, cylinder->height, 30, 1);
gluDeleteQuadric(quad);
glPopMatrix();
but it only renders half of a cylinder instead of the whole thing (the half coming towards the view). Second issue is I don't know how to close the cylinder. The gludisk only closes the bottom of the cylinder and I'm not sure how to close the top side.
If I understand what you're describing, it sounds like you might have back face culling enabled. Do you have a glEnable(GL_CULL_FACE) call in your code? This will eliminate all polygons that face away from you. In any case, once you draw a cylinder that is closed at both sides, it will be fine to keep it enabled.
You can close the cylinder at the top with another gluDisk() call, after applying a translation that moves it to (0, 0, height). For correctness, I believe you need to rotate the disk that closes the cylinder at the bottom. Otherwise it will face to the inside of the cylinder instead of to the outside, which affects the normals, and would get it eliminated by back face culling when it should be visible.
For the full thing, it should look something like this (untested):
glPushMatrix();
glTranslatef(cylinder->pos[0], cylinder->pos[1], cylinder->pos[2]);
glRotatef(cylinder->angle, -1.0f, 0.0f, 0.0f);
gluCylinder(quad, cylinder->radius, cylinder->radius, cylinder->height, 30, 1);
glPushMatrix();
glTranslatef(0.0f, 0.0f, cylinder->height);
gluDisk(quad, 0.0f, cylinder->radius, 30, 1);
glPopMatrix();
glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
gluDisk(quad, 0.0f, cylinder->radius, 30, 1);
gluDeleteQuadric(quad);
glPopMatrix();

Rotate object around another object

I drew two rectangles in my scene, and now I'm trying to rotate one rectangle around the other.
When I press the "Left key" my first rectangle moves in direction to the other, but I don't have the effect of rotate because it is only moving in a straight line in direction of the other rectangle.
How do I change the Z direction of the first rectangle?
tl;dr: It's like a solar system, but I don't understand how can I have the depth effect.
I'm using GLUT and OpenGL.
EDIT: More Info:
This is my current code (function that shows the scene):
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(-70.0f, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(xCor, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glFlush();
}
And a image to help:
Please enable depth test to hide objects beyond other objects.
Give a high value z vaue for the far object.
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-70.0f, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glPopMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(xCor, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -26.0f); // Z value of behind object is set far from eye
glPopMatrix();
What you want to do is moderately complicated.
The only way I know to locate rectangles in 3-space is to build them using glVertex3f() like this:
glBegin(GL_QUADS);
glVertex3f(x0, y0, z0);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();
Your "solar system" will have to be located in screen coordinates: X->left/right, Y->up/down, Z->into/out of screen. To create the effect of orbiting, you'll have to recalculate all four X and Z for each movement increment.
I also suggest using gluLookAt() if you want to see a different view.
The man page for glPerspective() says that zNear is always positive but you've given it a negative number. I'd leave that line commented out until I saw some orbiting.
HTH.

Texture Mapping on Quad not working

I'm trying to write some code that will render a texture onto a simple square, but have been having a lot of trouble getting it to work. I downloaded some example source code for texture mapping here and compiled it and it seems to do texture mapping fine onto their cylinder. Then, I added some of my code to see if it would render the texture onto my square, but it just textures it with a solid color that seems to be halfway between red and black instead of a checkerboard like it appears on the cylinder. Here is the code I added, right after the call to gluCylinder on the first pass:
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1,0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0,1);
glVertex2f(-1, 1);
glEnd();
Is there any reason why textures would render normally on the cylinder but not on my quad?
The example code is enabling automatic texture coordinate generation, so your glTexCoord2f calls are being ignored. The automatic generation is being projected onto a Z plane. Since your quad is a Y plane, it's essentially just stretching one pixel of your texture across the entire quad.
To get it to work, disable the automatic texture generation before your glBegin, and then re-enable it after the glEnd.
i.e.:
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1,0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0,1);
glVertex2f(-1, 1);
glEnd();
glEnable (GL_TEXTURE_GEN_S);
glEnable (GL_TEXTURE_GEN_T);
Alternatively, you could set the automatic texture generation to project on the Y plane, and omit the glTexCoord2f calls, and then set it back to Z when you're done:
float zPlane[4] = {0., 0., 1., 0.};
float yPlane[4] = {0., 1., 0., 0.};
glTexGenfv (GL_T, GL_OBJECT_PLANE, yPlane);
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
glTexGenfv (GL_T, GL_OBJECT_PLANE, zPlane);

Resources