Jump to content
  • Advertisement

C++ y = Sin(x). Legacy OpenGL 1.1, FreeGLUT






Configuration: All Configurations
Platforms: All Platforms

C/C++ -> Genaral -> Additional Include Directories:

Linker -> Input -> Additional Dependencies

Configuration: All Configurations
Platforms: Win32

Linker -> General -> Additional Library Directories:

Build Events -> Post-Build Event
xcopy /y /d "$(SolutionDir)Libs\freeglut-3.0.0-2\lib\Win32\freeglut.dll" "$(OutDir)"

Configuration: All Configurations
Platforms: x64

Linker -> General -> Additional Library Directories:

Build Events -> Post-Build Event
xcopy /y /d "$(SolutionDir)Libs\freeglut-3.0.0-2\lib\Win64\freeglut.dll" "$(OutDir)"



#include <GL/freeglut.h>
#include <string>
#include <math.h>
// Hide the console
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
const float M_PI_F = (float)M_PI;
const float g_fieldWidth = 4 * M_PI_F;
const float g_fieldHeight = 4;
const float g_halfFieldWidth = g_fieldWidth / 2;
const float g_halfFieldHeight = g_fieldHeight / 2;
const float g_onePercentOfHalfWidth = (float)g_halfFieldWidth / 100;
const float g_onePercentOfHalfHeight = (float)g_halfFieldHeight / 100;
void drawText(float x, float y, std::string text, float r, float g, float b)
    glColor3f(r, g, b);
    glRasterPos2f(x, y);
    glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)text.c_str());
void drawLine(
    float x0, float y0, float x1, float y1,
    float r, float g, float b)
    glColor3f(r, g, b);
        glVertex2f(x0, y0);
        glVertex2f(x1, y1);
void drawSin(float r, float g, float b)
    const float step = (float)M_PI / 10;
    float xPrev = -2 * (float)M_PI;
    for (size_t i = 0; i < 40; i++)
        float x0 = xPrev;
        float y0 = sin(x0);
        float x1 = x0 + step;
        float y1 = sin(x1);
        xPrev = x1;
        drawLine(x0, y0, x1, y1, r, g, b);
void drawCoordinates(float r, float g, float b)
    float halfOfLengthOfLine = 4;
    // 1
    drawText(-14 * g_onePercentOfHalfWidth, g_halfFieldHeight / 2, "1", r, g, b);
        -halfOfLengthOfLine * g_onePercentOfHalfWidth,
        g_halfFieldHeight / 2,
        halfOfLengthOfLine * g_onePercentOfHalfWidth,
        g_halfFieldHeight / 2, r, g, b);
    // -1
    drawText(-18 * g_onePercentOfHalfWidth, -g_halfFieldHeight / 2, "-1", r, g, b);
        -halfOfLengthOfLine * g_onePercentOfHalfWidth,
        -g_halfFieldHeight / 2, 2 * g_onePercentOfHalfWidth,
        -g_halfFieldHeight / 2, r, g, b);
    // Pi
        M_PI_F - 5 * g_onePercentOfHalfWidth,
        7 * g_onePercentOfHalfHeight, "Pi", r, g, b);
        M_PI_F, -halfOfLengthOfLine * g_onePercentOfHalfHeight,
        M_PI_F, halfOfLengthOfLine * g_onePercentOfHalfHeight,
        r, g, b);
    // -Pi
        -M_PI_F - 7 * g_onePercentOfHalfWidth,
        7 * g_onePercentOfHalfHeight, "-Pi", r, g, b);
        -M_PI_F, -halfOfLengthOfLine * g_onePercentOfHalfHeight,
        -M_PI_F, halfOfLengthOfLine * g_onePercentOfHalfHeight,
        r, g, b);
void draw()
        g_halfFieldWidth - 80 * g_onePercentOfHalfWidth,
        g_halfFieldHeight - 20 * g_onePercentOfHalfHeight,
        "y = Sin(x)", 1, 1, 1);
    // X
    drawLine(-g_halfFieldWidth, 0, g_halfFieldWidth, 0, 1, 0, 0);
        g_halfFieldWidth - 10 * g_onePercentOfHalfWidth,
        -10 * g_onePercentOfHalfHeight, "X", 1, 0, 0);
    // Y
    drawLine(0, -g_halfFieldHeight, 0, g_halfFieldHeight, 0, 1, 0);
        -10 * g_onePercentOfHalfWidth,
        g_halfFieldHeight - 15 * g_onePercentOfHalfHeight, "Y", 0, 1, 0);
    drawCoordinates(1, 1, 1);
    drawSin(0.5, 0.5, 1);
void setup2DGraphics(double width, double height)
    double halfWidth = width / 2;
    double halfHeight = height / 2;
    glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, 100, -100);
int main(int argc, char** argv)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(256, 256);
    setup2DGraphics(g_fieldWidth, g_fieldHeight);
    return 0;




1 Comment

Recommended Comments

I moved the sign "y = Sin(x)" on a new position.

Edited by 8Observer8

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By phil67rpg
      I have an idea for a RISK like game. I am going to use the united states as a world map. I break it into six regions I also use the playing cards that are one for each state only the 48 states will be used. the symbols on the cards will be ship plane and tank. I broke the regions into northwest, southwest, northmidwest, southmidwest, northeast and southeast. I am using the same combat principles and armies, also if a player gets a region they will get bonus armies and if the player gets three of a kind in playing cards they get bonus armies. I was thinking about using c++ and directx but I am unsure of how to proceed, what language and graphical library. I do have experience in c# and  c++ and opengl and directx.let me know of what you think of my idea.
    • By Goyira
      I've reading about game/engine initialization both in Game Engine Architecture and Game Coding Complete. I have understood there is a problem with C++ and initialization, because initialization of global variables is non-deterministic, and the order your initialize modules of engine is critic since some modules depends other modules. They explains some approaches to avoid the undefined behavior, but I wonder something. Why must I initialize modules like global variables? Why don't initialize like members of Application object? Maybe last would lead to annoying scope issues, but, why don't initialize in main() scope? Would they have "global" scope in main() and deterministic initialization?
      Thank you and sorry for my English.
    • By tamlam
      I have a vector saving 5 cube coordinates in oxy plan.
      Task: 1st cube roll 90 degrees, then disappear then 2nd cube roll 90 dg … until the last cube in the vector.
      Problem: whenever the program runs, all the cube will appear and roll the same time.
      Q: How can I solve this problem?
      I checked the value in for loop (temp), and value of (count) as below:
      angle 1 count 1
      angle 2 count 2
      temp7 => I do not know why the for loop does not wait for for the “presskey” action to increase the angle value before move to the next temp value.
      int Count(0); void Display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); SetLight(); glPushMatrix(); ConclusiveAxis(); DrawGrid(); for (int temp = 0; temp < cube.coordinates.size(); temp++) { cout<< "temp" << temp << endl; if ((cube.coordinates[temp] == true)) { sDirection = true; if (Count <= 90) { glPushMatrix(); glTranslatef(cube.coordinates[temp].x - 0.5, cube.coordinates[temp].y - 0.5, 0.0); glRotatef(angle, 0, 1, 0); glTranslatef(-0.5, 0.5, 0.5); glColor3f(0.5, 1.5, 1.0); glutSolidCube(1); glPopMatrix(); } else if (Count > 90) { angle = 0; Count = 0; } else { sDirection = false; } } else { } } glPopMatrix(); glutSwapBuffers(); } void SpecialKey(unsigned char key, int x, int y) { switch (key) { case 'r': //condition for next check if (sDirection == true) { angle++; glutPostRedisplay(); Count++; cout << "angle " << angle << " count " << Count << endl; } else { glutPostRedisplay(NULL); } break; } } void OpenGLCallBackAnimation(void) { GLSettings0.PickObject = PickObject0; OpenGLInitialize(0, GLSettings0, 300, 150, 1000, 650, "window"); glutDisplayFunc(Display); glutMouseFunc(MouseButton); glutMotionFunc(OnMouseMotion); glutPassiveMotionFunc(PassiveMotion); glutKeyboardFunc(SpecialKey); //keyboard call glutMouseWheelFunc(OpenGLMouseWheel0); glutReshapeFunc(OpenGLReshape0); //glutTimerFunc(0, time_callback, 0); OpenGLPostprocessor(GLSettings0); }  
    • By LiamBBonney
      Hi, my name is Liam, founder of TheIndieGamesLab! The hub of gaming experimentation. We believe that testing and experimenting with new and unique ideas is incredible important at TheIndieGamesLab. We also understand that when people have created something so different it is important to bring it into the spotlight, whether that be a new mechanic in a game, a new type of visual or even a streamer play testing and experimenting with new games. It is important to break pattern!
      If you feel your work is something unique and fresh we are here to help you not only promote your work but share ideas and experiment further with you. The idea of experimentation is fascinating to us. This is why the name TheIndieGamesLab has been chosen. We are fully aware of the importance of bringing great work and discoveries into the eyes of the public, ESPECIALLY when it’s a new and unique idea!
      Not only will we do our very best to help Indie Game Developers, we are helping any gamer with a new and fresh idea that they want to bring to the table or be displayed in front of the gaming community. This means we are here to help Indie Gamers, Indie Game Devs / Studios and so on! As well as helping the gaming scientists out there trying to bring something fresh to the lab, we want to inspire gamers to start experimenting and create something memorable.
      This is the start of something special. Together we can make TheIndieGamesLab the hub that gaming content creators can come to gain the recognition and support their work deserves! And become a movement! A movement to inspire and create!
      I have also created a discord server providing a platform for indie game developers to communicate with one another to help support each other and upcoming and existing projects. This server lets people get instant support from indie game devs that are just as passionate about it as you! You can also promote you new projects or post updates to your current projects, please com along, I look forward to seeing you there. Let's support each other in this crazy world that is indie games!
      Join now at https://discord.gg/efa6j3a

  • 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!