Jump to content
  • Advertisement
Sign in to follow this  
FantasyVI

OpenGL improve my code. Only if you have 10 min of your time

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello
If someone has 10 min of his time can you look at my code and tell me if i have any bad skills or how can i improve my self in any way.
my software is just a robot that is rotating on the Y axis. do you think using VBO is better ?
I'll also include the exe file for you guys to see it.
http://www.mediafire...h5neccx6r0t4hr2

[attachment=6305:robot.png]

thank you very much :)

Example.cpp

#include <windows.h>
#include <iostream>
#include <GL/glu.h>
#include "example.h"

Example::Example()
{
m_rotationAngle = 0.0f;
z=-5.0f;
}

bool Example::init()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LINE_SMOOTH);
glDepthFunc(GL_LEQUAL); // This will color all the cube
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //This will only color the lines

return true;
}

void Example::prepare(float dt)
{
const float SPEED = 50.0f;
m_rotationAngle += SPEED * dt;
if (m_rotationAngle > 360.0f)
{
m_rotationAngle -= 360.0f;
}
}

void Example::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f,z); // move back the Screen 5.0 Units

glRotatef(m_rotationAngle ,0.0f, 1.0f, 0.0f);
//glRotatef(m_rotationAngle ,0.0f, 0.0f, 1.0f);

RenderHead();
RenderArms();
RenderStomek();
RenderLegs();
RenderFeets();
}

void Example::RenderHead()
{
glTranslatef(0.0f, 1.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);

glBegin(GL_QUADS);

glVertex3f(-0.3f, -0.3f, 0.3f);
glVertex3f( 0.3f, -0.3f, 0.3f);
glVertex3f( 0.3f, 0.3f, 0.3f);
glVertex3f(-0.3f, 0.3f, 0.3f);

glVertex3f(-0.3f, -0.3f, -0.3f);
glVertex3f(-0.3f, 0.3f, -0.3f);
glVertex3f( 0.3f, 0.3f, -0.3f);
glVertex3f( 0.3f, -0.3f, -0.3f);

glVertex3f(-0.3f, 0.3f, -0.3f);
glVertex3f(-0.3f, 0.3f, 0.3f);
glVertex3f( 0.3f, 0.3f, 0.3f);
glVertex3f( 0.3f, 0.3f, -0.3f);

glVertex3f(-0.3f, -0.3f, -0.3f);
glVertex3f( 0.3f, -0.3f, -0.3f);
glVertex3f( 0.3f, -0.3f, 0.3f);
glVertex3f(-0.3f, -0.3f, 0.3f);

glVertex3f( 0.3f, -0.3f, -0.3f);
glVertex3f( 0.3f, 0.3f, -0.3f);
glVertex3f( 0.3f, 0.3f, 0.3f);
glVertex3f( 0.3f, -0.3f, 0.3f);

glVertex3f(-0.3f, -0.3f, -0.3f);
glVertex3f(-0.3f, -0.3f, 0.3f);
glVertex3f(-0.3f, 0.3f, 0.3f);
glVertex3f(-0.3f, 0.3f, -0.3f);
glEnd();

glColor3f(0.0f, 0.0f, -1.0f);
glTranslatef(0.0f, -1.5f, 0.0f);
}

void Example::RenderArms()
{
//Right Arm
glTranslatef(0.6f, 0.5f, 0.0f);
glColor3f(1.0f, 0.0f, 1.0f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);

glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);

glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);

glEnd();

glTranslatef(-0.6f, -0.5f, 0.0f);


//----------------------------------------------------------------------------Left Leg
glTranslatef(-0.6f, 0.5f, 0.0f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);

glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);

glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);

glEnd();

glColor3f(-1.0f, 0.0f, -1.0f);
glTranslatef(0.6f, -0.5f, 0.0f);
}

void Example::RenderStomek()
{
glTranslatef(0.0f, 0.5f,0.0f);
glColor3f(1.0f, 0.0f,0.0f);

glBegin(GL_QUADS);

glVertex3f(-0.4f, -0.6f, 0.2f);
glVertex3f( 0.4f, -0.6f, 0.2f);
glVertex3f( 0.4f, 0.6f, 0.2f);
glVertex3f(-0.4f, 0.6f, 0.2f);

glVertex3f(-0.4f, -0.6f, -0.2f);
glVertex3f(-0.4f, 0.6f, -0.2f);
glVertex3f( 0.4f, 0.6f, -0.2f);
glVertex3f( 0.4f, -0.6f, -0.2f);

glVertex3f(-0.4f, 0.6f, -0.2f);
glVertex3f(-0.4f, 0.6f, 0.2f);
glVertex3f( 0.4f, 0.6f, 0.2f);
glVertex3f( 0.4f, 0.6f, -0.2f);

glVertex3f(-0.4f, -0.6f, -0.2f);
glVertex3f( 0.4f, -0.6f, -0.2f);
glVertex3f( 0.4f, -0.6f, 0.2f);
glVertex3f(-0.4f, -0.6f, 0.2f);

glVertex3f( 0.4f, -0.6f, -0.2f);
glVertex3f( 0.4f, 0.6f, -0.2f);
glVertex3f( 0.4f, 0.6f, 0.2f);
glVertex3f( 0.4f, -0.6f, 0.2f);

glVertex3f(-0.4f, -0.6f, -0.2f);
glVertex3f(-0.4f, -0.6f, 0.2f);
glVertex3f(-0.4f, 0.6f, 0.2f);
glVertex3f(-0.4f, 0.6f, -0.2f);

glEnd();

glColor3f(-1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -0.5f, 0.0f);
}

void Example::RenderLegs()
{
//Right Leg
glTranslatef(0.2f, -0.7f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);

glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);

glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);

glEnd();

glTranslatef(-0.2f, 0.7f, 0.0f);


//----------------------------------------------------------------------------Left Leg
glTranslatef(-0.2f, -0.7f, 0.0f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1);

glVertex3f(-0.1f, 0.5f, -0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);

glVertex3f( 0.1f, -0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, -0.1f);
glVertex3f( 0.1f, 0.5f, 0.1f);
glVertex3f( 0.1f, -0.5f, 0.1f);

glVertex3f(-0.1f, -0.5f, -0.1f);
glVertex3f(-0.1f, -0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, 0.1f);
glVertex3f(-0.1f, 0.5f, -0.1f);

glEnd();

glColor3f(-1.0f, -1.0f, 0.0f);
glTranslatef(0.2f, 0.7f, 0.0f);
}

void Example::RenderFeets()
{
//Right Feet
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.2f, -1.4f, 0.2f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.1f, 0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f(-0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, -0.3f);

glVertex3f(-0.1f, 0.1f, -0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);
glVertex3f(-0.1f, -0.1f, 0.3f);

glVertex3f( 0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f(-0.1f, -0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, -0.3f);
glEnd();

glTranslatef(-0.2f, 1.4f, -0.2f);


//---------------------------------------------------------------------Left Feet
glTranslatef(-0.2f, -1.4f, 0.2f);

glBegin(GL_QUADS);

glVertex3f(-0.1f, -0.1f, 0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f(-0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, -0.3f);

glVertex3f(-0.1f, 0.1f, -0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);
glVertex3f(-0.1f, -0.1f, 0.3f);

glVertex3f( 0.1f, -0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, -0.3f);
glVertex3f( 0.1f, 0.1f, 0.3f);
glVertex3f( 0.1f, -0.1f, 0.3f);

glVertex3f(-0.1f, -0.1f, -0.3f);
glVertex3f(-0.1f, -0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, 0.3f);
glVertex3f(-0.1f, 0.1f, -0.3f);
glEnd();

glColor3f(-1.0f, -1.0f, -1.0f);
glTranslatef(0.2f, 1.4f, -0.2f);
}


void Example::shutdown()
{

}

void Example::onResize(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, float(width) / float(height), 1.0f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


Main.cpp

#include <windows.h>
#include "glwindow.h"

#include "example.h"

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR cmdLine,
int cmdShow)
{
//Set our window settings
const int windowWidth = 1024;
const int windowHeight = 768;
const int windowBPP = 32;
const int windowFullscreen = false;

//This is our window
GLWindow Window(hInstance);

Example example;

//Attach our example to our window
Window.attachExample(&example);

//Attempt to create the window
if (!Window.create(windowWidth, windowHeight, windowBPP, windowFullscreen))
{
//If it fails
MessageBox(NULL, "Unable to create the OpenGL Window", "An error occurred", MB_ICONERROR | MB_OK);

Window.destroy(); //Reset the display and exit
return 1;
}

if (!example.init()) //Initialize our example
{
MessageBox(NULL, "Could not initialize the application", "An error occurred", MB_ICONERROR | MB_OK);

Window.destroy(); //Reset the display and exit
return 1;
}

while(Window.isRunning())
{
Window.processEvents();

float elapsedTime = Window.getElapsedSeconds();
example.prepare(elapsedTime); //Do any pre-rendering logic

example.render();

Window.swapBuffers();
}

example.shutdown(); //Free any resources
Window.destroy(); //Destroy the program window

return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I did not spend a lot of time looking at the code, but I would say you're off to a good start :)

Things to be careful with IMO:

1. I suggest establishing a coding convention, even if this is a hobby project. You have some variable names that start with upper case and others that start with lower case. And I see the same for functions. It looks messy.

2. You're not using indentation on your "if" statements. It's easier to read code if it follows appropriate indentation.

3. You may want to start looking into vertex arrays and VBOs. Immediate mode (glBegin()/glEnd()) is slower, and if you wanted to use DirectX, you won't find immediate mode in it.

4. I'm being too nitpicky with this one, but "Feet" is already plural. The word "Feets" doesn't exist. The singular version of "Feet" is "Foot". If someone else looks at your code, it's important for it to be correct / look good in all aspects.

5. By "Stomek", I think you meant "Stomach". Again, I'm being too picky, but it's important.


Please take this as constructive criticism... I hope it helps :)

Share this post


Link to post
Share on other sites
Since all your RenderBodypart functions are basically doing the same, I would create a single function that can be supplied with parameters.

Something like:

RenderQuad(float width, float height, vec3 position, vec4 color);

edit: I don't know GL, so I've put "vec" for vector types

Share this post


Link to post
Share on other sites
For the tiny amount of data you have a VBO is not going to give you any measurable performance improvement. It's definitely worth learning how to use them, because when you start getting serious you will need them, but they're not a requirement here.

The next step I would take here is to implement a simple shader to do the colouring. Once again, a shader will do absolutely nothing for your current requirements - your program is so basic that it will actually add a substantial amount of code to it. But it is a good idea to start learning shaders, and this is a good place to learn them; the program is so simple that you can focus on learning how to use them right rather than getting bogged down in whether or not the shader code is correct.

Some might disagree with that, arguing that it's too early to learn shaders, and in part I can understand where they're coming from. I would have said the same myself not too long ago. But time moves on, and longer term learning how to do this now can save you a lot of pain and suffering in the future.

Share this post


Link to post
Share on other sites
thank you very much guys for your help.
I just started learning OpenGL not long ago and I'm trying to make a decent game.
I'll take everyone advice and start correcting my spelling mistakes :D and start learning shaders and VBO.

thank you all for taking the time to read my code :) I really appreciate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!